summaryrefslogtreecommitdiff
path: root/src/pager.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/pager.zig')
-rw-r--r--src/pager.zig22
1 files changed, 7 insertions, 15 deletions
diff --git a/src/pager.zig b/src/pager.zig
index 9102fb84..e4ccbab4 100644
--- a/src/pager.zig
+++ b/src/pager.zig
@@ -28,10 +28,10 @@ pub const Page = struct {
}
};
-/// `raw`, a program's output for a terminal, as text and, `colour` on
-/// (PagerColor), its spans. Both are `gpa`'s.
-pub fn parse(gpa: std.mem.Allocator, raw: []const u8, colour: bool) !Page {
- var h: Handler = .{ .gpa = gpa, .colour = colour };
+/// `raw`, a program's output for a terminal, as text and its spans. Both
+/// are `gpa`'s.
+pub fn parse(gpa: std.mem.Allocator, raw: []const u8) !Page {
+ var h: Handler = .{ .gpa = gpa };
errdefer h.text.deinit(gpa);
errdefer h.spans.deinit(gpa);
try h.text.ensureTotalCapacity(gpa, raw.len);
@@ -46,7 +46,6 @@ pub fn parse(gpa: std.mem.Allocator, raw: []const u8, colour: bool) !Page {
const Handler = struct {
gpa: std.mem.Allocator,
- colour: bool,
text: std.ArrayList(u8) = .empty,
spans: std.ArrayList(Span) = .empty,
/// What SGR has set so far.
@@ -74,7 +73,7 @@ const Handler = struct {
const cp = std.unicode.utf8Decode(h.text.items[h.last..]) catch return;
for (0..@min(value, 1024)) |_| h.print(cp);
},
- .set_attribute => if (h.colour) h.attribute(value),
+ .set_attribute => h.attribute(value),
.linefeed => {
h.append("\n");
h.line_start = h.text.items.len;
@@ -178,7 +177,7 @@ const Handler = struct {
/// `from` to the text's end drawn in `style`: the span before it goes
/// on when it ends there in the same style.
fn span(h: *Handler, from: usize, style: CellStyle) void {
- if (!h.colour or std.meta.eql(style, CellStyle{})) return;
+ if (std.meta.eql(style, CellStyle{})) return;
const end: u32 = @intCast(h.text.items.len);
if (h.spans.items.len > 0) {
const tail = &h.spans.items[h.spans.items.len - 1];
@@ -249,7 +248,7 @@ const testing = std.testing;
/// The text `raw` pages to, and its spans as `text[start..end]=style` lines.
fn expectPage(raw: []const u8, text: []const u8, spans: []const struct { []const u8, CellStyle }) !void {
- const page = try parse(testing.allocator, raw, true);
+ const page = try parse(testing.allocator, raw);
defer page.deinit(testing.allocator);
try testing.expectEqualStrings(text, page.text);
for (spans, 0..) |want, i| {
@@ -350,13 +349,6 @@ test "bytes that are not UTF-8 page as U+FFFD, the text around them kept" {
try expectPage("caf\xe9 \xff\xfe ok\n", "caf\u{fffd} \u{fffd}\u{fffd} ok\n", &.{});
}
-test "PagerColor off: the same text, no spans, overstrike included" {
- const page = try parse(testing.allocator, "\x1b[31mred\x1b[m N\x08N\n", false);
- defer page.deinit(testing.allocator);
- try testing.expectEqualStrings("red N\n", page.text);
- try testing.expectEqual(@as(usize, 0), page.spans.len);
-}
-
test "a carriage return's rewrite takes the spans of what it rewrote with it" {
try expectPage("\x1b[31m10%\r\x1b[32m100%\x1b[m\n", "100%\n", &.{.{ "100%", .{ .fg = .{ .index = 2 } } }});
}