This repository was archived by the owner on Jun 18, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathforge.plugin.zsh
More file actions
198 lines (159 loc) · 6.56 KB
/
Copy pathforge.plugin.zsh
File metadata and controls
198 lines (159 loc) · 6.56 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env zsh
# Documentation in [README.md](./README.md)
# Configuration: Change these variables to customize the forge command and special characters
# Using typeset to keep variables local to plugin scope and prevent public exposure
typeset -h _FORGE_BIN="${FORGE_BIN:-forge}"
typeset -h _FORGE_CONVERSATION_PATTERN=":"
# Detect fd command - Ubuntu/Debian use 'fdfind', others use 'fd'
typeset -h _FORGE_FD_CMD="$(command -v fdfind 2>/dev/null || command -v fd 2>/dev/null || echo 'fd')"
# Style tagged files
ZSH_HIGHLIGHT_PATTERNS+=('@\[[^]]#\]' 'fg=cyan,bold')
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(pattern)
# Style the conversation pattern with appropriate highlighting
# Keywords in yellow, rest in default white
# Highlight colon + word at the beginning in yellow
ZSH_HIGHLIGHT_PATTERNS+=('(#s):[a-zA-Z]#' 'fg=yellow,bold')
# Highlight everything after that word + space in white
ZSH_HIGHLIGHT_PATTERNS+=('(#s):[a-zA-Z]# *(*|[[:graph:]]*)' 'fg=white,bold')
# Private fzf function with common options for consistent UX
function _forge_fzf() {
fzf --cycle --select-1 --height 40% --reverse "$@"
}
# Helper function to print operating agent messages with consistent formatting
function _forge_print_agent_message() {
local agent_name="${1:-${FORGE_ACTIVE_AGENT}}"
echo "\033[33m⏺\033[0m \033[90m[$(date '+%H:%M:%S')] \033[1;37m${agent_name:u}\033[0m \033[90mis now the active agent\033[0m"
}
# Store conversation ID in a temporary variable (local to plugin)
export FORGE_CONVERSATION_ID=""
export FORGE_ACTIVE_AGENT="forge"
# Custom completion widget that handles both :commands and @ completion
function forge-completion() {
local current_word="${LBUFFER##* }"
# Handle @ completion (existing functionality)
if [[ "$current_word" =~ ^@.*$ ]]; then
local filter_text="${current_word#@}"
local selected
if [[ -n "$filter_text" ]]; then
selected=$($_FORGE_FD_CMD --type f --hidden --exclude .git | _forge_fzf --query "$filter_text")
else
selected=$($_FORGE_FD_CMD --type f --hidden --exclude .git | _forge_fzf)
fi
if [[ -n "$selected" ]]; then
selected="@[${selected}]"
LBUFFER="${LBUFFER%$current_word}"
BUFFER="${LBUFFER}${selected}${RBUFFER}"
CURSOR=$((${#LBUFFER} + ${#selected}))
fi
zle reset-prompt
return 0
fi
# Handle :command completion
if [[ "${LBUFFER}" =~ "^:[a-zA-Z]*$" ]]; then
# Extract the text after the colon for filtering
local filter_text="${LBUFFER#:}"
# Get available commands from forge show-agents
local command_output
command_output=$($_FORGE_BIN show-agents 2>/dev/null)
if [[ $? -eq 0 && -n "$command_output" ]]; then
# Use fzf for interactive selection with prefilled filter
local selected
if [[ -n "$filter_text" ]]; then
selected=$(echo "$command_output" | _forge_fzf --nth=1 --query "$filter_text" --prompt="Agent ❯ ")
else
selected=$(echo "$command_output" | _forge_fzf --nth=1 --prompt="Agent ❯ ")
fi
if [[ -n "$selected" ]]; then
# Extract just the command name (first word before any description)
local command_name="${selected%% *}"
# Replace the current buffer with the selected command
BUFFER=":$command_name "
CURSOR=${#BUFFER}
fi
fi
zle reset-prompt
return 0
fi
# Fall back to default completion
zle expand-or-complete
}
function forge-accept-line() {
# Save the original command for history
local original_buffer="$BUFFER"
# Parse the buffer first in parent shell context to avoid subshell issues
local user_action=""
local input_text=""
# Check if the line starts with any of the supported patterns
if [[ "$BUFFER" =~ "^:([a-zA-Z][a-zA-Z0-9_-]*)( (.*))?$" ]]; then
# Action with or without parameters: :foo or :foo bar baz
user_action="${match[1]}"
input_text="${match[3]:-}" # Use empty string if no parameters
elif [[ "$BUFFER" =~ "^: (.*)$" ]]; then
# Default action with parameters: : something
user_action=""
input_text="${match[1]}"
else
# For non-:commands, use normal accept-line
zle accept-line
return
fi
# Add the original command to history before transformation
print -s -- "$original_buffer"
# Handle reset command specially
if [[ "$user_action" == "reset" || "$user_action" == "r" ]]; then
echo
if [[ -n "$FORGE_CONVERSATION_ID" ]]; then
echo "\033[36m⏺\033[0m \033[90m[$(date '+%H:%M:%S')] Reset ${FORGE_CONVERSATION_ID}\033[0m"
fi
_forge_print_agent_message "FORGE"
FORGE_CONVERSATION_ID=""
FORGE_ACTIVE_AGENT="forge"
BUFFER=""
CURSOR=${#BUFFER}
zle reset-prompt
return 0
fi
# Handle info command specially
if [[ "$user_action" == "info" || "$user_action" == "i" ]]; then
echo
# Run forge info
$_FORGE_BIN info
BUFFER=""
CURSOR=${#BUFFER}
zle reset-prompt
return 0
fi
# Check if input_text is empty - just set the active agent
if [[ -z "$input_text" ]]; then
echo
FORGE_ACTIVE_AGENT="${user_action:-${FORGE_ACTIVE_AGENT}}"
_forge_print_agent_message
BUFFER=""
CURSOR=0
zle reset-prompt
return 0
fi
# Generate conversation ID if needed (in parent shell context)
if [[ -z "$FORGE_CONVERSATION_ID" ]]; then
FORGE_CONVERSATION_ID=$($_FORGE_BIN --generate-conversation-id)
fi
# Set the active agent for this execution
FORGE_ACTIVE_AGENT="${user_action:-${FORGE_ACTIVE_AGENT}}"
# Build and execute the forge command
local forge_cmd="$_FORGE_BIN"
local quoted_input=${input_text//\'/\'\\\'\'}
local full_command="$forge_cmd -p '$quoted_input'"
# Set buffer to the transformed command and execute
BUFFER="$full_command"
zle accept-line
return
}
# Register ZLE widgets
zle -N forge-accept-line
# Register completions
zle -N forge-completion
# Bind Enter to our custom accept-line that transforms :commands
bindkey '^M' forge-accept-line
bindkey '^J' forge-accept-line
# Update the Tab binding to use the new completion widget
bindkey '^I' forge-completion # Tab for both @ and :command completion