From 5d913f58e2671652068a70ac1bc677f3854616aa Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Sun, 27 Oct 2024 18:43:27 +0100 Subject: [PATCH] feat(node2buffer): support lists (not nested yet); fix block representation (remove unnecessary linebreak) --- src/widget/ViewPort.zig | 9 ++++++--- src/widget/node2buffer.zig | 22 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/widget/ViewPort.zig b/src/widget/ViewPort.zig index ef6c872..8699bd6 100644 --- a/src/widget/ViewPort.zig +++ b/src/widget/ViewPort.zig @@ -56,7 +56,7 @@ pub fn update(this: *@This(), event: Event) void { .path => |path| { const file = std.fs.cwd().openFile(path, .{ .mode = .read_only }) catch |err| { // TODO: in case of an error show an error-page or an error notification? - std.log.debug("could not open file: {s} due to {any}", .{ path, err }); + std.log.err("could not open file: {s} due to {any}", .{ path, err }); return; }; defer file.close(); @@ -65,6 +65,8 @@ pub fn update(this: *@This(), event: Event) void { } this.contents = file.readToEndAlloc(this.allocator, 4096) catch @panic("could not read to end"); + // TODO: support typst files as parser and display driver -> as I'll be using this file format anyway with my personal note system + // - I should leverage the typst compiler! (i.e. maybe use the html export, once that is available?) var zmd = Zmd.init(this.allocator); defer zmd.deinit(); @@ -88,7 +90,7 @@ pub fn update(this: *@This(), event: Event) void { /// control over this. pub fn draw(this: *@This(), win: vaxis.Window) void { // TODO: this is not very performant and should be improved - var rows: usize = 0; + var rows: usize = 1; for (this.buffer.items) |cell| { if (std.mem.eql(u8, cell.char.grapheme, "\n")) { rows += 1; @@ -99,11 +101,12 @@ pub fn draw(this: *@This(), win: vaxis.Window) void { const Pos = struct { x: usize = 0, y: usize = 0 }; var pos: Pos = .{}; for (this.buffer.items) |cell| { - this.view.writeCell(win, pos.x, pos.y, cell); + // NOTE: do not print newline characters, but instead go to the next row if (std.mem.eql(u8, cell.char.grapheme, "\n")) { pos.x = 0; pos.y += 1; } else { + this.view.writeCell(win, pos.x, pos.y, cell); pos.x += 1; } } diff --git a/src/widget/node2buffer.zig b/src/widget/node2buffer.zig index 16cd367..de5e348 100644 --- a/src/widget/node2buffer.zig +++ b/src/widget/node2buffer.zig @@ -15,7 +15,6 @@ pub fn toBuffer( ) !void { var next_start: ?usize = start; var style = sty; - // FIXME: support list display // determine general styling changes switch (node.token.element.type) { @@ -52,9 +51,8 @@ pub fn toBuffer( // determine content that needs to be displayed const content = value: { switch (node.token.element.type) { - .text => break :value input[node.token.start..node.token.end], + .text, .list_item => break :value input[node.token.start..node.token.end], .link => break :value input[node.token.start + 1 .. node.token.start + 1 + node.title.?.len], - // TODO: use corresponding link contents to create 'real' links using escape sequences .code_close => { if (next_start) |s| { next_start = null; @@ -153,6 +151,9 @@ pub fn toBuffer( // 03 | ... // ... // 10 | ... + try array.append(.{ + .char = .{ .grapheme = "\n" }, + }); var rows: usize = 0; var c: usize = 0; // TODO: would be cool to not have to re-iterate over the contents @@ -196,6 +197,9 @@ pub fn toBuffer( .style = style, }); for (c..content.len) |c_i| { + if (r == rows and content[c_i] == '\n') { + break; + } try array.append(.{ .char = .{ .grapheme = content[c_i .. c_i + 1] }, .style = style, @@ -207,6 +211,18 @@ pub fn toBuffer( } } }, + .list_item => { + // TODO: detect nested lists and adjust indentation accordingly + try array.append(.{ + .char = .{ .grapheme = "\n" }, + }); + for (content, 0..) |_, i| { + try array.append(.{ + .char = .{ .grapheme = content[i .. i + 1] }, + .style = .{ .fg = .{ .index = 1 } }, + }); + } + }, else => { for (content, 0..) |_, i| { try array.append(.{