jpeek 1.0.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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -0
  3. package/index.js +279 -0
  4. package/package.json +27 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 bhusan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # jpeek
2
+
3
+ Interactive fold/unfold JSON tree viewer for the terminal. Zero deps.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install jpeek
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ const { jpeek } = require('jpeek');
15
+
16
+ jpeek.interact('my data', { hello: 'world', list: [1, 2, 3] });
17
+ ```
18
+
19
+ `jpeek.interact(name, data)` opens an interactive tree in the terminal. Blocks
20
+ synchronously until you quit — no `await` needed.
21
+
22
+ - `↑`/`↓` (or `j`/`k`) — move
23
+ - `→`/`enter` — expand
24
+ - `←` — collapse / go to parent
25
+ - `space` — toggle
26
+ - `q` / `esc` / `ctrl-c` — quit
27
+
28
+ `jpeek.render(name, data)` prints a static, fully expanded tree instead —
29
+ useful for one-shot dumps. Press `q` to quit.
30
+
31
+ ## License
32
+
33
+ MIT
package/index.js ADDED
@@ -0,0 +1,279 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+
5
+ // ---- tiny ANSI palette (no deps) ----
6
+ const C = {
7
+ reset: '\x1b[0m',
8
+ dim: '\x1b[2m',
9
+ cyan: '\x1b[36m',
10
+ green: '\x1b[32m',
11
+ yellow: '\x1b[33m',
12
+ magenta: '\x1b[35m',
13
+ gray: '\x1b[90m',
14
+ invert: '\x1b[7m',
15
+ };
16
+
17
+ const INTERACT_HINT = 'Up arrow/Down arrow navigate · Right arrow/enter expand · Left arrow collapse · space toggle · q quit';
18
+ const RENDER_HINT = 'q to quit';
19
+
20
+ function typeOf(v) {
21
+ if (v === null) return 'null';
22
+ if (Array.isArray(v)) return 'array';
23
+ return typeof v; // object | string | number | boolean | undefined
24
+ }
25
+
26
+ function isContainer(v) {
27
+ const t = typeOf(v);
28
+ return t === 'object' || t === 'array';
29
+ }
30
+
31
+ function scalarText(v) {
32
+ const t = typeOf(v);
33
+ if (t === 'string') return JSON.stringify(v);
34
+ if (t === 'null') return 'null';
35
+ if (t === 'undefined') return 'undefined';
36
+ return String(v);
37
+ }
38
+
39
+ function scalarColored(v) {
40
+ const t = typeOf(v);
41
+ if (t === 'string') return C.green + JSON.stringify(v) + C.reset;
42
+ if (t === 'number') return C.yellow + v + C.reset;
43
+ if (t === 'boolean') return C.magenta + v + C.reset;
44
+ return C.gray + scalarText(v) + C.reset;
45
+ }
46
+
47
+ /**
48
+ * jpeek.interact(name, data)
49
+ * Opens an interactive fold/unfold tree in the terminal.
50
+ * Blocks the calling thread synchronously until the user quits (q / esc / ctrl-c) —
51
+ * no Promise, no await needed; call it as a plain statement.
52
+ * Keys: ↑/↓ (or j/k) move · →/enter expand · ← collapse/parent · space toggle · q quit
53
+ */
54
+ function interact(name, data) {
55
+ const expanded = new Set(['$']); // root open, everything else collapsed
56
+ let cursor = 0;
57
+ let top = 0;
58
+
59
+ // Flatten current tree into visible rows based on `expanded`.
60
+ function build() {
61
+ const rows = [];
62
+ (function walk(value, key, path, depth) {
63
+ const container = isContainer(value);
64
+ const isOpen = expanded.has(path);
65
+ rows.push({ key, value, path, depth, container, isOpen });
66
+ if (container && isOpen) {
67
+ if (typeOf(value) === 'array') {
68
+ value.forEach((item, i) => walk(item, i, path + '.' + i, depth + 1));
69
+ } else {
70
+ Object.keys(value).forEach((k) => walk(value[k], k, path + '.' + k, depth + 1));
71
+ }
72
+ }
73
+ })(data, '$', '$', 0);
74
+ return rows;
75
+ }
76
+
77
+ function summaryText(value) {
78
+ return typeOf(value) === 'array'
79
+ ? `Array(${value.length})`
80
+ : `{${Object.keys(value).length}}`;
81
+ }
82
+
83
+ function renderRow(row, highlighted) {
84
+ const indent = ' '.repeat(row.depth);
85
+ const marker = row.container ? (row.isOpen ? '▼ ' : '▶ ') : ' ';
86
+ const keyText = row.key === '$' ? '' : row.key + ': ';
87
+ const valText = row.container ? summaryText(row.value) : scalarText(row.value);
88
+
89
+ if (highlighted) {
90
+ // Plain text only — embedded color resets would break the invert.
91
+ return C.invert + indent + marker + keyText + valText + C.reset;
92
+ }
93
+ const cKey = row.key === '$' ? '' : C.cyan + row.key + C.reset + ': ';
94
+ const cVal = row.container ? C.dim + valText + C.reset : scalarColored(row.value);
95
+ return indent + marker + cKey + cVal;
96
+ }
97
+
98
+ function render() {
99
+ const rows = build();
100
+ const headerLines = name ? 1 : 0;
101
+ const footerLines = 1;
102
+ const height = (process.stdout.rows || 24) - 1 - headerLines - footerLines;
103
+ if (cursor < 0) cursor = 0;
104
+ if (cursor > rows.length - 1) cursor = rows.length - 1;
105
+ if (cursor < top) top = cursor;
106
+ if (cursor >= top + height) top = cursor - height + 1;
107
+
108
+ let out = '\x1b[2J\x1b[H'; // clear + cursor home
109
+ if (name) out += C.invert + ' ' + name + ' ' + C.reset + '\n';
110
+ rows.slice(top, top + height).forEach((row, i) => {
111
+ out += renderRow(row, top + i === cursor) + '\n';
112
+ });
113
+ out += '\n' + C.gray + INTERACT_HINT + C.reset + '\n';
114
+ process.stdout.write(out);
115
+ }
116
+
117
+ function cleanup() {
118
+ process.stdout.write('\x1b[?25h'); // show cursor
119
+ process.stdout.write('\x1b[?1049l'); // leave alternate screen
120
+ if (process.stdin.isTTY) process.stdin.setRawMode(false);
121
+ }
122
+
123
+ // input -> one of: 'up' 'down' 'left' 'right' 'space' 'enter' 'quit' 'other'
124
+ function decode(chunk) {
125
+ if (chunk.includes('q') || chunk.includes('\x03') || chunk === '\x1b') return 'quit';
126
+ if (chunk === '\x1b[A' || chunk === 'k') return 'up';
127
+ if (chunk === '\x1b[B' || chunk === 'j') return 'down';
128
+ if (chunk === '\x1b[C' || chunk === 'l') return 'right';
129
+ if (chunk === '\x1b[D' || chunk === 'h') return 'left';
130
+ if (chunk === ' ') return 'space';
131
+ if (chunk === '\r' || chunk === '\n') return 'enter';
132
+ return 'other';
133
+ }
134
+
135
+ function onKey(action) {
136
+ const rows = build();
137
+ const node = rows[cursor];
138
+
139
+ if (action === 'quit') return true;
140
+ if (action === 'up') cursor--;
141
+ else if (action === 'down') cursor++;
142
+ else if (action === 'right' || action === 'enter') {
143
+ if (node.container && !node.isOpen) expanded.add(node.path);
144
+ else if (node.container && node.isOpen) cursor++; // step into first child
145
+ } else if (action === 'space') {
146
+ if (node.container) {
147
+ node.isOpen ? expanded.delete(node.path) : expanded.add(node.path);
148
+ }
149
+ } else if (action === 'left') {
150
+ if (node.container && node.isOpen) {
151
+ expanded.delete(node.path);
152
+ } else {
153
+ const parent = node.path.split('.').slice(0, -1).join('.');
154
+ const idx = rows.findIndex((r) => r.path === parent);
155
+ if (idx >= 0) cursor = idx;
156
+ }
157
+ }
158
+ render();
159
+ return false;
160
+ }
161
+
162
+ if (process.stdin.isTTY) process.stdin.setRawMode(true);
163
+ process.stdout.write('\x1b[?1049h'); // alternate screen buffer
164
+ process.stdout.write('\x1b[?25l'); // hide cursor
165
+ render();
166
+
167
+ // Synchronous blocking read loop — no event loop, no Promise, no await needed.
168
+ const buf = Buffer.alloc(16);
169
+ let quit = false;
170
+ while (!quit) {
171
+ let n;
172
+ try {
173
+ n = fs.readSync(0, buf, 0, 16, null);
174
+ } catch (err) {
175
+ if (err.code === 'EAGAIN') continue;
176
+ if (err.code === 'EOF') break;
177
+ throw err;
178
+ }
179
+ if (n === 0) break; // EOF (e.g. piped stdin exhausted)
180
+ if (n < 0) continue;
181
+ quit = onKey(decode(buf.toString('utf8', 0, n)));
182
+ }
183
+
184
+ cleanup();
185
+ }
186
+
187
+ // Blocks synchronously (fs.readSync) until q / esc / ctrl-c. No Promise, no await.
188
+ function waitForQuit() {
189
+ const buf = Buffer.alloc(16);
190
+ for (;;) {
191
+ let n;
192
+ try {
193
+ n = fs.readSync(0, buf, 0, 16, null);
194
+ } catch (err) {
195
+ if (err.code === 'EAGAIN') continue;
196
+ if (err.code === 'EOF') return;
197
+ throw err;
198
+ }
199
+ if (n === 0) return; // EOF (e.g. piped stdin exhausted)
200
+ if (n < 0) continue;
201
+ const chunk = buf.toString('utf8', 0, n);
202
+ if (chunk.includes('q') || chunk.includes('\x03') || chunk === '\x1b') return;
203
+ }
204
+ }
205
+
206
+ /**
207
+ * jpeek.render(name, data)
208
+ * Opens the alternate screen buffer and prints the full (already expanded)
209
+ * tree — no navigation, just a static view. Blocks (fs.readSync, no await
210
+ * needed) until q / esc / ctrl-c, then leaves the alt screen and returns,
211
+ * so the next jpeek call runs right after.
212
+ */
213
+ function render(name, data) {
214
+ function summaryText(value) {
215
+ return typeOf(value) === 'array'
216
+ ? `Array(${value.length})`
217
+ : `{${Object.keys(value).length}}`;
218
+ }
219
+
220
+ function walk(value, key, depth, lines) {
221
+ const container = isContainer(value);
222
+ const indent = ' '.repeat(depth);
223
+ const keyText = key === '$' ? '' : C.cyan + key + C.reset + ': ';
224
+ const marker = container ? '▼ ' : ' ';
225
+
226
+ if (!container) {
227
+ lines.push(indent + marker + keyText + scalarColored(value));
228
+ return;
229
+ }
230
+ lines.push(indent + marker + keyText + C.dim + summaryText(value) + C.reset);
231
+ if (typeOf(value) === 'array') {
232
+ value.forEach((item, i) => walk(item, i, depth + 1, lines));
233
+ } else {
234
+ Object.keys(value).forEach((k) => walk(value[k], k, depth + 1, lines));
235
+ }
236
+ }
237
+
238
+ const lines = [];
239
+ walk(data, '$', 0, lines);
240
+
241
+ if (process.stdin.isTTY) process.stdin.setRawMode(true);
242
+ process.stdout.write('\x1b[?1049h'); // alternate screen buffer
243
+ process.stdout.write('\x1b[?25l'); // hide cursor
244
+ let out = '\x1b[2J\x1b[H';
245
+ if (name) out += C.invert + ' ' + name + ' ' + C.reset + '\n';
246
+ out += lines.join('\n') + '\n';
247
+ out += '\n' + C.gray + RENDER_HINT + C.reset + '\n';
248
+ process.stdout.write(out);
249
+
250
+ waitForQuit();
251
+
252
+ process.stdout.write('\x1b[?25h'); // show cursor
253
+ process.stdout.write('\x1b[?1049l'); // leave alternate screen
254
+ if (process.stdin.isTTY) process.stdin.setRawMode(false);
255
+ }
256
+
257
+ const jpeek = { interact, render };
258
+ module.exports = { jpeek };
259
+
260
+ // Demo when run directly: `node jpeek.js`
261
+ if (require.main === module) {
262
+ const sample = {
263
+ id: 42,
264
+ name: 'PortPro carrier',
265
+ active: true,
266
+ scac: null,
267
+ terminals: ['LAX', 'LGB', 'OAK'],
268
+ contact: { email: 'ops@example.com', phones: ['555-0100', '555-0101'] },
269
+ loads: [
270
+ { ref: 'L-001', finalAmount: 1250.5, stops: 2 },
271
+ { ref: 'L-002', finalAmount: 980, stops: 3 },
272
+ ],
273
+ };
274
+ jpeek.interact('sample', sample);
275
+ process.exit(0);
276
+ }
277
+
278
+
279
+
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "jpeek",
3
+ "version": "1.0.0",
4
+ "description": "Interactive fold/unfold JSON tree viewer for the terminal — zero deps.",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js"
8
+ ],
9
+ "scripts": {
10
+ "start": "node index.js",
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "keywords": [
14
+ "json",
15
+ "terminal",
16
+ "cli",
17
+ "tree",
18
+ "debug",
19
+ "inspect",
20
+ "viewer"
21
+ ],
22
+ "author": "bhusandotel <bhusandotel1@gmail.com>",
23
+ "license": "MIT",
24
+ "engines": {
25
+ "node": ">=14"
26
+ }
27
+ }