-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·210 lines (169 loc) · 6.8 KB
/
Copy pathindex.js
File metadata and controls
executable file
·210 lines (169 loc) · 6.8 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
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env node
'use strict';
const PHASES = ['inhale', 'hold', 'exhale', 'hold'];
const PHASE_CODES = [36, 33, 35, 33]; // cyan, yellow, magenta, yellow
const BOX_W = 26; // total width including borders
const BOX_H = 11; // total height including borders (odd = clean center)
const CENTER_ROW = Math.floor(BOX_H / 2);
const TICK_MS = 50;
const INDENT = ' ';
// ── CLI args ──────────────────────────────────────────────────────────────────
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log([
'',
' tmango 🥭 — box breathing in your terminal',
'',
' Usage: tmango [options]',
'',
' Options:',
' -t, --time <seconds> Interval per phase in seconds (default: 4)',
' -h, --help Show this help message',
'',
' Controls:',
' space start / stop',
' q quit',
'',
].join('\n'));
process.exit(0);
}
const timeIdx = args.findIndex(a => a === '--time' || a === '-t');
const parsedTime = timeIdx !== -1 ? parseInt(args[timeIdx + 1], 10) : NaN;
const INTERVAL_SEC = (!isNaN(parsedTime) && parsedTime > 0) ? parsedTime : 4;
let running = false;
let phase = 0;
let phaseMs = 0;
let elapsedMs = 0;
let ticker = null;
// ── ANSI helpers ─────────────────────────────────────────────────────────────
const hide = '\x1b[?25l';
const show = '\x1b[?25h';
function c(code, s) { return `\x1b[${code}m${s}\x1b[0m`; }
function phaseColor(ph, s) { return c(PHASE_CODES[ph], s); }
function phaseBold(ph, s) { return c(`1;${PHASE_CODES[ph]}`, s); }
// ── Box rendering ─────────────────────────────────────────────────────────────
function getDotPos(ph, progress) {
const p = Math.max(0, Math.min(1, progress));
if (ph === 0) return { row: 0, col: Math.round(p * (BOX_W - 1)) };
if (ph === 1) return { row: Math.round(p * (BOX_H - 1)), col: BOX_W - 1 };
if (ph === 2) return { row: BOX_H - 1, col: Math.round((1 - p) * (BOX_W - 1)) };
/* ph === 3 */ return { row: Math.round((1 - p) * (BOX_H - 1)), col: 0 };
}
function renderBox() {
const progress = running ? Math.min(phaseMs / (INTERVAL_SEC * 1000), 1) : 0;
const dp = running ? getDotPos(phase, progress) : null;
const inner = BOX_W - 2;
const lines = [];
for (let r = 0; r < BOX_H; r++) {
const isTop = r === 0, isBot = r === BOX_H - 1;
const hasDot = dp && dp.row === r;
const dc = hasDot ? dp.col : -1;
const dot = hasDot ? phaseColor(phase, '●') : null;
let line;
if (isTop || isBot) {
const L = isTop ? '╭' : '╰';
const R = isTop ? '╮' : '╯';
if (!hasDot) {
line = L + '─'.repeat(inner) + R;
} else if (dc === 0) {
line = dot + '─'.repeat(inner) + R;
} else if (dc === BOX_W - 1) {
line = L + '─'.repeat(inner) + dot;
} else {
line = L + '─'.repeat(dc - 1) + dot + '─'.repeat(inner - dc) + R;
}
} else {
const L = (hasDot && dc === 0) ? dot : '│';
const R = (hasDot && dc === BOX_W - 1) ? dot : '│';
let content;
if (r === CENTER_ROW) {
if (running) {
const label = PHASES[phase].toUpperCase();
const pad = Math.floor((inner - label.length) / 2);
const padR = inner - pad - label.length;
content = ' '.repeat(pad) + phaseBold(phase, label) + ' '.repeat(padR);
} else {
const label = 'space to start';
const pad = Math.floor((inner - label.length) / 2);
const padR = inner - pad - label.length;
content = ' '.repeat(pad) + c('2', label) + ' '.repeat(padR);
}
} else {
content = ' '.repeat(inner);
}
line = L + content + R;
}
lines.push(INDENT + line);
}
return lines.join('\n');
}
// ── Status bar ────────────────────────────────────────────────────────────────
function formatTime(ms) {
const totalSec = Math.floor(ms / 1000);
const m = Math.floor(totalSec / 60);
const s = totalSec % 60;
return `${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
}
function render() {
let out = '\x1b[2J\x1b[H\n';
out += INDENT + c('1', 'tmango') + ' 🥭\n\n';
out += renderBox();
out += '\n\n';
if (running) {
out += INDENT + ` ${phaseBold(phase, PHASES[phase])} · elapsed ${formatTime(elapsedMs)}`;
} else {
out += INDENT + c('2', ' press space to start');
}
out += '\n\n';
out += INDENT + c('2', ' [space] start/stop [q] quit');
out += '\n';
process.stdout.write(out);
}
// ── Timer loop ────────────────────────────────────────────────────────────────
function tick() {
phaseMs += TICK_MS;
elapsedMs += TICK_MS;
if (phaseMs >= INTERVAL_SEC * 1000) {
phaseMs -= INTERVAL_SEC * 1000;
phase = (phase + 1) % 4;
}
render();
}
function start() {
if (ticker) return;
running = true;
phase = 0;
phaseMs = 0;
elapsedMs = 0;
ticker = setInterval(tick, TICK_MS);
render();
}
function stop() {
if (ticker) { clearInterval(ticker); ticker = null; }
running = false;
elapsedMs = 0;
render();
}
// ── Keyboard input ────────────────────────────────────────────────────────────
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', (key) => {
if (key === 'q' || key === '\u0003') { // q or Ctrl-C
cleanup();
process.exit(0);
}
if (key === ' ') {
running ? stop() : start();
}
});
// ── Cleanup ───────────────────────────────────────────────────────────────────
function cleanup() {
if (ticker) clearInterval(ticker);
process.stdout.write(show + '\x1b[2J\x1b[H');
}
process.on('exit', () => process.stdout.write(show));
process.on('SIGTERM', () => { cleanup(); process.exit(0); });
// ── Init ──────────────────────────────────────────────────────────────────────
process.stdout.write(hide);
render();