jsonl-cli 0.1.2__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Taehoon Hwang
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.
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: jsonl-cli
3
+ Version: 0.1.2
4
+ Summary: CLI JSONL viewer
5
+ Requires-Python: >=3.9
6
+ License-File: LICENSE
7
+ Dynamic: license-file
@@ -0,0 +1,97 @@
1
+ # jsonl-viewer
2
+
3
+ A CLI tool to manually view the rows of a JSON lines file.
4
+
5
+ ![HellaSwag JSONL Sample](./resources/hellaswag_sample.png)
6
+ Sample JSONL entry fron the HellaSwag dataset.
7
+
8
+ ## Requirements
9
+
10
+ `curses` and Python (>= 3.9) must be installed.
11
+
12
+ ## Installation
13
+
14
+ Run the following to install:
15
+ ```
16
+ $ brew tap looooonk/tap
17
+ $ brew install jsonl-cli
18
+ ```
19
+
20
+ Alternatively, after cloning the repository, run the following installation command:
21
+
22
+ ```
23
+ $ git clone https://github.com/looooonk/jsonl-viewer.git
24
+ $ cd jsonl-viewer
25
+ $ python -m pip install -e .
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ To view the contents of a JSON lines file, use
31
+
32
+ ```
33
+ jsonl PATH_TO_FILE
34
+ ```
35
+
36
+ To view a summary of a JSON lines file, use
37
+
38
+ ```
39
+ jsonl PATH_TO_FILE -b
40
+ ```
41
+
42
+ To view more information, use
43
+
44
+ ```
45
+ jsonl -h
46
+ ```
47
+
48
+ ## Themes
49
+
50
+ You can specify a certain color theme using the `-t` argument.
51
+
52
+ Currently, 4 themes are supported:
53
+ - `catppuccin-frappe`
54
+ - `catppuccin-latte`
55
+ - `catppuccin-macchiato`
56
+ - `catppuccin-mocha`
57
+
58
+ If not specified, the theme will default to `catppuccin-mocha`.
59
+
60
+ You may add additional themes at `./json_cli/themes/`.
61
+
62
+ ## Commands
63
+ For easy navigation, you can press `:` in the curses window to enter a command, much like vim.
64
+
65
+ Currently, the following commands are supported:
66
+
67
+ - Jump to certain line number (1-indexed):
68
+ ```
69
+ :goto INT
70
+ ```
71
+ - Search all fields in parsed JSON rows:
72
+ ```
73
+ /TEXT
74
+ :find TEXT
75
+ ```
76
+ - Search only specific fields, including nested fields:
77
+ ```
78
+ :find FIELD[,FIELD...]: TEXT
79
+ :find user.name,items[].title: alice
80
+ ```
81
+ - Repeat the last search:
82
+ ```
83
+ n
84
+ N
85
+ :next
86
+ :prev
87
+ ```
88
+ - Search options:
89
+ ```
90
+ :find -c TEXT
91
+ :find -r FIELD: REGEX
92
+ ```
93
+
94
+ Field paths use `.` for nested objects, numeric list indexes such as `items.0.name`,
95
+ `[]` for any list item, and `*` for any object value or list item. Search is
96
+ case-insensitive by default; `-c` makes it case-sensitive and `-r` treats the
97
+ query as a regular expression.
File without changes
@@ -0,0 +1,4 @@
1
+ from .cli import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
@@ -0,0 +1,247 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import curses
5
+ import os
6
+ from typing import List, Optional
7
+
8
+ from .colors import _hex_to_rgb, _key_to_pair_id, _rgb_to_xterm256, _load_theme
9
+ from .render import _render_json_styled, _wrap_styled_lines
10
+ from .helpers import _validate_path, _build_offsets, _human_bytes, \
11
+ _read_line_at, _scan_brief, _parse_row, _die
12
+ from .command import _apply_command, _prompt_command
13
+ from .search import SearchSpec, _find_next, _parse_search_spec
14
+
15
+
16
+ def _viewer(stdscr: "curses._CursesWindow", path: str, theme: str) -> None:
17
+ """
18
+ Main driver code for the curses window.
19
+
20
+ Args:
21
+ stdscr: The curses window to display to.
22
+ path: Path to the JSONL file.
23
+ """
24
+ curses.curs_set(0)
25
+
26
+ status_msg: str | None = None
27
+
28
+ curses.start_color()
29
+ try:
30
+ curses.use_default_colors()
31
+ except curses.error:
32
+ pass
33
+
34
+ color_theme = _load_theme(theme)
35
+
36
+ num_pairs = len(color_theme)
37
+ for i, hx in enumerate(color_theme, start=1):
38
+ r, g, b = _hex_to_rgb(hx)
39
+ fg = _rgb_to_xterm256(r, g, b)
40
+ try:
41
+ curses.init_pair(i, fg, -1)
42
+ except curses.error:
43
+ pass
44
+
45
+ def key_attr_fn(key: str) -> int:
46
+ pid = _key_to_pair_id(key, num_pairs)
47
+ return curses.A_BOLD | curses.color_pair(pid)
48
+
49
+ normal_attr = curses.A_NORMAL
50
+
51
+ stdscr.keypad(True)
52
+
53
+ offsets = _build_offsets(path)
54
+ total_lines = max(0, len(offsets) - 1)
55
+
56
+ idx = 0
57
+ scroll = 0
58
+ indent_delta = 4
59
+ last_search: SearchSpec | None = None
60
+
61
+ def apply_search(raw: str, include_current: bool = False, direction: int = 1) -> None:
62
+ nonlocal idx, scroll, status_msg, last_search
63
+ spec, err = _parse_search_spec(raw)
64
+ if err:
65
+ status_msg = err
66
+ return
67
+
68
+ hit = _find_next(path, offsets, total_lines, idx, spec, direction, include_current)
69
+ last_search = spec
70
+ if hit is None:
71
+ status_msg = f"no match: {spec.label()}"
72
+ return
73
+
74
+ idx = hit.idx
75
+ scroll = 0
76
+ prefix = "wrapped to " if hit.wrapped else ""
77
+ status_msg = f"{prefix}row {idx + 1}: {spec.label()}"
78
+
79
+ def repeat_search(direction: int) -> None:
80
+ nonlocal idx, scroll, status_msg
81
+ if last_search is None:
82
+ status_msg = "no previous search"
83
+ return
84
+
85
+ hit = _find_next(path, offsets, total_lines, idx, last_search, direction, False)
86
+ if hit is None:
87
+ status_msg = f"no match: {last_search.label()}"
88
+ return
89
+
90
+ idx = hit.idx
91
+ scroll = 0
92
+ prefix = "wrapped to " if hit.wrapped else ""
93
+ status_msg = f"{prefix}row {idx + 1}: {last_search.label()}"
94
+
95
+ while True:
96
+ height, width = stdscr.getmaxyx()
97
+ stdscr.erase()
98
+
99
+ if total_lines == 0:
100
+ stdscr.addnstr(0, 0, "Empty file. Press q to quit.", width - 1)
101
+ stdscr.refresh()
102
+ ch = stdscr.getch()
103
+ if ch in (ord("q"), ord("Q")):
104
+ return
105
+ continue
106
+
107
+ if idx < 0:
108
+ idx = 0
109
+ if idx >= total_lines:
110
+ idx = total_lines - 1
111
+
112
+ start = offsets[idx]
113
+ end = offsets[idx + 1]
114
+ raw = _read_line_at(path, start, end)
115
+ row = _parse_row(raw, idx)
116
+
117
+ header = f"{os.path.basename(path)} | {idx + 1}/{total_lines} | ↑/↓ scroll ←/→ row / find n/N next/prev q quit"
118
+ stdscr.addnstr(0, 0, header, max(0, width - 1), curses.A_REVERSE)
119
+
120
+ title = row.title
121
+ stdscr.addnstr(1, 0, title, max(0, width - 1), curses.A_BOLD)
122
+
123
+ content_height = max(0, height - 3)
124
+ content_width = max(1, width - 1)
125
+
126
+ if row.ok:
127
+ styled_lines = _render_json_styled(row.obj, 0, key_attr_fn, normal_attr, indent_delta)
128
+ else:
129
+ raw = row.raw_fallback or ""
130
+ styled_lines = [[(raw, curses.A_DIM)]]
131
+
132
+ styled_lines = _wrap_styled_lines(styled_lines, content_width)
133
+
134
+ max_scroll = max(0, len(styled_lines) - content_height)
135
+ if scroll > max_scroll:
136
+ scroll = max_scroll
137
+ if scroll < 0:
138
+ scroll = 0
139
+
140
+ view = styled_lines[scroll : scroll + content_height]
141
+
142
+ for i, line in enumerate(view):
143
+ y = 2 + i
144
+ x = 0
145
+ remaining = content_width
146
+ for text, attr in line:
147
+ if remaining <= 0:
148
+ break
149
+ if not text:
150
+ continue
151
+ chunk = text[:remaining]
152
+ try:
153
+ stdscr.addstr(y, x, chunk, attr)
154
+ except curses.error:
155
+ pass
156
+ x += len(chunk)
157
+ remaining -= len(chunk)
158
+
159
+ footer = status_msg
160
+ if footer is None and max_scroll > 0:
161
+ footer = f"Lines {scroll} - {min(scroll + content_height, len(styled_lines))}"
162
+ if footer and height > 0:
163
+ stdscr.addnstr(height - 1, 0, footer, max(0, width - 1), curses.A_DIM)
164
+
165
+ stdscr.refresh()
166
+ ch = stdscr.getch()
167
+
168
+ if ch in (ord("q"), ord("Q")):
169
+ return
170
+ elif ch == ord(":"):
171
+ cmd = _prompt_command(stdscr, prompt=":")
172
+ status_msg = None
173
+ if cmd is not None:
174
+ parts = cmd.split(maxsplit=1)
175
+ name = parts[0].lower() if parts else ""
176
+ if name in ("find", "f", "search", "s"):
177
+ apply_search(parts[1] if len(parts) == 2 else "", include_current=True)
178
+ elif name in ("next", "n"):
179
+ repeat_search(1)
180
+ elif name in ("prev", "previous", "p"):
181
+ repeat_search(-1)
182
+ else:
183
+ new_idx, msg = _apply_command(cmd, total_lines, idx)
184
+ if new_idx == -1:
185
+ return
186
+ idx = new_idx
187
+ scroll = 0
188
+ status_msg = msg
189
+ elif ch == ord("/"):
190
+ cmd = _prompt_command(stdscr, prompt="/")
191
+ status_msg = None
192
+ if cmd is not None:
193
+ apply_search(cmd, include_current=True)
194
+ elif ch == ord("n"):
195
+ repeat_search(1)
196
+ elif ch == ord("N"):
197
+ repeat_search(-1)
198
+ elif ch == curses.KEY_DOWN:
199
+ scroll += 1
200
+ elif ch == curses.KEY_UP:
201
+ scroll -= 1
202
+ elif ch == curses.KEY_LEFT:
203
+ idx -= 1
204
+ scroll = 0
205
+ elif ch == curses.KEY_RIGHT:
206
+ idx += 1
207
+ scroll = 0
208
+ elif ch in (curses.KEY_NPAGE,):
209
+ indent_delta = max(indent_delta - 1, 1)
210
+ elif ch in (curses.KEY_PPAGE,):
211
+ indent_delta = min(indent_delta + 1, 8)
212
+ elif ch == curses.KEY_RESIZE:
213
+ pass
214
+ else:
215
+ pass
216
+
217
+
218
+ def main(argv: Optional[List[str]] = None) -> None:
219
+ parser = argparse.ArgumentParser(prog="jsonl", add_help=True)
220
+ parser.add_argument("file", metavar="FILE", help="Path to a .jsonl file")
221
+ parser.add_argument("-b", "--brief", action="store_true", help="Show file characteristics and exit")
222
+ parser.add_argument("-t", "--theme", metavar="STR", type=str, default="catppuccin-mocha", help="Color theme")
223
+ args = parser.parse_args(argv)
224
+
225
+ path = _validate_path(args.file)
226
+
227
+ if args.brief:
228
+ line_count, size, cols, invalid = _scan_brief(path)
229
+ print(f"File: {path}")
230
+ print(f"Size: {_human_bytes(size)} ({size} bytes)")
231
+ print(f"Lines: {line_count}")
232
+ if invalid:
233
+ print(f"Invalid JSON lines: {invalid}")
234
+ print("Columns:")
235
+ if cols:
236
+ for c in cols:
237
+ print(f" - {c}")
238
+ else:
239
+ print(" (no object keys found)")
240
+ return
241
+
242
+ try:
243
+ curses.wrapper(_viewer, path, args.theme)
244
+ except KeyboardInterrupt:
245
+ return
246
+ except curses.error as e:
247
+ _die(f"curses error: {e}", code=1)
@@ -0,0 +1,84 @@
1
+ import zlib
2
+ import json
3
+ from pathlib import Path
4
+ from typing import List
5
+
6
+
7
+ def _hex_to_rgb(h: str) -> tuple[int, int, int]:
8
+ """
9
+ Converts a hex color string into RGB values.
10
+
11
+ Args:
12
+ h: The hex string.
13
+
14
+ Returns:
15
+ A 3-tuple of the RGB values in base 16, each between 00 and FF inclusive.
16
+ """
17
+ h = h.lstrip("#")
18
+ return (int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16))
19
+
20
+
21
+ def _rgb_to_xterm256(r: int, g: int, b: int) -> int:
22
+ """
23
+ Maps 24-bit RGB values to xterm-256 color indices.
24
+ Will always map the closest possible color.
25
+
26
+ Args:
27
+ r: The base 16 value for red.
28
+ g: The base 16 value for green.
29
+ b: The base 16 value for blue.
30
+
31
+ Returns:
32
+ The index of the corresponding xterm-256 color.
33
+ """
34
+ if r == g == b:
35
+ if r < 8:
36
+ return 16
37
+ if r > 248:
38
+ return 231
39
+ return 232 + (r - 8) // 10
40
+
41
+ def to_6(x: int) -> int:
42
+ return int(round(x / 255 * 5))
43
+
44
+ rr, gg, bb = to_6(r), to_6(g), to_6(b)
45
+ return 16 + 36 * rr + 6 * gg + bb
46
+
47
+
48
+ def _key_to_pair_id(key: str, num_pairs: int) -> int:
49
+ """
50
+ Maps a string to a color via hashing.
51
+ Uses zlib.crc32() as the hash function.
52
+
53
+ Args:
54
+ key: The string to assign a color to.
55
+ num_pairs: The number of colors that can be assigned.
56
+
57
+ Returns:
58
+ The index in curses corresponding to that color.
59
+ Note that index 0 in curses corresponds to the default text color, so the return value is 1-indexed.
60
+ """
61
+ h = zlib.crc32(key.encode("utf-8")) & 0xFFFFFFFF
62
+ return 1 + (h % num_pairs)
63
+
64
+
65
+ def _load_theme(theme: str) -> List[str]:
66
+ """
67
+ Loads a theme's hex colors.
68
+
69
+ Args:
70
+ theme: Name of the theme.
71
+
72
+ Returns:
73
+ A list of strings that represent hex colors for that theme.
74
+ """
75
+ try:
76
+ path = Path(__file__).parent / "themes" / f"{theme}.json"
77
+ with path.open("r", encoding="utf-8") as f:
78
+ data = json.load(f)
79
+ return data["key-colors"]
80
+ except:
81
+ path = Path(__file__).parent / "themes" / "catppuccin-mocha.json" # Default
82
+ with path.open("r", encoding="utf-8") as f:
83
+ data = json.load(f)
84
+ return data["key-colors"]
@@ -0,0 +1,99 @@
1
+ from __future__ import annotations
2
+
3
+ import curses
4
+
5
+
6
+ def _prompt_command(stdscr: "curses._CursesWindow", prompt: str = ":") -> str | None:
7
+ """
8
+ Read a command from the bottom line.
9
+
10
+ Args:
11
+ stdscr: The curses window to display to.
12
+ prompt: The prompt / command to read.
13
+
14
+ Returns:
15
+ The command string (without leading ':') or None if cancelled (ESC).
16
+ """
17
+ height, width = stdscr.getmaxyx()
18
+ y = height - 1
19
+
20
+ buf: list[str] = []
21
+ pos = 0
22
+
23
+ stdscr.move(y, 0)
24
+ stdscr.clrtoeol()
25
+ stdscr.addnstr(y, 0, prompt, width - 1, curses.A_REVERSE)
26
+ stdscr.refresh()
27
+
28
+ while True:
29
+ ch = stdscr.get_wch()
30
+
31
+ if ch == "\x1b":
32
+ return None
33
+
34
+ if ch in ("\n", "\r"):
35
+ return "".join(buf).strip()
36
+
37
+ if ch in (curses.KEY_BACKSPACE, "\b", "\x7f"):
38
+ if pos > 0:
39
+ buf.pop(pos - 1)
40
+ pos -= 1
41
+
42
+ elif ch == curses.KEY_LEFT:
43
+ pos = max(0, pos - 1)
44
+ elif ch == curses.KEY_RIGHT:
45
+ pos = min(len(buf), pos + 1)
46
+ elif ch == curses.KEY_HOME:
47
+ pos = 0
48
+ elif ch == curses.KEY_END:
49
+ pos = len(buf)
50
+ elif isinstance(ch, str) and ch.isprintable():
51
+ buf.insert(pos, ch)
52
+ pos += 1
53
+
54
+ cmd_text = prompt + "".join(buf)
55
+ if len(cmd_text) >= width:
56
+ cmd_text = cmd_text[-(width - 1):]
57
+ stdscr.move(y, 0)
58
+ stdscr.clrtoeol()
59
+ stdscr.addnstr(y, 0, cmd_text, width - 1, curses.A_REVERSE)
60
+
61
+ cursor_x = min(width - 1, len(prompt) + pos)
62
+ stdscr.move(y, cursor_x)
63
+ stdscr.refresh()
64
+
65
+
66
+ def _apply_command(cmd: str, total_lines: int, idx: int) -> tuple[int, str | None]:
67
+ """
68
+ Applies a command string.
69
+
70
+ Args:
71
+ cmd: The command inputted.
72
+ total_lines: The number of rows in the JSONL file.
73
+ idx: The current index being viewed.
74
+
75
+ Returns:
76
+ A tuple (new_idx, status_message_or_None).
77
+ """
78
+ if not cmd:
79
+ return idx, None
80
+
81
+ parts = cmd.split()
82
+ name = parts[0].lower()
83
+
84
+ if name in ("goto", "g") and len(parts) == 2:
85
+ try:
86
+ n = int(parts[1])
87
+ except ValueError:
88
+ return idx, "goto expects an integer row number"
89
+
90
+ if total_lines <= 0:
91
+ return idx, "file has no rows"
92
+
93
+ n = max(1, min(total_lines, n))
94
+ return n - 1, None
95
+
96
+ if name in ("q", "quit", "exit"):
97
+ return -1, None
98
+
99
+ return idx, f"unknown command: {cmd}"
@@ -0,0 +1,14 @@
1
+ from dataclasses import dataclass
2
+ from typing import Any, Optional
3
+
4
+ # We use this datastructure since different portions of 1 line need to have different curses bitmasks (due to colors and bolding).
5
+ Segment = tuple[str, int] # (raw_text, curses_attribute_bitmask)
6
+ StyledLine = list[Segment] # 1 Line in the output
7
+
8
+ # A container for 1 JSONL row.
9
+ @dataclass
10
+ class RowData:
11
+ ok: bool # Whether the line has been successfully parsed to JSON.
12
+ title: str # The status for the row displayed at the top of the window.
13
+ obj: Any = None # The parsed Python object for this JSONL row.
14
+ raw_fallback: Optional[str] = None # Fallback raw JSONL text for failed formatting.