add: search text field in preparation for fuzzy search feature
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 59s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 59s
This commit is contained in:
@@ -18,6 +18,7 @@ pub fn Website(App: type) type {
|
|||||||
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
||||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
switch (event) {
|
switch (event) {
|
||||||
|
.page => |page| switch (page) {
|
||||||
.about => {
|
.about => {
|
||||||
model.page.deinit(this.gpa);
|
model.page.deinit(this.gpa);
|
||||||
model.page = .about;
|
model.page = .about;
|
||||||
@@ -37,6 +38,12 @@ pub fn Website(App: type) type {
|
|||||||
model.document = .init(try std.fs.cwd().readFileAlloc(this.gpa, if (path) |p| p else "./doc/blog.md", std.math.maxInt(usize)));
|
model.document = .init(try std.fs.cwd().readFileAlloc(this.gpa, if (path) |p| p else "./doc/blog.md", std.math.maxInt(usize)));
|
||||||
model.page = .{ .blog = path };
|
model.page = .{ .blog = path };
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
.key => |key| {
|
||||||
|
if (key.eql(.{ .cp = '/' })) model.searching = true;
|
||||||
|
|
||||||
|
if (key.eql(.{ .cp = zterm.input.Escape })) model.searching = false;
|
||||||
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,20 +125,66 @@ pub fn Title(App: type) type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn InfoBanner(App: type) type {
|
pub fn InfoBanner(App: type) type {
|
||||||
const left_text: []const u8 = "Build with zig";
|
const left_text: []const u8 = "Build using zig";
|
||||||
const right_text: []const u8 = "Yves Biener (@yves-biener)";
|
const right_text: []const u8 = "Yves Biener";
|
||||||
|
|
||||||
return struct {
|
return struct {
|
||||||
|
gpa: Allocator,
|
||||||
|
search: App.TextField,
|
||||||
|
queue: *App.Queue,
|
||||||
|
|
||||||
|
pub fn init(gpa: Allocator, queue: *App.Queue) @This() {
|
||||||
|
return .{
|
||||||
|
.gpa = gpa,
|
||||||
|
.search = .init(gpa, .init(.default, .default)),
|
||||||
|
.queue = queue,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
.vtable = &.{
|
.vtable = &.{
|
||||||
|
.deinit = deinit,
|
||||||
|
.minSize = minSize,
|
||||||
|
.handle = handle,
|
||||||
.content = content,
|
.content = content,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
fn deinit(ctx: *anyopaque) void {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
|
this.search.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn minSize(ctx: *anyopaque, model: *const App.Model, _: zterm.Point) zterm.Point {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
|
if (!model.searching) this.search.clear();
|
||||||
|
return .{ .y = if (model.searching) 2 else 1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
|
switch (event) {
|
||||||
|
.key => if (model.searching) {
|
||||||
|
const prev_len = this.search.input.items.len;
|
||||||
|
try this.search.element().handle(model, event);
|
||||||
|
const next_len = this.search.input.items.len;
|
||||||
|
|
||||||
|
if (next_len == 0) {
|
||||||
|
model.searching = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE do not include the leading '/' in the `.search` event payload!
|
||||||
|
if (prev_len != next_len) this.queue.push(.{ .search = this.search.input.items[1..] });
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
for (0.., left_text) |idx, cp| {
|
for (0.., left_text) |idx, cp| {
|
||||||
@@ -170,6 +223,11 @@ pub fn InfoBanner(App: type) type {
|
|||||||
cells[idx].style.emphasis = &.{.dim};
|
cells[idx].style.emphasis = &.{.dim};
|
||||||
cells[idx].cp = cp;
|
cells[idx].cp = cp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (model.searching) {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
|
try this.search.element().content(model, cells[size.x..], .{ .x = size.x, .y = 1 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/main.zig
26
src/main.zig
@@ -90,12 +90,9 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
// footer
|
// footer
|
||||||
{
|
{
|
||||||
var info_banner: InfoBanner = .{};
|
var info_banner: InfoBanner = .init(allocator, &app.queue);
|
||||||
const footer: App.Container = try .init(allocator, .{
|
const footer: App.Container = try .init(allocator, .{
|
||||||
.size = .{
|
.size = .{ .grow = .horizontal },
|
||||||
.dim = .{ .y = 1 },
|
|
||||||
.grow = .horizontal,
|
|
||||||
},
|
|
||||||
}, info_banner.element());
|
}, info_banner.element());
|
||||||
try container.append(footer);
|
try container.append(footer);
|
||||||
}
|
}
|
||||||
@@ -114,7 +111,11 @@ pub fn main() !void {
|
|||||||
blog = std.mem.trimStart(u8, blog, "./");
|
blog = std.mem.trimStart(u8, blog, "./");
|
||||||
blog = std.mem.trimEnd(u8, blog, ".md");
|
blog = std.mem.trimEnd(u8, blog, ".md");
|
||||||
blog = std.mem.trimEnd(u8, blog, "/");
|
blog = std.mem.trimEnd(u8, blog, "/");
|
||||||
app.postEvent(.{ .blog = try std.fmt.allocPrint(allocator, "./doc/{s}.md", .{blog}) });
|
app.postEvent(.{
|
||||||
|
.page = .{
|
||||||
|
.blog = try std.fmt.allocPrint(allocator, "./doc/{s}.md", .{blog}),
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
arg_it.deinit();
|
arg_it.deinit();
|
||||||
|
|
||||||
@@ -137,10 +138,16 @@ pub fn main() !void {
|
|||||||
.key => |key| {
|
.key => |key| {
|
||||||
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = 'q' })) app.quit();
|
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = 'q' })) app.quit();
|
||||||
// test if the event handling is working correctly
|
// test if the event handling is working correctly
|
||||||
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = try allocator.dupe(u8, "./doc/test.md") });
|
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{
|
||||||
|
.page = .{
|
||||||
|
.blog = try allocator.dupe(u8, "./doc/test.md"),
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
.page => |page| switch (page) {
|
||||||
.about => log.info(ResourceRequestFormat, .{"./doc/about.md"}),
|
.about => log.info(ResourceRequestFormat, .{"./doc/about.md"}),
|
||||||
.blog => |path| log.info(ResourceRequestFormat, .{if (path) |p| p else "./doc/blog.md"}),
|
.blog => |path| log.info(ResourceRequestFormat, .{if (path) |p| p else "./doc/blog.md"}),
|
||||||
|
},
|
||||||
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
@@ -178,7 +185,10 @@ const zlog = @import("zlog");
|
|||||||
const zterm = @import("zterm");
|
const zterm = @import("zterm");
|
||||||
const App = zterm.App(
|
const App = zterm.App(
|
||||||
Model,
|
Model,
|
||||||
Model.Pages,
|
union(enum) {
|
||||||
|
search: []const u21,
|
||||||
|
page: Model.Pages,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const contents = @import("content.zig");
|
const contents = @import("content.zig");
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
page: Pages,
|
page: Pages,
|
||||||
document: Document,
|
document: Document,
|
||||||
|
|
||||||
|
searching: bool = false,
|
||||||
|
|
||||||
pub fn deinit(this: *@This(), allocator: Allocator) void {
|
pub fn deinit(this: *@This(), allocator: Allocator) void {
|
||||||
this.page.deinit(allocator);
|
this.page.deinit(allocator);
|
||||||
this.document.deinit(allocator);
|
this.document.deinit(allocator);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
pub fn NavigationButton(App: type) fn (std.meta.FieldEnum(App.Event)) type {
|
pub fn NavigationButton(App: type) fn (std.meta.FieldEnum(App.Model.Pages)) type {
|
||||||
const navigation_struct = struct {
|
const navigation_struct = struct {
|
||||||
fn navigation_fn(page: std.meta.FieldEnum(App.Event)) type {
|
fn navigation_fn(page: std.meta.FieldEnum(App.Model.Pages)) type {
|
||||||
return struct {
|
return struct {
|
||||||
text: []const u8,
|
text: []const u8,
|
||||||
highlight: bool,
|
highlight: bool,
|
||||||
@@ -12,7 +12,6 @@ pub fn NavigationButton(App: type) fn (std.meta.FieldEnum(App.Event)) type {
|
|||||||
.highlight = switch (page) {
|
.highlight = switch (page) {
|
||||||
.about => model.page == .about,
|
.about => model.page == .about,
|
||||||
.blog => model.page == .blog,
|
.blog => model.page == .blog,
|
||||||
else => @compileError("NavigationButton initiated with non page `App.Event` variant"),
|
|
||||||
},
|
},
|
||||||
.queue = queue,
|
.queue = queue,
|
||||||
};
|
};
|
||||||
@@ -36,12 +35,13 @@ pub fn NavigationButton(App: type) fn (std.meta.FieldEnum(App.Event)) type {
|
|||||||
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
|
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
|
||||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.about, .blog => this.highlight = event == page,
|
.page => |p| this.highlight = p == page,
|
||||||
// TODO accept key input too?
|
// TODO accept key input too?
|
||||||
.mouse => |mouse| if (mouse.button == .left and mouse.kind == .release) this.queue.push(switch (page) {
|
.mouse => |mouse| if (mouse.button == .left and mouse.kind == .release) this.queue.push(switch (page) {
|
||||||
.about => .about,
|
.about => .{ .page = .about },
|
||||||
.blog => .{ .blog = null },
|
.blog => .{
|
||||||
else => @compileError("The provided navigation button event to trigger is not a `App.Model.Pages` enum variation."),
|
.page = .{ .blog = null },
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
@@ -54,8 +54,7 @@ pub fn NavigationButton(App: type) fn (std.meta.FieldEnum(App.Event)) type {
|
|||||||
assert(size.y == 1);
|
assert(size.y == 1);
|
||||||
|
|
||||||
for (1.., this.text) |idx, cp| {
|
for (1.., this.text) |idx, cp| {
|
||||||
cells[idx].style.fg = if (this.highlight) .black else .default;
|
if (this.highlight) cells[idx].style.ul = .green;
|
||||||
cells[idx].style.bg = if (this.highlight) .green else .default;
|
|
||||||
cells[idx].style.emphasis = &.{.underline};
|
cells[idx].style.emphasis = &.{.underline};
|
||||||
cells[idx].cp = cp;
|
cells[idx].cp = cp;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user