feat(node2buffer): support lists (not nested yet); fix block representation (remove unnecessary linebreak)
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m37s

This commit is contained in:
2024-10-27 18:43:27 +01:00
parent b91918e2e6
commit 5d913f58e2
2 changed files with 25 additions and 6 deletions

View File

@@ -56,7 +56,7 @@ pub fn update(this: *@This(), event: Event) void {
.path => |path| { .path => |path| {
const file = std.fs.cwd().openFile(path, .{ .mode = .read_only }) catch |err| { 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? // 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; return;
}; };
defer file.close(); 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"); 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); var zmd = Zmd.init(this.allocator);
defer zmd.deinit(); defer zmd.deinit();
@@ -88,7 +90,7 @@ pub fn update(this: *@This(), event: Event) void {
/// control over this. /// control over this.
pub fn draw(this: *@This(), win: vaxis.Window) void { pub fn draw(this: *@This(), win: vaxis.Window) void {
// TODO: this is not very performant and should be improved // TODO: this is not very performant and should be improved
var rows: usize = 0; var rows: usize = 1;
for (this.buffer.items) |cell| { for (this.buffer.items) |cell| {
if (std.mem.eql(u8, cell.char.grapheme, "\n")) { if (std.mem.eql(u8, cell.char.grapheme, "\n")) {
rows += 1; rows += 1;
@@ -99,11 +101,12 @@ pub fn draw(this: *@This(), win: vaxis.Window) void {
const Pos = struct { x: usize = 0, y: usize = 0 }; const Pos = struct { x: usize = 0, y: usize = 0 };
var pos: Pos = .{}; var pos: Pos = .{};
for (this.buffer.items) |cell| { 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")) { if (std.mem.eql(u8, cell.char.grapheme, "\n")) {
pos.x = 0; pos.x = 0;
pos.y += 1; pos.y += 1;
} else { } else {
this.view.writeCell(win, pos.x, pos.y, cell);
pos.x += 1; pos.x += 1;
} }
} }

View File

@@ -15,7 +15,6 @@ pub fn toBuffer(
) !void { ) !void {
var next_start: ?usize = start; var next_start: ?usize = start;
var style = sty; var style = sty;
// FIXME: support list display
// determine general styling changes // determine general styling changes
switch (node.token.element.type) { switch (node.token.element.type) {
@@ -52,9 +51,8 @@ pub fn toBuffer(
// determine content that needs to be displayed // determine content that needs to be displayed
const content = value: { const content = value: {
switch (node.token.element.type) { 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], .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 => { .code_close => {
if (next_start) |s| { if (next_start) |s| {
next_start = null; next_start = null;
@@ -153,6 +151,9 @@ pub fn toBuffer(
// 03 | ... // 03 | ...
// ... // ...
// 10 | ... // 10 | ...
try array.append(.{
.char = .{ .grapheme = "\n" },
});
var rows: usize = 0; var rows: usize = 0;
var c: usize = 0; var c: usize = 0;
// TODO: would be cool to not have to re-iterate over the contents // TODO: would be cool to not have to re-iterate over the contents
@@ -196,6 +197,9 @@ pub fn toBuffer(
.style = style, .style = style,
}); });
for (c..content.len) |c_i| { for (c..content.len) |c_i| {
if (r == rows and content[c_i] == '\n') {
break;
}
try array.append(.{ try array.append(.{
.char = .{ .grapheme = content[c_i .. c_i + 1] }, .char = .{ .grapheme = content[c_i .. c_i + 1] },
.style = style, .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 => { else => {
for (content, 0..) |_, i| { for (content, 0..) |_, i| {
try array.append(.{ try array.append(.{