@@ -6,7 +6,11 @@ const util = @import("../util.zig");
66 const types = @import("../types.zig");
77 const common = @import("common.zig");
88
9-const Args = struct { path: []const u8 };
9+const Args = struct {
10+ path: []const u8,
11+ offset: ?usize = null,
12+ range: ?usize = null,
13+};
1014
1115 const image_mime = [_]struct { ext: []const u8, mime: []const u8 }{
1216 .{ .ext = ".png", .mime = "image/png" },
@@ -34,10 +38,62 @@ fn mimeFor(path: []const u8) ?[]const u8 {
3438 return null;
3539 }
3640
41+/// The position `n` lines after `pos`, or null when fewer than `n` lines
42+/// remain. A huge `n` costs no more than the text has lines.
43+fn advance(text: []const u8, pos: usize, n: usize) ?usize {
44+ var p = pos;
45+ var left = n;
46+ while (left > 0) : (left -= 1) {
47+ const nl = std.mem.indexOfScalarPos(u8, text, p, '\n') orelse return null;
48+ p = nl + 1;
49+ }
50+ return p;
51+}
52+
53+/// The bytes of `range` lines starting at the 1-based line `offset`, each line
54+/// keeping its trailing newline so consecutive reads reassemble the file. Null
55+/// when the text has fewer than `offset` lines; a range that runs past the end
56+/// stops there.
57+fn lineSlice(text: []const u8, offset: usize, range: ?usize) ?[]const u8 {
58+ const start = advance(text, 0, offset - 1) orelse return null;
59+ if (offset > 1 and start >= text.len) return null;
60+ if (range) |r| if (advance(text, start, r)) |end| return text[start..end];
61+ return text[start..];
62+}
63+
64+fn countLines(text: []const u8) usize {
65+ if (text.len == 0) return 0;
66+ return std.mem.count(u8, text, "\n") + @intFromBool(text[text.len - 1] != '\n');
67+}
68+
69+/// The display body for a text read: the line window that was asked for, then
70+/// the number of lines returned, and whether the cap cut the read short. The
71+/// path stays out: the call line above the body already carries it.
72+fn bodyLine(a: std.mem.Allocator, args: Args, lines: usize, truncated: bool) ?[]const u8 {
73+ var out: std.Io.Writer.Allocating = .init(a);
74+ const w = &out.writer;
75+ var window = false;
76+ if (args.offset) |o| {
77+ w.print("offset {d}", .{o}) catch return null;
78+ window = true;
79+ }
80+ if (args.range) |r| {
81+ if (window) w.writeAll(", ") catch return null;
82+ w.print("range {d}", .{r}) catch return null;
83+ window = true;
84+ }
85+ if (window) w.writeAll(" · ") catch return null;
86+ if (lines == 1) w.writeAll("1 line") catch return null else w.print("{d} lines", .{lines}) catch return null;
87+ if (truncated) w.writeAll(", truncated") catch return null;
88+ return out.written();
89+}
90+
3791 pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const u8, ctx: common.Context) common.Result {
3892 const args = std.json.parseFromSliceLeaky(Args, scratch, args_json, .{ .ignore_unknown_fields = true }) catch
3993 return fail(a, "read failed: invalid arguments", .{});
4094 const path = args.path;
95+ if (args.offset) |o| if (o < 1) return fail(a, "read failed: offset must be at least 1", .{});
96+ if (args.range) |r| if (r < 1) return fail(a, "read failed: range must be at least 1", .{});
4197
4298 const data = util.readFileAlloc(a, path, 1 << 30) catch |e|
4399 return fail(a, "read failed: {s}: {s}", .{ path, @errorName(e) });
@@ -55,7 +111,12 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
55111 const summary = std.fmt.allocPrint(a, "read {s} ({s}, {d} bytes)", .{ path, mime, data.len }) catch "read";
56112 const images = a.alloc(types.ImageContent, 1) catch return .{ .text = summary, .is_error = false };
57113 images[0] = .{ .data = b64, .mime_type = mime };
58- return .{ .text = summary, .is_error = false, .images = images };
114+ return .{
115+ .text = summary,
116+ .is_error = false,
117+ .images = images,
118+ .body = std.fmt.allocPrint(a, "{s}, {d} bytes", .{ mime, data.len }) catch null,
119+ };
59120 }
60121
61122 const head = data[0..@min(data.len, 8000)];
@@ -64,13 +125,22 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
64125 }
65126
66127 const text = util.utf8Clean(a, data) catch return fail(a, "read failed: out of memory", .{});
67- if (text.len > max_text_chars) {
68- // Cut back to a code point boundary so the truncation never splits a
69- // sequence and hands the provider bytes that are not valid UTF-8.
128+ const offset = args.offset orelse 1;
129+ const slice = lineSlice(text, offset, args.range) orelse
130+ return fail(a, "read failed: {s} has fewer than {d} lines", .{ path, offset });
131+
132+ // Cut back to a code point boundary so the truncation never splits a
133+ // sequence and hands the provider bytes that are not valid UTF-8.
134+ var sent = slice;
135+ if (slice.len > max_text_chars) {
70136 var cut: usize = max_text_chars;
71- while (cut > 0 and text[cut] & 0xC0 == 0x80) cut -= 1;
72- const truncated = std.fmt.allocPrint(a, "{s}\n\n... truncated ... {s} is {d} characters\n", .{ text[0..cut], path, text.len }) catch "read failed";
73- return .{ .text = truncated, .is_error = false };
137+ while (cut > 0 and slice[cut] & 0xC0 == 0x80) cut -= 1;
138+ sent = slice[0..cut];
74139 }
75- return .{ .text = text, .is_error = false };
140+ const truncated = sent.len < slice.len;
141+ const result_text = if (truncated)
142+ std.fmt.allocPrint(a, "{s}\n\n... truncated ... {s} is {d} characters\n", .{ sent, path, slice.len }) catch "read failed"
143+ else
144+ sent;
145+ return .{ .text = result_text, .is_error = false, .body = bodyLine(a, args, countLines(sent), truncated) };
76146 }