this repo has no description
1# Natural language to shell command widget
2# Usage: source this file, then type natural language and press Ctrl+G
3
4typeset -g _NLCMD_SCRIPT_DIR="${0:A:h}"
5
6nlcmd() {
7 local input="$BUFFER"
8 local spin='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
9
10 # Use a FIFO for streaming
11 local fifo=$(mktemp -u)
12 mkfifo "$fifo"
13
14 # Start CLI in background, write to FIFO
15 ( echo "$input" | bun "$_NLCMD_SCRIPT_DIR/cli.ts" 2>/dev/null > "$fifo" ) &!
16 local pid=$!
17
18 # Open FIFO for reading (non-blocking setup)
19 exec {fd}<"$fifo"
20
21 local char i=0 first=1
22
23 while true; do
24 if read -r -k 1 -u $fd -t 0.05 char 2>/dev/null; then
25 if [[ $first -eq 1 ]]; then
26 BUFFER=""
27 first=0
28 fi
29 BUFFER+="$char"
30 CURSOR=${#BUFFER}
31 zle redisplay
32 else
33 local ret=$?
34 # Check if process is still running
35 if ! kill -0 $pid 2>/dev/null; then
36 # Process done, drain remaining
37 while read -r -k 1 -u $fd char 2>/dev/null; do
38 if [[ $first -eq 1 ]]; then
39 BUFFER=""
40 first=0
41 fi
42 BUFFER+="$char"
43 done
44 break
45 fi
46 # Still waiting - show spinner
47 if [[ $first -eq 1 ]]; then
48 BUFFER="$input ${spin:$((i % 10)):1}"
49 CURSOR=${#BUFFER}
50 zle redisplay
51 ((i++))
52 fi
53 fi
54 done
55
56 exec {fd}<&-
57 rm -f "$fifo"
58}
59
60zle -N nlcmd
61bindkey '^G' nlcmd