This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

tools.menu: fix terminal precedence

+67 -61
+67 -61
tools/menu.nix
··· 2 2 { 3 3 perSystem = 4 4 { pkgs, ... }: 5 - let 6 - # Function to detect if we're running in a terminal environment 7 - terminalDetectionFunction = '' 8 - is_terminal_environment() { 9 - # Check if we're running in a TTY 10 - if [ -t 0 ] || [ -t 1 ]; then 11 - return 0 # True, we're in a terminal 12 - fi 13 - 14 - # Check if our parent process is a terminal 15 - parent_process=$(ps -o comm= $PPID 2>/dev/null || true) 16 - terminal_programs="bash zsh fish sh ksh csh tcsh dash xterm konsole gnome-terminal alacritty kitty terminal terminator urxvt rxvt xfce4-terminal iterm2 Terminal" 17 - for term in $terminal_programs; do 18 - if echo "$parent_process" | grep -q "$term"; then 19 - return 0 # True, parent is a terminal 20 - fi 21 - done 22 - 23 - # Check if we're in an interactive environment 24 - if [ -n "$TERM" ] && [ "$TERM" != "dumb" ]; then 25 - # Check if we're in an SSH session 26 - if [ -n "$SSH_TTY" ] || [ -n "$SSH_CONNECTION" ] || [ -n "$SSH_CLIENT" ]; then 27 - return 0 # True, SSH session implies terminal 28 - fi 29 - fi 30 - 31 - return 1 # False, not in a terminal 32 - } 33 - ''; 34 - in 35 - { 5 + { 36 6 packages.menu = 37 7 if pkgs.stdenv.hostPlatform.isDarwin then 38 8 (pkgs.writeShellApplication { ··· 40 10 runtimeInputs = [ 41 11 pkgs.choose-gui 42 12 pkgs.fzf 43 - pkgs.procps 44 13 ]; 45 14 text = '' 46 15 set -efu 47 - 48 - # Terminal detection function 49 - ${terminalDetectionFunction} 50 - 51 - # Decide which menu program to use 52 - if is_terminal_environment; then 53 - # Use terminal-based menu 54 - cat | fzf "$@" 55 - else 56 - # Use GUI-based menu 57 - cat | choose "$@" 16 + 17 + # Create temporary files for input and output 18 + TEMP_DIR=$(mktemp -d) 19 + INPUT_FILE="$TEMP_DIR/input" 20 + OUTPUT_FILE="$TEMP_DIR/output" 21 + 22 + # Set up trap to clean up temp dir on exit 23 + trap 'rm -rf "$TEMP_DIR"' EXIT 24 + 25 + # Save stdin to file 26 + cat > "$INPUT_FILE" 27 + 28 + # Try fzf first with terminal UI - explicitly store exit code 29 + # This avoids issues with losing the exit code in a pipe 30 + set +e 31 + fzf "$@" < "$INPUT_FILE" > "$OUTPUT_FILE" 2>/dev/null 32 + FZF_EXIT=$? 33 + set -e 34 + 35 + # Handle fzf results based on exit code 36 + if [ $FZF_EXIT -eq 0 ]; then 37 + # Success - output result and exit 38 + cat "$OUTPUT_FILE" 39 + exit 0 40 + elif [ $FZF_EXIT -eq 130 ]; then 41 + # User pressed Escape - honor the cancellation 42 + exit 130 58 43 fi 44 + 45 + # If we get here, fzf failed with error (not cancellation) 46 + 47 + # Fall back to GUI 48 + choose "$@" < "$INPUT_FILE" 59 49 ''; 60 50 }) 61 51 else if pkgs.stdenv.hostPlatform.isLinux then ··· 65 55 pkgs.rofi 66 56 pkgs.wofi 67 57 pkgs.fzf 68 - pkgs.procps 69 58 ]; 70 59 text = '' 71 60 set -efu 72 - 73 - # Terminal detection function 74 - ${terminalDetectionFunction} 75 - 76 - # Decide which menu program to use 77 - if is_terminal_environment; then 78 - # Use terminal-based menu 79 - cat | fzf "$@" 80 - elif [[ -n $WAYLAND_DISPLAY ]]; then 81 - # Use wofi in Wayland 82 - cat | wofi -d "$@" 83 - elif [[ -n $DISPLAY ]]; then 84 - # Use rofi in X11 85 - cat | rofi -dmenu "$@" 61 + # Create temporary files for input and output 62 + TEMP_DIR=$(mktemp -d) 63 + INPUT_FILE="$TEMP_DIR/input" 64 + OUTPUT_FILE="$TEMP_DIR/output" 65 + 66 + # Save stdin to file 67 + cat > "$INPUT_FILE" 68 + 69 + # Try fzf first with terminal UI - explicitly store exit code 70 + # This avoids issues with losing the exit code in a pipe 71 + set +e 72 + fzf "$@" < "$INPUT_FILE" > "$OUTPUT_FILE" 2>/dev/null 73 + FZF_EXIT=$? 74 + set -e 75 + 76 + # Handle fzf results based on exit code 77 + if [ $FZF_EXIT -eq 0 ]; then 78 + # Success - output result and exit 79 + cat "$OUTPUT_FILE" 80 + exit 0 81 + elif [ $FZF_EXIT -eq 130 ]; then 82 + # User pressed Escape - honor the cancellation 83 + exit 130 84 + fi 85 + 86 + # If we get here, fzf failed with error (not cancellation) 87 + 88 + # Fall back to GUI based on available display environment 89 + if [ -n "''${WAYLAND_DISPLAY:-}" ]; then 90 + wofi -d "$@" < "$INPUT_FILE" 91 + elif [ -n "''${DISPLAY:-}" ]; then 92 + rofi -dmenu "$@" < "$INPUT_FILE" 86 93 else 87 - # No terminal or GUI environment, fallback to fzf 88 - cat | fzf "$@" 94 + echo "Error: No suitable display environment available" >&2 95 + exit 1 89 96 fi 90 97 ''; 91 - 92 98 }) 93 99 else 94 100 throw "unsupported system";