-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstatusline.cjs
More file actions
81 lines (63 loc) · 1.9 KB
/
statusline.cjs
File metadata and controls
81 lines (63 loc) · 1.9 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
#!/usr/bin/env node
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
function getInstallRoot() {
return path.join(os.homedir(), '.claude', 'multi-account-switch');
}
function getStatuslineTargetPath() {
return path.join(getInstallRoot(), 'hooks', 'statusline-target.json');
}
function readJsonIfExists(filePath, fallback = null) {
if (!fs.existsSync(filePath)) return fallback;
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
function getStatuslineLabel() {
return 'use !ccs';
}
function isStaleDownstreamTarget(target) {
if (!target || target.type !== 'command' || !target.command) {
return true;
}
const command = String(target.command).toLowerCase();
if (command.includes('tabber') || command.includes('omc-hud')) {
return true;
}
if ((command.endsWith('.py') || command.includes('.py"') || command.includes(".py'")) && !command.includes('python')) {
return true;
}
return false;
}
function runDownstream(input) {
const target = readJsonIfExists(getStatuslineTargetPath(), null);
if (isStaleDownstreamTarget(target)) {
return '';
}
const result = spawnSync(target.command, {
input,
encoding: 'utf8',
shell: true,
windowsHide: true
});
if (result.error || result.status !== 0) {
return '';
}
const output = (result.stdout || '').trimEnd();
if (output.includes('Plugin not installed') || output.includes('[OMC HUD]')) {
return '';
}
return output;
}
function prependLabel(label, downstream) {
if (!downstream) {
return label;
}
const lines = downstream.split(/\r?\n/);
lines[0] = lines[0] ? `${label} │ ${lines[0]}` : label;
return lines.join('\n');
}
const stdin = fs.readFileSync(0, 'utf8');
const label = getStatuslineLabel();
const downstream = runDownstream(stdin);
process.stdout.write(`${prependLabel(label, downstream)}\n`);