mod: simplify minor implementation details
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 30s

Add scroll length detection, and link transformation with highlighting.
This commit is contained in:
2024-10-15 16:04:06 +02:00
parent a3d0b7af9a
commit b1257fd490
2 changed files with 18 additions and 25 deletions

View File

@@ -36,17 +36,10 @@ pub fn deinit(this: *@This()) void {
/// Update loop for a given widget to react to the provided `Event`. It may
/// change its internal state, update variables, react to user input, etc.
pub fn update(this: *@This(), event: Event) void {
// FIXME: limit scroll-able area to corresponding contents!
switch (event) {
.key_press => |key| {
if (key.matches(vaxis.Key.right, .{}) or key.matches('l', .{})) {
this.view.scroll.x +|= 1;
} else if (key.matches(vaxis.Key.right, .{ .shift = true }) or key.matches('>', .{})) {
this.view.scroll.x +|= 32;
} else if (key.matches(vaxis.Key.left, .{}) or key.matches('h', .{})) {
this.view.scroll.x -|= 1;
} else if (key.matches(vaxis.Key.left, .{ .shift = true }) or key.matches('<', .{})) {
this.view.scroll.x -|= 32;
} else if (key.matches(vaxis.Key.up, .{}) or key.matches('k', .{})) {
if (key.matches(vaxis.Key.up, .{}) or key.matches('k', .{})) {
this.view.scroll.y -|= 1;
} else if (key.matches(vaxis.Key.page_up, .{}) or key.matches('u', .{ .ctrl = true })) {
this.view.scroll.y -|= 32;
@@ -94,20 +87,18 @@ pub fn update(this: *@This(), event: Event) void {
/// the dimension one widget may take on the screen. The widget itself has no
/// control over this.
pub fn draw(this: *@This(), win: vaxis.Window) void {
// FIXME: the current node2buffer implementation cannot determine how many rows and columns there are from the read file contents
// this.view.draw(win, .{ .cols = this.buffer.cols, .rows = this.buffer.rows }); // needed for scroll bar scaling and display
// TODO: this is not very performant and should be improved
var rows: usize = 0;
for (this.buffer.items) |cell| {
if (std.mem.eql(u8, cell.char.grapheme, "\n")) {
rows += 1;
}
}
this.view.draw(win, .{ .cols = win.width, .rows = rows }); // needed for scroll bar scaling and display
const Pos = struct { x: usize = 0, y: usize = 0 };
var pos: Pos = .{};
const bounds = this.view.bounds(win);
for (this.buffer.items) |cell| {
if (bounds.above(pos.y)) break;
if (!bounds.colInside(pos.x)) {
pos.x = 0;
pos.y += 1;
}
this.view.writeCell(win, pos.x, pos.y, cell);
if (std.mem.eql(u8, cell.char.grapheme, "\n")) {
pos.x = 0;

View File

@@ -14,7 +14,7 @@ pub fn toBuffer(
var next_start: ?usize = start;
var style = sty;
// FIXME: support list display
// TODO: improve code block listings (i.e. add line numbers with corresponding seperator, etc.)
// TODO: improve code block listings (i.e. add line numbers with corresponding separator, etc.)
// determine general styling changes
switch (node.token.element.type) {
@@ -41,6 +41,9 @@ pub fn toBuffer(
next_start = node.token.start;
style.fg = .{ .index = 8 };
},
.link => {
style.fg = .{ .index = 3 };
},
else => {},
}
@@ -48,13 +51,12 @@ pub fn toBuffer(
const content = value: {
switch (node.token.element.type) {
.text => break :value input[node.token.start..node.token.end],
// TODO: use corresponding link contents to create 'real' links using escape sequences
.link => break :value input[node.token.start .. node.token.start + node.href.?.len + node.title.?.len + 4],
.bold_close, .italic_close, .block_close, .code_close, .title_close, .href_close => {
if (next_start) |s| {
next_start = null;
const e = node.token.end;
std.log.debug("start - end: {d}..{d}", .{ s, e });
std.log.debug("content: {s}", .{input[s..e]});
break :value input[s..e];
break :value input[s..node.token.end];
}
break :value "";
},