claude-rpc 1.5.4 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -55
- package/package.json +2 -1
- package/src/cli.js +134 -90
- package/src/commands.js +245 -0
- package/src/completion.js +196 -0
- package/src/doctor.js +1 -1
- package/src/help.js +117 -0
- package/src/nudge.js +2 -2
- package/src/png.js +135 -0
- package/src/pricing.js +1 -1
- package/src/select.js +60 -0
- package/src/statcards.js +278 -0
- package/src/version.js +1 -1
package/src/commands.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// The one table of CLI commands. `help`, `help <command>`, tab completion
|
|
2
|
+
// (`__complete`) and the "did you mean" hint all read it, so a command, flag
|
|
3
|
+
// or sub-command added here shows up in all four at once. Dispatch itself
|
|
4
|
+
// stays in cli.js's switch; test/commands.test.js fails when the two drift.
|
|
5
|
+
//
|
|
6
|
+
// Shape of an entry:
|
|
7
|
+
// name what you type
|
|
8
|
+
// group help section (GROUPS order); omitted → hidden from help + completion
|
|
9
|
+
// summary one line for the list
|
|
10
|
+
// usage argument synopsis after the name ('' when none)
|
|
11
|
+
// subs [name, summary] positional choices for the first argument
|
|
12
|
+
// flags [flag, summary, values?] — values: an array, 'file', 'project' or
|
|
13
|
+
// 'any' (takes a free-form value); no values → boolean switch
|
|
14
|
+
// aliases older names that still work (stable per VERSIONING.md), not listed
|
|
15
|
+
// examples shown by `help <command>`
|
|
16
|
+
//
|
|
17
|
+
// Pure data + pure helpers: no fs, no imports from the core graph — the
|
|
18
|
+
// completion path loads this on every <Tab>.
|
|
19
|
+
|
|
20
|
+
export const GROUPS = [
|
|
21
|
+
['start', 'Get started'],
|
|
22
|
+
['stats', 'Your stats'],
|
|
23
|
+
['share', 'Share'],
|
|
24
|
+
['privacy', 'Privacy'],
|
|
25
|
+
['online', 'Online'],
|
|
26
|
+
['daemon', 'Daemon'],
|
|
27
|
+
['fix', 'Troubleshoot'],
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const METRICS = ['hours', 'tokens', 'streak', 'sessions'];
|
|
31
|
+
const RANGES = ['week', 'month', 'year', 'all'];
|
|
32
|
+
export const SHELLS = ['bash', 'zsh', 'fish', 'powershell'];
|
|
33
|
+
|
|
34
|
+
export const COMMANDS = [
|
|
35
|
+
// ── Get started ──
|
|
36
|
+
{ name: 'setup', group: 'start', summary: 'Hook into Claude Code and start the card — run once',
|
|
37
|
+
flags: [['--link', 'Also verify this machine with a claude-rpc.com code', 'any'],
|
|
38
|
+
['--wrapped', 'With --link: publish your year-in-review too']],
|
|
39
|
+
aliases: ['install'],
|
|
40
|
+
examples: ['claude-rpc setup', 'claude-rpc setup --link ABCD-1234 --wrapped'] },
|
|
41
|
+
{ name: 'completion', group: 'start', summary: 'Tab completion for your shell', usage: '[install|uninstall|<shell>]',
|
|
42
|
+
subs: [['install', 'Turn it on for your shell (asks nothing else)'], ['uninstall', 'Turn it off'],
|
|
43
|
+
...SHELLS.map((s) => [s, `Print the ${s} script`])],
|
|
44
|
+
examples: ['claude-rpc completion install', 'claude-rpc completion zsh > ~/.zfunc/_claude-rpc'] },
|
|
45
|
+
{ name: 'uninstall', group: 'start', summary: 'Undo setup (keeps your config and stats)' },
|
|
46
|
+
|
|
47
|
+
// ── Your stats ──
|
|
48
|
+
{ name: 'status', group: 'stats', summary: 'Live stats dashboard in the terminal',
|
|
49
|
+
flags: [['--dump', 'Print once as plain text (also when piped)']], aliases: ['dump'] },
|
|
50
|
+
{ name: 'today', group: 'stats', summary: 'Today, plus when in the day you code' },
|
|
51
|
+
{ name: 'week', group: 'stats', summary: 'This week day by day',
|
|
52
|
+
flags: [['--compare', 'Side by side with last week']], aliases: ['compare'],
|
|
53
|
+
examples: ['claude-rpc week', 'claude-rpc week --compare'] },
|
|
54
|
+
{ name: 'projects', group: 'stats', summary: 'Projects ranked by time — or one project in detail', usage: '[name]',
|
|
55
|
+
args: 'project', flags: [['--limit', 'How many to list (default 12)', 'any']], aliases: ['project'],
|
|
56
|
+
examples: ['claude-rpc projects', 'claude-rpc projects claude-rpc'] },
|
|
57
|
+
{ name: 'usage', group: 'stats', summary: 'Plan limits: session and weekly % used' },
|
|
58
|
+
{ name: 'recap', group: 'stats', summary: 'Standup summary (yesterday by default)', usage: '[today|yesterday|week|YYYY-MM-DD]',
|
|
59
|
+
subs: [['yesterday', 'Default — Monday covers Friday'], ['today', 'So far today'], ['week', 'This week']],
|
|
60
|
+
flags: [['--md', 'Paste-ready markdown'], ['--json', 'Raw numbers']],
|
|
61
|
+
examples: ['claude-rpc recap', 'claude-rpc recap week --md'] },
|
|
62
|
+
{ name: 'insights', group: 'stats', summary: 'Patterns spotted in your history' },
|
|
63
|
+
{ name: 'wrapped', group: 'stats', summary: 'Your animated year in review',
|
|
64
|
+
flags: [['--publish', 'Put it on claude-rpc.com/wrapped/<handle>'], ['--year', 'Which year', 'any'], ['--yes', 'Skip the confirm']] },
|
|
65
|
+
{ name: 'serve', group: 'stats', summary: 'Open the live web dashboard' },
|
|
66
|
+
{ name: 'export', group: 'stats', summary: 'Everything as JSON, or daily rows as CSV',
|
|
67
|
+
flags: [['--csv', 'Daily rows as CSV'], ['--out', 'Write to a file', 'file']] },
|
|
68
|
+
|
|
69
|
+
// ── Share ──
|
|
70
|
+
{ name: 'statcards', group: 'share', summary: 'Cards and badges as a live link, PNG or SVG', usage: '[card]',
|
|
71
|
+
subs: [['stats', 'Stats + year heatmap'], ['profile', 'Profile card'], ['badge', 'Small badge'],
|
|
72
|
+
['heatmap', 'Year heatmap'], ['poster', 'Poster summary'], ['session', 'This session'],
|
|
73
|
+
['wrapped', 'Year-in-review card'], ['list', 'What each card is and where it can go']],
|
|
74
|
+
flags: [['--link', 'Live, self-updating URL (needs a public profile)'], ['--png', 'Save a PNG (optional path)', 'file'],
|
|
75
|
+
['--svg', 'Save an SVG (optional path)', 'file'], ['--md', 'With --link: print Markdown'],
|
|
76
|
+
['--html', 'With --link: print an <img> tag'], ['--metric', 'What a badge/heatmap counts', METRICS],
|
|
77
|
+
['--range', 'Window for badge/poster', RANGES], ['--copy', 'Copy the snippet to the clipboard']],
|
|
78
|
+
aliases: ['badge', 'card', 'github-stat', 'calendar', 'session-card', 'readme'],
|
|
79
|
+
examples: ['claude-rpc statcards', 'claude-rpc statcards stats --link --md', 'claude-rpc statcards poster --png', 'claude-rpc statcards badge --metric streak --svg streak.svg'] },
|
|
80
|
+
{ name: 'statusline', group: 'share', summary: 'One-line status for tmux and shell prompts',
|
|
81
|
+
flags: [['--template', 'Template using any `claude-rpc vars` name', 'any']] },
|
|
82
|
+
{ name: 'mcp', group: 'share', summary: 'Let Claude read your stats (MCP server)', usage: '[install|uninstall]',
|
|
83
|
+
subs: [['install', 'Wire it into Claude Code'], ['uninstall', 'Remove it']] },
|
|
84
|
+
|
|
85
|
+
// ── Privacy ──
|
|
86
|
+
{ name: 'private', group: 'privacy', summary: 'Hide this folder from Discord — or show it again', usage: '[on|off|status]',
|
|
87
|
+
subs: [['on', 'Hide this folder (default)'], ['off', 'Show it again'], ['status', 'What Discord sees for this folder']],
|
|
88
|
+
aliases: ['public', 'privacy'],
|
|
89
|
+
examples: ['claude-rpc private', 'claude-rpc private off', 'claude-rpc private status'] },
|
|
90
|
+
{ name: 'pause', group: 'privacy', summary: 'Hide the card everywhere for a while (default 1h)', usage: '[30m|2h|off|status]',
|
|
91
|
+
subs: [['30m', '30 minutes'], ['1h', 'An hour'], ['2h', 'Two hours'], ['off', 'Show the card again'], ['status', 'Paused?']],
|
|
92
|
+
aliases: ['resume', 'unpause'],
|
|
93
|
+
examples: ['claude-rpc pause 2h', 'claude-rpc pause off'] },
|
|
94
|
+
|
|
95
|
+
// ── Online ──
|
|
96
|
+
{ name: 'profile', group: 'online', summary: 'Your public leaderboard profile', usage: '[status|set|on|off|publish|verify]',
|
|
97
|
+
subs: [['status', 'Show it'], ['set', 'Change handle / name / github'], ['on', 'Go public'], ['off', 'Go private'],
|
|
98
|
+
['publish', 'Push your numbers now'], ['verify', 'Prove the GitHub account is yours']],
|
|
99
|
+
flags: [['--handle', 'With set: your handle', 'any'], ['--name', 'With set: display name', 'any'], ['--github', 'With set: GitHub login', 'any']] },
|
|
100
|
+
{ name: 'squad', group: 'online', summary: 'Private leaderboards with friends', usage: '[status|create|join|leave]',
|
|
101
|
+
subs: [['status', 'Your squads'], ['create', 'Start one'], ['join', 'Join with a code'], ['leave', 'Leave one']] },
|
|
102
|
+
{ name: 'link', group: 'online', summary: 'Join machines into one profile', usage: '[code]' },
|
|
103
|
+
{ name: 'community', group: 'online', summary: 'Anonymous community totals', usage: '[status|on|off|report]',
|
|
104
|
+
subs: [['status', 'Show it'], ['on', 'Opt in'], ['off', 'Opt out'], ['report', 'Send now']] },
|
|
105
|
+
|
|
106
|
+
// ── Daemon ──
|
|
107
|
+
{ name: 'start', group: 'daemon', summary: 'Start the Discord daemon' },
|
|
108
|
+
{ name: 'stop', group: 'daemon', summary: 'Stop it' },
|
|
109
|
+
{ name: 'restart', group: 'daemon', summary: 'Restart it and wait until it is up' },
|
|
110
|
+
{ name: 'logs', group: 'daemon', summary: 'Follow the daemon log', aliases: ['tail', 'log'] },
|
|
111
|
+
|
|
112
|
+
// ── Troubleshoot ──
|
|
113
|
+
{ name: 'doctor', group: 'fix', summary: 'Check everything; --fix repairs what it can',
|
|
114
|
+
flags: [['--fix', 'Apply the repairs']] },
|
|
115
|
+
{ name: 'scan', group: 'fix', summary: 'Re-count your history from transcripts',
|
|
116
|
+
flags: [['--force', 'Re-parse everything, ignore the cache']], aliases: ['rescan'] },
|
|
117
|
+
{ name: 'backfill', group: 'fix', summary: 'Import transcripts from a backup or another machine', usage: '<dir>', args: 'file' },
|
|
118
|
+
{ name: 'preview', group: 'fix', summary: 'How every card frame renders right now' },
|
|
119
|
+
{ name: 'vars', group: 'fix', summary: 'Every template variable and its live value (JSON)' },
|
|
120
|
+
{ name: 'upgrade-config', summary: 'Re-run config migrations' },
|
|
121
|
+
{ name: 'daemon', summary: 'Run the daemon in the foreground (debug)' },
|
|
122
|
+
{ name: 'help', summary: 'This screen, or one command in detail', usage: '[command]', args: 'command' },
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
// Internal entry points the dispatcher knows but nobody types.
|
|
126
|
+
export const INTERNAL = ['hook', 'scan-worker', '__complete', '--version', '-V', '-v', '--help', '-h'];
|
|
127
|
+
|
|
128
|
+
const BY_NAME = new Map();
|
|
129
|
+
for (const cmd of COMMANDS) {
|
|
130
|
+
BY_NAME.set(cmd.name, cmd);
|
|
131
|
+
for (const a of cmd.aliases || []) BY_NAME.set(a, cmd);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// The command entry for a name or any alias, or null.
|
|
135
|
+
export function findCommand(name) {
|
|
136
|
+
return BY_NAME.get(name) || null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Every name the dispatcher must accept: commands, aliases, internals.
|
|
140
|
+
export function allNames() {
|
|
141
|
+
return [...BY_NAME.keys(), ...INTERNAL];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function visibleCommands() {
|
|
145
|
+
return COMMANDS.filter((c) => c.group);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Damerau-free Levenshtein, small strings only.
|
|
149
|
+
function distance(a, b) {
|
|
150
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
151
|
+
for (let i = 1; i <= a.length; i++) {
|
|
152
|
+
let prev = row[0];
|
|
153
|
+
row[0] = i;
|
|
154
|
+
for (let j = 1; j <= b.length; j++) {
|
|
155
|
+
const tmp = row[j];
|
|
156
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
157
|
+
prev = tmp;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return row[b.length];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// What each older name now spells, for `help <old>` and typo hints.
|
|
164
|
+
export const ALIAS_HINT = {
|
|
165
|
+
install: 'setup', dump: 'status --dump', compare: 'week --compare', project: 'projects <name>',
|
|
166
|
+
rescan: 'scan --force', resume: 'pause off', unpause: 'pause off', public: 'private off',
|
|
167
|
+
privacy: 'private status', tail: 'logs', log: 'logs', badge: 'statcards badge', card: 'statcards poster',
|
|
168
|
+
'github-stat': 'statcards profile', calendar: 'statcards heatmap', 'session-card': 'statcards session',
|
|
169
|
+
readme: 'statcards stats --link',
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// "did you mean" for an unknown command: a unique prefix match first
|
|
173
|
+
// (`stat` is ambiguous, `statc` → statcards), else the closest name within 2
|
|
174
|
+
// edits. Older names count too, so muscle memory still gets a hint — the hint
|
|
175
|
+
// is the name that matched, which still works.
|
|
176
|
+
export function suggestCommand(input) {
|
|
177
|
+
const s = String(input || '').toLowerCase();
|
|
178
|
+
if (!s) return null;
|
|
179
|
+
const names = [...BY_NAME.keys()];
|
|
180
|
+
const prefixed = names.filter((n) => n.startsWith(s));
|
|
181
|
+
if (prefixed.length && new Set(prefixed.map((n) => BY_NAME.get(n).name)).size === 1) {
|
|
182
|
+
return prefixed.sort((a, b) => a.length - b.length)[0];
|
|
183
|
+
}
|
|
184
|
+
let best = null;
|
|
185
|
+
let bestD = 3;
|
|
186
|
+
for (const n of names) {
|
|
187
|
+
const d = distance(s, n);
|
|
188
|
+
if (d < bestD) { bestD = d; best = n; }
|
|
189
|
+
}
|
|
190
|
+
return best;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ── completion ───────────────────────────────────────────────────────────
|
|
194
|
+
//
|
|
195
|
+
// complete(words, ctx) → candidate lines, each "value" or "value\tdescription".
|
|
196
|
+
// `words` are the args after `claude-rpc`, the last one being the word under
|
|
197
|
+
// the cursor (possibly ''). A lone '__files__' line asks the shell to fall back
|
|
198
|
+
// to file-name completion. ctx.projects() lazily supplies project names.
|
|
199
|
+
|
|
200
|
+
export const FILES = '__files__';
|
|
201
|
+
|
|
202
|
+
function matching(pairs, cur) {
|
|
203
|
+
return pairs.filter(([v]) => v.startsWith(cur)).map(([v, d]) => (d ? `${v}\t${d}` : v));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function valueCandidates(kind, cur, ctx) {
|
|
207
|
+
if (kind === 'file') return [FILES];
|
|
208
|
+
if (kind === 'project') return matching((ctx.projects?.() || []).map((p) => [p, '']), cur);
|
|
209
|
+
if (kind === 'command') return matching(visibleCommands().map((c) => [c.name, c.summary]), cur);
|
|
210
|
+
if (Array.isArray(kind)) return matching(kind.map((v) => [v, '']), cur);
|
|
211
|
+
return [];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function complete(words, ctx = {}) {
|
|
215
|
+
const w = words.length ? words : [''];
|
|
216
|
+
const cur = w[w.length - 1];
|
|
217
|
+
if (w.length === 1) return matching(visibleCommands().map((c) => [c.name, c.summary]), cur);
|
|
218
|
+
|
|
219
|
+
const cmd = findCommand(w[0]);
|
|
220
|
+
if (!cmd) return [];
|
|
221
|
+
const flags = cmd.flags || [];
|
|
222
|
+
const prev = w[w.length - 2];
|
|
223
|
+
|
|
224
|
+
// Value for the flag just typed (`--metric <Tab>`).
|
|
225
|
+
const flag = flags.find(([f]) => f === prev);
|
|
226
|
+
if (flag && flag[2] && !cur.startsWith('-')) return valueCandidates(flag[2], cur, ctx);
|
|
227
|
+
|
|
228
|
+
if (cur.startsWith('-')) {
|
|
229
|
+
const used = new Set(w.slice(1, -1));
|
|
230
|
+
return matching(flags.filter(([f]) => !used.has(f)).map(([f, d]) => [f, d]), cur);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// First positional → the command's sub-commands or argument values.
|
|
234
|
+
const positionals = w.slice(1, -1).filter((x, i, arr) => {
|
|
235
|
+
if (x.startsWith('-')) return false;
|
|
236
|
+
const before = i > 0 ? arr[i - 1] : w[0];
|
|
237
|
+
const f = flags.find(([name]) => name === before);
|
|
238
|
+
return !(f && f[2]);
|
|
239
|
+
});
|
|
240
|
+
if (positionals.length === 0) {
|
|
241
|
+
if (cmd.subs) return matching(cmd.subs, cur);
|
|
242
|
+
if (cmd.args) return valueCandidates(cmd.args, cur, ctx);
|
|
243
|
+
}
|
|
244
|
+
return [];
|
|
245
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// Shell tab completion. Each script is a thin shim that asks
|
|
2
|
+
// `claude-rpc __complete <words…>` for candidates, so the command table in
|
|
3
|
+
// commands.js is the only list — new commands complete without reinstalling.
|
|
4
|
+
//
|
|
5
|
+
// claude-rpc completion <bash|zsh|fish|powershell> print the script
|
|
6
|
+
// claude-rpc completion install [shell] turn it on
|
|
7
|
+
// claude-rpc completion uninstall turn it off everywhere
|
|
8
|
+
//
|
|
9
|
+
// install writes the script under <config dir>/completion/ and adds one
|
|
10
|
+
// marked line to the shell's rc file (fish needs no rc line: its completions
|
|
11
|
+
// dir autoloads). Re-running is a no-op; uninstall removes exactly the marked
|
|
12
|
+
// block.
|
|
13
|
+
|
|
14
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, unlinkSync } from 'node:fs';
|
|
15
|
+
import { homedir } from 'node:os';
|
|
16
|
+
import { join, dirname, basename } from 'node:path';
|
|
17
|
+
import { SHELLS, FILES } from './commands.js';
|
|
18
|
+
|
|
19
|
+
const BEGIN = '# >>> claude-rpc completion >>>';
|
|
20
|
+
const END = '# <<< claude-rpc completion <<<';
|
|
21
|
+
|
|
22
|
+
export function scriptFor(shell) {
|
|
23
|
+
switch (shell) {
|
|
24
|
+
case 'bash': return `# claude-rpc tab completion (bash)
|
|
25
|
+
_claude_rpc() {
|
|
26
|
+
local cur=\${COMP_WORDS[COMP_CWORD]} line
|
|
27
|
+
local -a words=("\${COMP_WORDS[@]:1:COMP_CWORD}")
|
|
28
|
+
COMPREPLY=()
|
|
29
|
+
while IFS= read -r line; do
|
|
30
|
+
[ "$line" = "${FILES}" ] && { COMPREPLY=($(compgen -f -- "$cur")); return; }
|
|
31
|
+
[ -n "$line" ] && COMPREPLY+=("\${line%%$'\\t'*}")
|
|
32
|
+
done < <(claude-rpc __complete "\${words[@]}" 2>/dev/null)
|
|
33
|
+
}
|
|
34
|
+
complete -o nosort -F _claude_rpc claude-rpc 2>/dev/null || complete -F _claude_rpc claude-rpc
|
|
35
|
+
`;
|
|
36
|
+
case 'zsh': return `#compdef claude-rpc
|
|
37
|
+
# claude-rpc tab completion (zsh)
|
|
38
|
+
_claude_rpc() {
|
|
39
|
+
local -a lines items
|
|
40
|
+
local l
|
|
41
|
+
lines=("\${(@f)$(claude-rpc __complete "\${(@)words[2,CURRENT]}" 2>/dev/null)}")
|
|
42
|
+
if [[ "\${lines[1]}" == "${FILES}" ]]; then _files; return; fi
|
|
43
|
+
for l in $lines; do
|
|
44
|
+
[[ -z $l ]] && continue
|
|
45
|
+
if [[ $l == *$'\\t'* ]]; then items+=("\${\${l%%$'\\t'*}//:/\\\\:}:\${l#*$'\\t'}")
|
|
46
|
+
else items+=("\${l//:/\\\\:}"); fi
|
|
47
|
+
done
|
|
48
|
+
_describe -V 'claude-rpc' items
|
|
49
|
+
}
|
|
50
|
+
if (( ! $+functions[compdef] )); then autoload -Uz compinit && compinit; fi
|
|
51
|
+
compdef _claude_rpc claude-rpc
|
|
52
|
+
`;
|
|
53
|
+
case 'fish': return `# claude-rpc tab completion (fish)
|
|
54
|
+
function __claude_rpc_complete
|
|
55
|
+
set -l tokens (commandline -opc) (commandline -ct)
|
|
56
|
+
set -l out (claude-rpc __complete $tokens[2..-1] 2>/dev/null)
|
|
57
|
+
if test "$out[1]" = "${FILES}"
|
|
58
|
+
__fish_complete_path (commandline -ct)
|
|
59
|
+
else
|
|
60
|
+
printf '%s\\n' $out
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
complete -c claude-rpc -f -k -a '(__claude_rpc_complete)'
|
|
64
|
+
`;
|
|
65
|
+
case 'powershell': return `# claude-rpc tab completion (PowerShell)
|
|
66
|
+
Register-ArgumentCompleter -Native -CommandName claude-rpc, claude-rpc.exe -ScriptBlock {
|
|
67
|
+
param($wordToComplete, $commandAst, $cursorPosition)
|
|
68
|
+
# Words before the cursor; the one being typed goes via env because
|
|
69
|
+
# Windows PowerShell drops empty-string arguments to native commands.
|
|
70
|
+
$words = @($commandAst.CommandElements | Select-Object -Skip 1 |
|
|
71
|
+
Where-Object { $_.Extent.EndOffset -lt $cursorPosition } | ForEach-Object { $_.Extent.Text })
|
|
72
|
+
$env:CLAUDE_RPC_COMP_CUR = $wordToComplete
|
|
73
|
+
$out = @(& claude-rpc __complete @words 2>$null)
|
|
74
|
+
Remove-Item Env:CLAUDE_RPC_COMP_CUR -ErrorAction SilentlyContinue
|
|
75
|
+
if ($out.Count -gt 0 -and $out[0] -eq '${FILES}') { return }
|
|
76
|
+
foreach ($l in $out) {
|
|
77
|
+
if (-not $l) { continue }
|
|
78
|
+
$p = $l -split "\`t", 2
|
|
79
|
+
$tip = if ($p.Count -gt 1) { $p[1] } else { $p[0] }
|
|
80
|
+
[System.Management.Automation.CompletionResult]::new($p[0], $p[0], 'ParameterValue', $tip)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
default: return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// The shell the user is most likely typing in.
|
|
89
|
+
export function detectShell(env = process.env, platform = process.platform) {
|
|
90
|
+
const sh = basename(env.SHELL || '').replace(/\.exe$/i, '');
|
|
91
|
+
if (SHELLS.includes(sh)) return sh;
|
|
92
|
+
if (sh === 'pwsh') return 'powershell';
|
|
93
|
+
if (platform === 'win32') return 'powershell';
|
|
94
|
+
return platform === 'darwin' ? 'zsh' : 'bash';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function completionDir(configDir) {
|
|
98
|
+
return join(configDir, 'completion');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Where each shell's pieces live. `rc` null → no rc edit needed.
|
|
102
|
+
export function targets(shell, { configDir, env = process.env, home = homedir(), profilePath, platform = process.platform } = {}) {
|
|
103
|
+
const dir = completionDir(configDir);
|
|
104
|
+
switch (shell) {
|
|
105
|
+
// macOS Terminal opens login shells, which read .bash_profile, not .bashrc.
|
|
106
|
+
case 'bash': return { script: join(dir, 'claude-rpc.bash'), rc: join(home, platform === 'darwin' ? '.bash_profile' : '.bashrc') };
|
|
107
|
+
case 'zsh': return { script: join(dir, 'claude-rpc.zsh'), rc: join(env.ZDOTDIR || home, '.zshrc') };
|
|
108
|
+
case 'fish': return { script: join(env.XDG_CONFIG_HOME || join(home, '.config'), 'fish', 'completions', 'claude-rpc.fish'), rc: null };
|
|
109
|
+
case 'powershell': return { script: join(dir, 'claude-rpc.ps1'), rc: profilePath || null };
|
|
110
|
+
default: return null;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function sourceLine(shell, script) {
|
|
115
|
+
if (shell === 'powershell') return `if (Test-Path '${script}') { . '${script}' }`;
|
|
116
|
+
return `[ -f '${script}' ] && . '${script}'`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// rc text with our marked block added (idempotent) or removed.
|
|
120
|
+
export function withBlock(text, line) {
|
|
121
|
+
const clean = withoutBlock(text);
|
|
122
|
+
const sep = clean && !clean.endsWith('\n') ? '\n' : '';
|
|
123
|
+
return `${clean}${sep}${BEGIN}\n${line}\n${END}\n`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function withoutBlock(text) {
|
|
127
|
+
const a = text.indexOf(BEGIN);
|
|
128
|
+
if (a === -1) return text;
|
|
129
|
+
const b = text.indexOf(END, a);
|
|
130
|
+
if (b === -1) return text;
|
|
131
|
+
let tail = text.slice(b + END.length);
|
|
132
|
+
if (tail.startsWith('\r\n')) tail = tail.slice(2);
|
|
133
|
+
else if (tail.startsWith('\n')) tail = tail.slice(1);
|
|
134
|
+
return text.slice(0, a) + tail;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// PowerShell's $PROFILE path (CurrentUserCurrentHost), asked from the shell.
|
|
138
|
+
export async function powershellProfile() {
|
|
139
|
+
const { spawnSync } = await import('node:child_process');
|
|
140
|
+
for (const exe of ['pwsh', 'powershell']) {
|
|
141
|
+
try {
|
|
142
|
+
const r = spawnSync(exe, ['-NoProfile', '-NoLogo', '-Command', '$PROFILE'], { encoding: 'utf8', timeout: 8000, windowsHide: true });
|
|
143
|
+
const p = (r.stdout || '').trim();
|
|
144
|
+
if (r.status === 0 && p) return p;
|
|
145
|
+
} catch { /* not installed — try the next one */ }
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function isInstalled(shell, opts) {
|
|
151
|
+
const t = targets(shell, opts);
|
|
152
|
+
if (!t || !existsSync(t.script)) return false;
|
|
153
|
+
if (!t.rc) return true;
|
|
154
|
+
try { return readFileSync(t.rc, 'utf8').includes(BEGIN); } catch { return false; }
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Install for one shell. Returns { script, rc, changed }.
|
|
158
|
+
export function install(shell, opts) {
|
|
159
|
+
const t = targets(shell, opts);
|
|
160
|
+
if (!t) throw new Error(`unknown shell: ${shell}`);
|
|
161
|
+
if (shell === 'powershell' && !t.rc) throw new Error('could not find your PowerShell $PROFILE');
|
|
162
|
+
const body = scriptFor(shell);
|
|
163
|
+
let changed = false;
|
|
164
|
+
const prev = existsSync(t.script) ? readFileSync(t.script, 'utf8') : null;
|
|
165
|
+
if (prev !== body) {
|
|
166
|
+
mkdirSync(dirname(t.script), { recursive: true });
|
|
167
|
+
writeFileSync(t.script, body);
|
|
168
|
+
changed = true;
|
|
169
|
+
}
|
|
170
|
+
if (t.rc) {
|
|
171
|
+
const before = existsSync(t.rc) ? readFileSync(t.rc, 'utf8') : '';
|
|
172
|
+
const after = withBlock(before, sourceLine(shell, t.script));
|
|
173
|
+
if (after !== before) {
|
|
174
|
+
mkdirSync(dirname(t.rc), { recursive: true });
|
|
175
|
+
writeFileSync(t.rc, after);
|
|
176
|
+
changed = true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return { ...t, changed };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Remove from every shell we may have touched. Returns the paths changed.
|
|
183
|
+
export function uninstall(opts) {
|
|
184
|
+
const touched = [];
|
|
185
|
+
for (const shell of SHELLS) {
|
|
186
|
+
const t = targets(shell, opts);
|
|
187
|
+
if (!t) continue;
|
|
188
|
+
if (existsSync(t.script)) { try { unlinkSync(t.script); touched.push(t.script); } catch { /* locked — leave it */ } }
|
|
189
|
+
if (t.rc && existsSync(t.rc)) {
|
|
190
|
+
const before = readFileSync(t.rc, 'utf8');
|
|
191
|
+
const after = withoutBlock(before);
|
|
192
|
+
if (after !== before) { writeFileSync(t.rc, after); touched.push(t.rc); }
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return touched;
|
|
196
|
+
}
|
package/src/doctor.js
CHANGED
|
@@ -414,7 +414,7 @@ function checkAggregate() {
|
|
|
414
414
|
`${agg.sessions || 0} sessions · ${hours}h · refreshed ${ageMin.toFixed(0)} min ago`);
|
|
415
415
|
} catch (e) {
|
|
416
416
|
check('aggregate built', 'fail', `parse error: ${e.message}`,
|
|
417
|
-
'run `claude-rpc
|
|
417
|
+
'run `claude-rpc scan --force` to rebuild the aggregate from scratch', 'rescan');
|
|
418
418
|
}
|
|
419
419
|
}
|
|
420
420
|
|
package/src/help.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// `claude-rpc help` and `claude-rpc help <command>`, both rendered from the
|
|
2
|
+
// command table in commands.js.
|
|
3
|
+
|
|
4
|
+
import { c } from './ui.js';
|
|
5
|
+
import { VERSION } from './version.js';
|
|
6
|
+
import { GROUPS, ALIAS_HINT, findCommand, visibleCommands } from './commands.js';
|
|
7
|
+
|
|
8
|
+
const GROUP_STYLE = {
|
|
9
|
+
start: [c.green, '▸'],
|
|
10
|
+
stats: [c.cyan, '▤'],
|
|
11
|
+
share: [c.magenta, '↗'],
|
|
12
|
+
privacy: [c.yellow, '◐'],
|
|
13
|
+
online: [c.blue, '◎'],
|
|
14
|
+
daemon: [c.green, '●'],
|
|
15
|
+
fix: [c.red, '✚'],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const width = () => Math.max(60, Math.min(process.stdout.columns || 100, 110));
|
|
19
|
+
|
|
20
|
+
// Word-wrap `text` to `w` columns, continuation lines indented by `pad`.
|
|
21
|
+
function wrap(text, w, pad) {
|
|
22
|
+
if (w < 20 || text.length <= w) return text;
|
|
23
|
+
const out = [];
|
|
24
|
+
let line = '';
|
|
25
|
+
for (const word of text.split(' ')) {
|
|
26
|
+
if (line && line.length + 1 + word.length > w) { out.push(line); line = word; }
|
|
27
|
+
else line = line ? `${line} ${word}` : word;
|
|
28
|
+
}
|
|
29
|
+
if (line) out.push(line);
|
|
30
|
+
return out.join(`\n${' '.repeat(pad)}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function banner() {
|
|
34
|
+
const title = `${c.bold}${c.magenta}◆ claude-rpc${c.reset}`;
|
|
35
|
+
const tag = `${c.dim}Discord Rich Presence for Claude Code${c.reset}`;
|
|
36
|
+
const ver = `${c.gray}v${VERSION}${c.reset}`;
|
|
37
|
+
return `\n ${title} ${tag} ${ver}\n`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function renderHelp() {
|
|
41
|
+
const cmds = visibleCommands();
|
|
42
|
+
// Long synopses (recap's date forms) collapse to the bare name; `help <cmd>` has them.
|
|
43
|
+
const MAX = 24;
|
|
44
|
+
const label = (cmd) => {
|
|
45
|
+
const full = cmd.name + (cmd.usage ? ` ${cmd.usage}` : '');
|
|
46
|
+
return full.length > MAX ? cmd.name : full;
|
|
47
|
+
};
|
|
48
|
+
const colW = cmds.reduce((m, cmd) => Math.max(m, label(cmd).length), 0);
|
|
49
|
+
const descW = width() - colW - 8;
|
|
50
|
+
const lines = [banner()];
|
|
51
|
+
|
|
52
|
+
lines.push(` ${c.dim}Usage:${c.reset} ${c.bold}claude-rpc${c.reset} ${c.cyan}<command>${c.reset} ${c.dim}[options]${c.reset}\n`);
|
|
53
|
+
|
|
54
|
+
for (const [key, title] of GROUPS) {
|
|
55
|
+
const [color, icon] = GROUP_STYLE[key] || [c.cyan, '·'];
|
|
56
|
+
const inGroup = cmds.filter((cmd) => cmd.group === key);
|
|
57
|
+
if (!inGroup.length) continue;
|
|
58
|
+
lines.push(` ${color}${icon}${c.reset} ${c.bold}${title}${c.reset}`);
|
|
59
|
+
for (const cmd of inGroup) {
|
|
60
|
+
const shown = label(cmd);
|
|
61
|
+
const usage = shown !== cmd.name ? `${c.dim}${cmd.usage}${c.reset}` : '';
|
|
62
|
+
const head = `${c.cyan}${c.bold}${cmd.name}${c.reset}${usage ? ` ${usage}` : ''}`;
|
|
63
|
+
const pad = ' '.repeat(colW - shown.length + 2);
|
|
64
|
+
lines.push(` ${head}${pad}${wrap(cmd.summary, descW, colW + 6)}`);
|
|
65
|
+
}
|
|
66
|
+
lines.push('');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const tips = [
|
|
70
|
+
['one command in detail', 'claude-rpc help <command>'],
|
|
71
|
+
['tab completion', 'claude-rpc completion install'],
|
|
72
|
+
['something off?', 'claude-rpc doctor --fix'],
|
|
73
|
+
['docs', 'https://claude-rpc.com/docs'],
|
|
74
|
+
];
|
|
75
|
+
const tipW = tips.reduce((m, [k]) => Math.max(m, k.length), 0);
|
|
76
|
+
for (const [k, v] of tips) lines.push(` ${c.gray}${k.padEnd(tipW)}${c.reset} ${c.cyan}${v}${c.reset}`);
|
|
77
|
+
lines.push('');
|
|
78
|
+
lines.push(` ${c.gray}exit codes: 0 ok · 1 usage error · 2 system error · 3 wrong state${c.reset}`);
|
|
79
|
+
lines.push('');
|
|
80
|
+
return lines.join('\n');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function renderCommandHelp(name) {
|
|
84
|
+
const cmd = findCommand(name);
|
|
85
|
+
if (!cmd) return null;
|
|
86
|
+
const lines = [''];
|
|
87
|
+
const viaAlias = cmd.name !== name ? ` ${c.dim}(\`${name}\` still works — it's \`claude-rpc ${ALIAS_HINT[name] || cmd.name}\` now)${c.reset}` : '';
|
|
88
|
+
lines.push(` ${c.bold}${c.magenta}◆ claude-rpc ${cmd.name}${c.reset}${viaAlias}`);
|
|
89
|
+
lines.push(` ${cmd.summary}`);
|
|
90
|
+
lines.push('');
|
|
91
|
+
lines.push(` ${c.dim}Usage${c.reset}`);
|
|
92
|
+
lines.push(` ${c.bold}claude-rpc${c.reset} ${c.cyan}${cmd.name}${c.reset}${cmd.usage ? ` ${cmd.usage}` : ''}${cmd.flags?.length ? ` ${c.dim}[options]${c.reset}` : ''}`);
|
|
93
|
+
lines.push('');
|
|
94
|
+
|
|
95
|
+
const table = (title, rows) => {
|
|
96
|
+
if (!rows.length) return;
|
|
97
|
+
const w = rows.reduce((m, [k]) => Math.max(m, k.length), 0);
|
|
98
|
+
lines.push(` ${c.dim}${title}${c.reset}`);
|
|
99
|
+
for (const [k, d] of rows) lines.push(` ${c.cyan}${k.padEnd(w)}${c.reset} ${d}`);
|
|
100
|
+
lines.push('');
|
|
101
|
+
};
|
|
102
|
+
table('Choices', (cmd.subs || []).map(([s, d]) => [s, d]));
|
|
103
|
+
table('Options', (cmd.flags || []).map(([f, d, v]) => {
|
|
104
|
+
const val = Array.isArray(v) ? ` <${v.join('|')}>` : v === 'file' ? ' [file]' : v ? ' <value>' : '';
|
|
105
|
+
return [f + val, d];
|
|
106
|
+
}));
|
|
107
|
+
if (cmd.examples?.length) {
|
|
108
|
+
lines.push(` ${c.dim}Examples${c.reset}`);
|
|
109
|
+
for (const ex of cmd.examples) lines.push(` ${c.gray}$${c.reset} ${ex}`);
|
|
110
|
+
lines.push('');
|
|
111
|
+
}
|
|
112
|
+
if (cmd.aliases?.length) {
|
|
113
|
+
lines.push(` ${c.gray}also answers to: ${cmd.aliases.join(', ')}${c.reset}`);
|
|
114
|
+
lines.push('');
|
|
115
|
+
}
|
|
116
|
+
return lines.join('\n');
|
|
117
|
+
}
|
package/src/nudge.js
CHANGED
|
@@ -46,7 +46,7 @@ export function pickShareNudge(agg, shown = new Set()) {
|
|
|
46
46
|
const m = reached(streak, [3, 7, 14, 30, 50, 100, 200, 365]);
|
|
47
47
|
if (m) out.push({
|
|
48
48
|
key: `streak:${m}`, weight: 1000 + m,
|
|
49
|
-
message: `${m}-day streak — a personal record. Drop a live badge in your README: \`claude-rpc badge --metric streak --
|
|
49
|
+
message: `${m}-day streak — a personal record. Drop a live badge in your README: \`claude-rpc statcards badge --metric streak --link\``,
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
52
|
|
|
@@ -54,7 +54,7 @@ export function pickShareNudge(agg, shown = new Set()) {
|
|
|
54
54
|
const s = reached(sessions, [50, 100, 250, 500, 1000, 2500, 5000, 10000]);
|
|
55
55
|
if (s) out.push({
|
|
56
56
|
key: `sessions:${s}`, weight: s / 50,
|
|
57
|
-
message: `${fmt(s)} Claude Code sessions logged. Show it off: \`claude-rpc
|
|
57
|
+
message: `${fmt(s)} Claude Code sessions logged. Show it off: \`claude-rpc statcards poster --range all --png\`.`,
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
const hours = Math.floor((agg.activeMs || 0) / 3_600_000);
|