This repository has no description
0

Configure Feed

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

tools.menu: add smart terminal detection to prefer fzf in terminal environments

+62 -6
+62 -6
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 5 35 { 6 36 packages.menu = 7 37 if pkgs.stdenv.hostPlatform.isDarwin then ··· 9 39 name = "menu"; 10 40 runtimeInputs = [ 11 41 pkgs.choose-gui 42 + pkgs.fzf 43 + pkgs.procps 12 44 ]; 13 45 text = '' 14 46 set -efu 15 - # TODO check if a terminal is available and use fzf in that case 16 - exec choose "$@" 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 "$@" 58 + fi 17 59 ''; 18 60 }) 19 61 else if pkgs.stdenv.hostPlatform.isLinux then ··· 22 64 runtimeInputs = [ 23 65 pkgs.rofi 24 66 pkgs.wofi 67 + pkgs.fzf 68 + pkgs.procps 25 69 ]; 26 70 text = '' 27 71 set -efu 28 - # TODO check if a terminal is available and use fzf in that case 29 - if [[ -n $WAYLAND_DISPLAY ]]; then 30 - exec wofi "$@" 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 "$@" 31 83 elif [[ -n $DISPLAY ]]; then 32 - exec rofi "$@" 84 + # Use rofi in X11 85 + cat | rofi -dmenu "$@" 86 + else 87 + # No terminal or GUI environment, fallback to fzf 88 + cat | fzf "$@" 33 89 fi 34 90 ''; 35 91