-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-init.sh
More file actions
138 lines (112 loc) · 3.74 KB
/
Copy pathshell-init.sh
File metadata and controls
138 lines (112 loc) · 3.74 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
#!/usr/bin/env zsh
# Apprentice Shell Integration
# This file provides command logging functionality for the Apprentice system.
APPRENTICE_HOME="${APPRENTICE_HOME:-$HOME/.apprentice}"
APPRENTICE_BUILD_HOME="${APPRENTICE_BUILD_HOME:-$HOME/.apprentice}"
APPRENTICE_LOG_DIR="$APPRENTICE_HOME/memory/logs"
APPRENTICE_OUTPUT_DIR="$APPRENTICE_HOME/memory/outputs"
APPRENTICE_LAST_COMMAND=""
APPRENTICE_LAST_EXIT_CODE=0
# Add apr to PATH
if [[ ! -f "$APPRENTICE_BUILD_HOME/dist/cli.js" ]]; then
echo "Apprentice CLI not found, please run 'npm install && npm run build' in $APPRENTICE_HOME"
return 0
fi
export PATH="$APPRENTICE_BUILD_HOME/node_modules/.bin:$PATH"
# Remove any existing alias and define as function for better completion support
unalias apr 2>/dev/null
function apr {
node "$APPRENTICE_BUILD_HOME/dist/cli.js" "$@"
}
unalias pw 2>/dev/null
alias pw='apr patchwork run'
# Add completions to fpath and initialize
fpath=("$APPRENTICE_HOME/completions" $fpath)
# Initialize completions
autoload -Uz compinit
compinit -C -d "${ZDOTDIR:-$HOME}/.zcompdump-apprentice"
# Associate completion function with apr
compdef _apr apr
# Ensure output directory exists
mkdir -p "$APPRENTICE_OUTPUT_DIR" 2>/dev/null
# Before command: just record the command
apprentice_preexec() {
APPRENTICE_LAST_COMMAND="$1"
}
# After command: log it (without output capture to avoid hangs)
apprentice_precmd() {
APPRENTICE_LAST_EXIT_CODE=$?
# Skip if no command was run
[[ -z "$APPRENTICE_LAST_COMMAND" ]] && return
# Gather context
local cwd=$(pwd)
# Get git branch - prefer symbolic-ref (for normal branches), fall back to describe, then short SHA
local git_branch=$(
git symbolic-ref --short HEAD 2>/dev/null || \
git describe --tags --exact-match HEAD 2>/dev/null || \
git rev-parse --short HEAD 2>/dev/null || \
echo ""
)
# Get git SHA (first 7 characters)
local git_sha=$(git rev-parse --short=7 HEAD 2>/dev/null || echo "")
(
node "$APPRENTICE_BUILD_HOME/dist/log-command.js" \
"$APPRENTICE_LAST_COMMAND" \
"$cwd" \
"$git_branch" \
"$git_sha" \
"$APPRENTICE_LAST_EXIT_CODE" </dev/null 2>/dev/null
) &!
APPRENTICE_LAST_COMMAND=""
}
# Register hooks
autoload -Uz add-zsh-hook
add-zsh-hook preexec apprentice_preexec
add-zsh-hook precmd apprentice_precmd
# Widget for smart git suggestions (Ctrl+G)
apprentice_git_suggest() {
local prefix="$LBUFFER"
local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
local cwd=$(pwd)
# Get suggestions as JSON
local suggestions=$(node "$APPRENTICE_BUILD_HOME/dist/cli.js" suggest \
--prefix "$prefix" \
--json 2>/dev/null)
if [[ -z "$suggestions" || "$suggestions" == "[]" ]]; then
zle -M "No suggestions available"
return
fi
# Extract first suggestion's command
local first_cmd=$(echo "$suggestions" | node -e "
let data = '';
process.stdin.on('data', d => data += d);
process.stdin.on('end', () => {
try {
const suggestions = JSON.parse(data);
if (suggestions.length > 0) {
console.log(suggestions[0].command);
}
} catch {}
});
")
if [[ -n "$first_cmd" ]]; then
LBUFFER="$first_cmd"
RBUFFER=""
zle redisplay
fi
}
zle -N apprentice_git_suggest
# Bind to Ctrl+X g instead of Ctrl+G (which can conflict with terminal)
bindkey '^Xg' apprentice_git_suggest
# Widget for showing all suggestions (Ctrl+X Ctrl+S)
apprentice_show_suggestions() {
local prefix="$LBUFFER"
echo
node "$APPRENTICE_BUILD_HOME/dist/cli.js" suggest --prefix "$prefix" 2>/dev/null
echo
zle reset-prompt
}
zle -N apprentice_show_suggestions
bindkey '^Xs' apprentice_show_suggestions
# Start apprentice indexer daemon (quiet if already running)
apr indexer start --quiet