This repository has no description
3.3 kB
102 lines
1{ ... }:
2{
3 perSystem =
4 { pkgs, ... }:
5 {
6 packages.menu =
7 if pkgs.stdenv.hostPlatform.isDarwin then
8 (pkgs.writeShellApplication {
9 name = "menu";
10 runtimeInputs = [
11 pkgs.choose-gui
12 pkgs.fzf
13 ];
14 text = ''
15 set -efu
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
43 fi
44
45 # If we get here, fzf failed with error (not cancellation)
46
47 # Fall back to GUI
48 choose "$@" < "$INPUT_FILE"
49 '';
50 })
51 else if pkgs.stdenv.hostPlatform.isLinux then
52 (pkgs.writeShellApplication {
53 name = "menu";
54 runtimeInputs = [
55 pkgs.rofi
56 pkgs.wofi
57 pkgs.fzf
58 ];
59 text = ''
60 set -efu
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"
93 else
94 echo "Error: No suitable display environment available" >&2
95 exit 1
96 fi
97 '';
98 })
99 else
100 throw "unsupported system";
101 };
102}