-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshellock.fish
More file actions
143 lines (119 loc) · 3.29 KB
/
shellock.fish
File metadata and controls
143 lines (119 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Shellock - Fish shell integration
# Real-time CLI flag explainer
# Track the number of lines we displayed (for cleanup)
set -g __shellock_lines 0
function __shellock_explain
# Get current command line
set -l cmdline (commandline -b)
# Skip if empty or just whitespace
if test -z (string trim "$cmdline")
__shellock_clear
return
end
# Skip if no flags (nothing starting with -)
if not string match -q '*-*' "$cmdline"
__shellock_clear
return
end
# Get explanations from shellock
set -l shellock_path (dirname (status filename))/shellock.py
if not test -x "$shellock_path"
return
end
# Run shellock and capture output
set -l output ($shellock_path explain "$cmdline" 2>/dev/null)
if test -z "$output"
__shellock_clear
return
end
# Clear previous output
__shellock_clear
# Count lines
set -g __shellock_lines (count $output)
# Save cursor position and move to line below prompt
# Use terminal escape sequences
printf '\e[s' # Save cursor
# Print each line of explanation
for line in $output
printf '\n%s' "$line"
end
printf '\e[u' # Restore cursor
end
function __shellock_clear
if test $__shellock_lines -gt 0
# Save cursor, clear the lines we wrote, restore cursor
printf '\e[s'
for i in (seq $__shellock_lines)
printf '\n\e[2K' # Move down and clear line
end
printf '\e[u'
set -g __shellock_lines 0
end
end
function __shellock_on_dash
# Insert the dash character
commandline -i '-'
# Then show explanations
__shellock_explain
end
function __shellock_on_space
# Insert space
commandline -i ' '
# Check if we should update explanations
set -l cmdline (commandline -b)
# Only update if there are flags
if string match -q '*-*' "$cmdline"
__shellock_explain
end
end
function __shellock_on_enter
# Clear explanations before executing
__shellock_clear
# Execute the command
commandline -f execute
end
function __shellock_on_ctrl_c
# Clear explanations on cancel
__shellock_clear
commandline -f cancel-commandline
end
function __shellock_manual
# Manual trigger with Ctrl+H
__shellock_explain
end
function __shellock_on_backspace
# Perform normal backspace
commandline -f backward-delete-char
# Update explanations (will clear if no flags left)
__shellock_explain
end
function __shellock_on_delete
# Perform normal delete
commandline -f delete-char
# Update explanations (will clear if no flags left)
__shellock_explain
end
function __shellock_on_kill_whole_line
# Kill entire line (Ctrl+U)
commandline -f kill-whole-line
# Update explanations (will clear since no content remains)
__shellock_explain
end
function __shellock_on_kill_line
# Kill to end of line (Ctrl+K)
commandline -f kill-line
# Update explanations
__shellock_explain
end
function __shellock_on_backward_kill_word
# Kill word backward (Ctrl+W)
commandline -f backward-kill-word
# Update explanations
__shellock_explain
end
function __shellock_on_kill_word
# Kill word forward (Alt+D)
commandline -f kill-word
# Update explanations
__shellock_explain
end