summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig40
1 files changed, 15 insertions, 25 deletions
diff --git a/src/main.zig b/src/main.zig
index ea2db917..52093b9a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -144,32 +144,22 @@ fn pageInto(io: std.Io, gpa: std.mem.Allocator, dial: []const u8, text: []const
/// Stdin to its end, as it came (its escapes are the session's to parse,
/// pager.zig), at most a file's limit, a longer one cut there and said so
/// at its end.
-fn readStdinText(gpa: std.mem.Allocator) ![]u8 {
+fn readStdinText(io: std.Io, gpa: std.mem.Allocator) ![]u8 {
const limit = pardes.memory.limits.max_file_bytes;
var raw: std.ArrayList(u8) = .empty;
var chunk: [64 * 1024]u8 = undefined;
- var cut = false;
- while (true) {
- const n = std.c.read(0, &chunk, chunk.len);
- if (n < 0) {
- if (std.posix.errno(n) == .INTR) continue;
- return error.ReadFailed;
- }
- if (n == 0) break;
- const got: usize = @intCast(n);
- if (raw.items.len + got > limit) {
- try raw.appendSlice(gpa, chunk[0 .. limit - raw.items.len]);
- cut = true;
- break;
- }
- try raw.appendSlice(gpa, chunk[0..got]);
- }
- if (cut) {
- // CAN ends an escape the cut left half sent, and SGR 0 its colour,
- // so the note is never swallowed or tinted.
- const said = "\x18\x1b[m\n[pardes: the rest of stdin is cut: a pane holds at most a file's limit]\n";
- try raw.appendSlice(gpa, said);
- }
+ var stdin = std.Io.File.stdin().readerStreaming(io, &chunk);
+ // One byte past the limit says there is more.
+ stdin.interface.appendRemaining(gpa, &raw, .limited(limit + 1)) catch |err| switch (err) {
+ error.StreamTooLong => {
+ raw.shrinkRetainingCapacity(limit);
+ // CAN ends an escape the cut left half sent, and SGR 0 its colour,
+ // so the note is never swallowed or tinted.
+ const said = "\x18\x1b[m\n[pardes: the rest of stdin is cut: a pane holds at most a file's limit]\n";
+ try raw.appendSlice(gpa, said);
+ },
+ else => |e| return e,
+ };
return raw.toOwnedSlice(gpa);
}
@@ -561,7 +551,7 @@ fn nativeMain(std_init: std.process.Init) !void {
// made of it (as acme's `B` has no stdin, through pane/new, a name
// and the body), and back at once: the PAGER a pane's shell gets.
if (positional) |word| if (std.mem.eql(u8, word, "-")) {
- const text = readStdinText(arena) catch |err| Refuse.with(init.io, "stdin", err);
+ const text = readStdinText(init.io, arena) catch |err| Refuse.with(init.io, "stdin", err);
// Nothing paged: no pane, and done.
if (text.len == 0) return;
pageInto(init.io, arena, parent.dial, text);
@@ -676,7 +666,7 @@ fn nativeMain(std_init: std.process.Init) !void {
// `pardes -` outside a session: an editor whose first pane holds stdin,
// as `vim -` does; the terminal is /dev/tty, never stdin.
if (positional) |a| if (std.mem.eql(u8, a, "-")) {
- opts.stdin_text = readStdinText(arena) catch |err| badArgs(init.io, "stdin: {s}", .{@errorName(err)});
+ opts.stdin_text = readStdinText(init.io, arena) catch |err| badArgs(init.io, "stdin: {s}", .{@errorName(err)});
// Nothing paged: nothing to show.
if (opts.stdin_text.?.len == 0) return;
positional = null;