Compare commits
3 Commits
361623d0f1
...
8199a23566
| Author | SHA1 | Date | |
|---|---|---|---|
| 8199a23566 | |||
| 83d83c17fa | |||
| 1b4c1bbab8 |
@@ -18,6 +18,12 @@ jobs:
|
||||
uses: https://codeberg.org/mlugg/setup-zig@v2
|
||||
with:
|
||||
version: latest
|
||||
- name: Lint check
|
||||
run: zig fmt --check .
|
||||
- name: Spell checking
|
||||
uses: crate-ci/typos@v1.39.0
|
||||
with:
|
||||
config: ./.typos-config
|
||||
- name: Run tests
|
||||
run: zig build --release=fast
|
||||
- name: Release build artifacts
|
||||
|
||||
@@ -1,68 +1,68 @@
|
||||
pub fn Content(App: type) type {
|
||||
pub fn Website(App: type) type {
|
||||
return struct {
|
||||
allocator: Allocator,
|
||||
document: *const App.Model.Document,
|
||||
gpa: Allocator,
|
||||
|
||||
pub fn init(allocator: Allocator, document: *const App.Model.Document) @This() {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.document = document,
|
||||
};
|
||||
pub fn init(gpa: Allocator) @This() {
|
||||
return .{ .gpa = gpa };
|
||||
}
|
||||
|
||||
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
.vtable = &.{
|
||||
.minSize = minSize,
|
||||
.handle = handle,
|
||||
.content = content,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn minSize(ctx: *anyopaque, _: *const App.Model, size: zterm.Point) zterm.Point {
|
||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||
_ = this;
|
||||
//const text = this.document.content;
|
||||
//var index: usize = 0;
|
||||
const new_size: zterm.Point = .{ .x = size.x };
|
||||
|
||||
return new_size;
|
||||
}
|
||||
|
||||
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||
switch (event) {
|
||||
.init => this.document = &model.document,
|
||||
.about => {
|
||||
model.page.deinit(this.allocator);
|
||||
model.page.deinit(this.gpa);
|
||||
model.page = .about;
|
||||
|
||||
model.document.deinit(this.allocator);
|
||||
model.document = .init(try std.fs.cwd().readFileAlloc(this.allocator, "./doc/about.md", std.math.maxInt(usize)));
|
||||
this.document = &model.document;
|
||||
model.document.deinit(this.gpa);
|
||||
model.document = .init(try std.fs.cwd().readFileAlloc(this.gpa, "./doc/about.md", std.math.maxInt(usize)));
|
||||
},
|
||||
.blog => |path| {
|
||||
model.page.deinit(this.allocator);
|
||||
model.document.deinit(this.allocator);
|
||||
model.page.deinit(this.gpa);
|
||||
model.document.deinit(this.gpa);
|
||||
errdefer {
|
||||
if (path) |p| this.allocator.free(p);
|
||||
if (path) |p| this.gpa.free(p);
|
||||
model.document = .invalidPage;
|
||||
model.page = .{ .blog = null };
|
||||
}
|
||||
|
||||
model.document = .init(try std.fs.cwd().readFileAlloc(this.allocator, 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 };
|
||||
|
||||
this.document = &model.document;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||
_ = ctx;
|
||||
pub fn Content(App: type) type {
|
||||
return struct {
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
.vtable = &.{
|
||||
.minSize = minSize,
|
||||
.content = content,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn minSize(_: *anyopaque, model: *const App.Model, _: zterm.Point) zterm.Point {
|
||||
// NOTE currently the contents are assumed to fit into the viewport, excessive lines are cut off
|
||||
// (there currently no horizontal scrolling)
|
||||
return .{ .y = @intCast(std.mem.count(u8, model.document.content, "\n") + 1) };
|
||||
}
|
||||
|
||||
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||
|
||||
const text = model.document.content;
|
||||
@@ -81,6 +81,10 @@ pub fn Content(App: type) type {
|
||||
else => cp,
|
||||
};
|
||||
}
|
||||
if (text[index - 1] != '\n') {
|
||||
// go to the end of the line
|
||||
while (index < text.len and text[index] != '\n') index +=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -97,9 +101,7 @@ pub fn Title(App: type) type {
|
||||
};
|
||||
}
|
||||
|
||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||
_ = this;
|
||||
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||
|
||||
if (model.document.title) |text| {
|
||||
@@ -117,10 +119,10 @@ pub fn Title(App: type) type {
|
||||
}
|
||||
|
||||
pub fn InfoBanner(App: type) type {
|
||||
return struct {
|
||||
left_text: []const u8 = "Build with zig",
|
||||
right_text: []const u8 = "Yves Biener (@yves-biener)",
|
||||
const left_text: []const u8 = "Build with zig";
|
||||
const right_text: []const u8 = "Yves Biener (@yves-biener)";
|
||||
|
||||
return struct {
|
||||
pub fn element(this: *@This()) App.Element {
|
||||
return .{
|
||||
.ptr = this,
|
||||
@@ -130,11 +132,10 @@ pub fn InfoBanner(App: type) type {
|
||||
};
|
||||
}
|
||||
|
||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||
fn content(_: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||
|
||||
for (0.., this.left_text) |idx, cp| {
|
||||
for (0.., left_text) |idx, cp| {
|
||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||
if (idx == cells.len) break;
|
||||
|
||||
@@ -147,7 +148,7 @@ pub fn InfoBanner(App: type) type {
|
||||
.about => {},
|
||||
.blog => |path| if (path) |p| page: {
|
||||
const start_idx = (size.x -| p.len) / 2;
|
||||
if (start_idx <= this.left_text.len) break :page;
|
||||
if (start_idx <= left_text.len) break :page;
|
||||
|
||||
for (start_idx.., p) |idx, cp| {
|
||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||
@@ -159,10 +160,10 @@ pub fn InfoBanner(App: type) type {
|
||||
},
|
||||
}
|
||||
|
||||
var start_idx = size.x -| this.right_text.len;
|
||||
if (start_idx <= this.left_text.len) start_idx = this.left_text.len + 1;
|
||||
var start_idx = size.x -| right_text.len;
|
||||
if (start_idx <= left_text.len) start_idx = left_text.len + 1;
|
||||
|
||||
for (start_idx.., this.right_text) |idx, cp| {
|
||||
for (start_idx.., right_text) |idx, cp| {
|
||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||
if (idx == cells.len) break;
|
||||
|
||||
@@ -178,3 +179,4 @@ const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const assert = std.debug.assert;
|
||||
const zterm = @import("zterm");
|
||||
const Model = @import("model.zig");
|
||||
|
||||
@@ -29,13 +29,14 @@ pub fn main() !void {
|
||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||
defer renderer.deinit();
|
||||
|
||||
var website: Website = .init(allocator);
|
||||
var container = try App.Container.init(allocator, .{
|
||||
.layout = .{
|
||||
.padding = .horizontal(2),
|
||||
.direction = .vertical,
|
||||
.separator = .{ .enabled = true },
|
||||
},
|
||||
}, .{});
|
||||
}, website.element());
|
||||
defer container.deinit();
|
||||
var content_container: App.Container = undefined;
|
||||
|
||||
@@ -79,7 +80,7 @@ pub fn main() !void {
|
||||
}
|
||||
// main actual tui_website page content
|
||||
{
|
||||
var content: Content = .init(allocator, &app.model.document);
|
||||
var content: Content = .{};
|
||||
content_container = try .init(allocator, .{}, content.element());
|
||||
|
||||
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
||||
@@ -139,7 +140,7 @@ pub fn main() !void {
|
||||
.key => |key| {
|
||||
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = 'q' })) app.quit();
|
||||
// test if the event handling is working correctly
|
||||
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = allocator.dupe(u8, "./doc/test.md") catch unreachable });
|
||||
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = try allocator.dupe(u8, "./doc/test.md") });
|
||||
},
|
||||
.about => log.info(ResourceRequestFormat, .{"./doc/about.md"}),
|
||||
.blog => |path| log.info(ResourceRequestFormat, .{if (path) |p| p else "./doc/blog.md"}),
|
||||
@@ -185,6 +186,7 @@ const App = zterm.App(
|
||||
|
||||
const contents = @import("content.zig");
|
||||
const Model = @import("model.zig");
|
||||
const Website = contents.Website(App);
|
||||
const Content = contents.Content(App);
|
||||
const Title = contents.Title(App);
|
||||
const InfoBanner = contents.InfoBanner(App);
|
||||
|
||||
Reference in New Issue
Block a user