Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
28afdc0ff9
|
|||
|
b228c69ae5
|
|||
|
489d958ac5
|
|||
|
f197416b43
|
|||
|
c599bc55c7
|
|||
|
c5d674ac5c
|
|||
|
ba15f4e93f
|
|||
|
a877ac3e7d
|
|||
|
e4c3f69821
|
|||
|
55861114dc
|
|||
|
74bf941820
|
|||
|
8ac6c16289
|
|||
|
8602db4d43
|
|||
|
5acca12abf
|
21
build.zig
21
build.zig
@@ -4,6 +4,14 @@ pub fn build(b: *std.Build) void {
|
|||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const zlog = b.dependency("zlog", .{
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
.timestamp = true,
|
||||||
|
.stderr = false,
|
||||||
|
.file = "log",
|
||||||
|
});
|
||||||
|
|
||||||
const zterm = b.dependency("zterm", .{
|
const zterm = b.dependency("zterm", .{
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
@@ -17,18 +25,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
.{ .name = "zterm", .module = zterm.module("zterm") },
|
.{ .name = "zterm", .module = zterm.module("zterm") },
|
||||||
.{
|
.{ .name = "zlog", .module = zlog.module("zlog") },
|
||||||
.name = "about",
|
|
||||||
.module = b.createModule(.{
|
|
||||||
.root_source_file = b.path("doc/about.md"),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
.{
|
|
||||||
.name = "blog",
|
|
||||||
.module = b.createModule(.{
|
|
||||||
.root_source_file = b.path("doc/blog.md"),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
.{
|
.{
|
||||||
.name = .tui_website,
|
.name = .tui_website,
|
||||||
// This is a [Semantic Version](https://semver.org/).
|
// This is a [Semantic Version](https://semver.org/).
|
||||||
.version = "0.0.1",
|
.version = "0.0.3",
|
||||||
.fingerprint = 0x93d98a4d9d000e9c, // Changing this has security and trust implications.
|
.fingerprint = 0x93d98a4d9d000e9c, // Changing this has security and trust implications.
|
||||||
.minimum_zig_version = "0.16.0-dev.463+f624191f9",
|
.minimum_zig_version = "0.16.0-dev.463+f624191f9",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.zterm = .{
|
.zterm = .{
|
||||||
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#89aeac1e968f1390bd945f734aac8612efbab179",
|
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#ad32e46bc9fd6d1d9b7a3615979a3b80877c9300",
|
||||||
.hash = "zterm-0.3.0-1xmmELjzGwBxVlqXRHn7p-sXFU9xPxqFMxF0PY2CkzFn",
|
.hash = "zterm-0.3.0-1xmmEO8PHABP4tE6BnEZllJTh6Hpza_5C9wS8s2vjwou",
|
||||||
|
},
|
||||||
|
.zlog = .{
|
||||||
|
.url = "git+https://gitea.yves-biener.de/yves-biener/zlog#f43034cea9a0863e618c3d0a43706ce38c8791cf",
|
||||||
|
.hash = "zlog-0.16.0-6JSlRx1JAADasbK5FS6Qaf0Iq1SCQaH5VZRxT2SRE2xs",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
# Blog
|
---
|
||||||
|
title: Welcome to my Blog
|
||||||
|
date: 2025-11-01
|
||||||
|
---
|
||||||
This is the main entry page for my blog. I plan on writing on things I feel like worth sharing, mentioning for other developers, tinkers like me.
|
This is the main entry page for my blog. I plan on writing on things I feel like worth sharing, mentioning for other developers, tinkers like me.
|
||||||
|
|
||||||
The blog is currently a work in progress and will be updated once a first version of the tui website supports more dynamic contents.
|
The blog is currently a work in progress and will be updated once a first version of the tui website supports more dynamic contents.
|
||||||
|
|||||||
51
doc/test.md
Normal file
51
doc/test.md
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
---
|
||||||
|
title: Capabilities of tui-website
|
||||||
|
---
|
||||||
|
Not everything that can be done with markdown can be representented by this tui-application. It is simple enough to
|
||||||
|
support basic markdown files. Currently the text has to be manually reflowed to 120 characters, such that there is no
|
||||||
|
issue when rendering (i.e. correct line breaks, etc.)
|
||||||
|
|
||||||
|
The following list contains all the available features that are supported (with some examples below):
|
||||||
|
|
||||||
|
- unordered lists (using `-`, `+` and `*`, where the corresponding symbol is used when rendering)
|
||||||
|
* links
|
||||||
|
+ blocks
|
||||||
|
- `inline blocks` (written as `inline blocks`)
|
||||||
|
* _italic_ (written as `_italic_`)
|
||||||
|
+ **bold** (written as `**bold**`)
|
||||||
|
- headlines (corresponding sub heading six levels deep)
|
||||||
|
|
||||||
|
Headings currently require a second newline before the list items to render correctly with a empty line in between the
|
||||||
|
following contents and the last list item.
|
||||||
|
|
||||||
|
Another paragraph.
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
|
||||||
|
> quotes are not supported (yet)
|
||||||
|
|
||||||
|
The following is a block. Which renders as follows (containing line numbers and **no code highlighting**):
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
```zig
|
||||||
|
fn myFunction() void {
|
||||||
|
if (hasFeature()) {
|
||||||
|
// Feature-specific code
|
||||||
|
} else {
|
||||||
|
// Default code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fn hasFeature() bool {
|
||||||
|
return (comptime comptimeCheck()) and runtimeCheck();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Combinations are possible
|
||||||
|
|
||||||
|
1. see this excellent blog post: [Conditionally Disabling Code with comptime in Zig - Mitchell Hashimoto](https://mitchellh.com/writing/zig-comptime-conditional-disable)
|
||||||
|
|
||||||
|
### See deeper levels are possible and will be rendered correctly
|
||||||
|
|
||||||
|
This is just some placeholder text.
|
||||||
149
src/content.zig
149
src/content.zig
@@ -1,25 +1,68 @@
|
|||||||
pub fn Content(App: type) type {
|
pub fn Content(App: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
pub fn init(allocator: Allocator) @This() {
|
allocator: Allocator,
|
||||||
_ = allocator;
|
document: *const App.Model.Document,
|
||||||
return .{};
|
|
||||||
|
pub fn init(allocator: Allocator, document: *const App.Model.Document) @This() {
|
||||||
|
return .{
|
||||||
|
.allocator = allocator,
|
||||||
|
.document = document,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
.vtable = &.{
|
.vtable = &.{
|
||||||
|
.minSize = minSize,
|
||||||
.handle = handle,
|
.handle = handle,
|
||||||
.content = content,
|
.content = content,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn minSize(ctx: *anyopaque, size: zterm.Point) zterm.Point {
|
||||||
|
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||||
|
const text = this.document.content;
|
||||||
|
var index: usize = 0;
|
||||||
|
var new_size: zterm.Point = .{ .x = size.x };
|
||||||
|
|
||||||
|
for (0..text.len) |_| rows: {
|
||||||
|
for (0..size.x - 1) |_| { // assume that there is one cell reserved for the scrollbar
|
||||||
|
if (index == text.len) break :rows;
|
||||||
|
const cp = text[index];
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
switch (cp) {
|
||||||
|
'\n' => break,
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_size.y += 1;
|
||||||
|
}
|
||||||
|
return new_size;
|
||||||
|
}
|
||||||
|
|
||||||
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
|
||||||
_ = ctx;
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.about => model.page = .about,
|
.init => this.document = &model.document,
|
||||||
.blog => model.page = .blog,
|
.about => {
|
||||||
|
model.page.deinit(this.allocator);
|
||||||
|
model.page = .about;
|
||||||
|
|
||||||
|
model.document.deinit(this.allocator);
|
||||||
|
model.document = .init(try std.fs.cwd().readFileAlloc("./doc/about.md", this.allocator, .unlimited));
|
||||||
|
this.document = &model.document;
|
||||||
|
},
|
||||||
|
.blog => |path| {
|
||||||
|
model.page.deinit(this.allocator);
|
||||||
|
model.page = .{ .blog = path };
|
||||||
|
|
||||||
|
model.document.deinit(this.allocator);
|
||||||
|
model.document = .init(try std.fs.cwd().readFileAlloc(if (path) |p| p else "./doc/blog.md", this.allocator, .unlimited));
|
||||||
|
this.document = &model.document;
|
||||||
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,16 +71,12 @@ pub fn Content(App: type) type {
|
|||||||
_ = ctx;
|
_ = ctx;
|
||||||
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
const text = switch (model.page) {
|
const text = model.document.content;
|
||||||
.about => @embedFile("about"),
|
|
||||||
.blog => @embedFile("blog"),
|
|
||||||
};
|
|
||||||
var index: usize = 0;
|
var index: usize = 0;
|
||||||
|
|
||||||
for (0..size.y) |row| {
|
for (0..size.y) |row| {
|
||||||
for (0..size.x) |col| {
|
for (0..size.x) |col| {
|
||||||
const cell = row * size.x + col;
|
const cell = row * size.x + col;
|
||||||
assert(cell < cells.len);
|
|
||||||
|
|
||||||
if (index == text.len) return;
|
if (index == text.len) return;
|
||||||
const cp = text[index];
|
const cp = text[index];
|
||||||
@@ -53,6 +92,94 @@ pub fn Content(App: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn Title(App: type) type {
|
||||||
|
return struct {
|
||||||
|
pub fn element(this: *@This()) App.Element {
|
||||||
|
return .{
|
||||||
|
.ptr = this,
|
||||||
|
.vtable = &.{
|
||||||
|
.content = content,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
|
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||||
|
_ = this;
|
||||||
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
|
if (model.document.title) |text| {
|
||||||
|
for (0.., text) |idx, cp| {
|
||||||
|
cells[idx].style.fg = .green;
|
||||||
|
cells[idx].style.emphasis = &.{.bold};
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
|
||||||
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
|
if (idx == cells.len - 1) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn InfoBanner(App: type) type {
|
||||||
|
return struct {
|
||||||
|
left_text: []const u8 = "Build with zig",
|
||||||
|
right_text: []const u8 = "Yves Biener (@yves-biener)",
|
||||||
|
|
||||||
|
pub fn element(this: *@This()) App.Element {
|
||||||
|
return .{
|
||||||
|
.ptr = this,
|
||||||
|
.vtable = &.{
|
||||||
|
.content = content,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
|
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||||
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
|
for (0.., this.left_text) |idx, cp| {
|
||||||
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
|
cells[idx].style.fg = .default;
|
||||||
|
cells[idx].style.emphasis = &.{.dim};
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (model.page) {
|
||||||
|
.about => {},
|
||||||
|
.blog => |path| if (path) |p| page: {
|
||||||
|
const start_idx = (size.x -| p.len) / 2;
|
||||||
|
if (start_idx <= this.left_text.len) break :page;
|
||||||
|
|
||||||
|
for (start_idx.., p) |idx, cp| {
|
||||||
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
|
cells[idx].style.fg = .default;
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var start_idx = size.x -| this.right_text.len;
|
||||||
|
if (start_idx <= this.left_text.len) start_idx = this.left_text.len + 1;
|
||||||
|
|
||||||
|
for (start_idx.., this.right_text) |idx, cp| {
|
||||||
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
|
cells[idx].style.fg = .default;
|
||||||
|
cells[idx].style.emphasis = &.{.dim};
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
|
|||||||
177
src/main.zig
177
src/main.zig
@@ -1,3 +1,4 @@
|
|||||||
|
// usage: tui_website <path>
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
errdefer |err| log.err("Application Error: {any}", .{err});
|
errdefer |err| log.err("Application Error: {any}", .{err});
|
||||||
|
|
||||||
@@ -6,7 +7,18 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var app: App = .init(.{});
|
var arg_it = try std.process.argsWithAllocator(allocator);
|
||||||
|
errdefer arg_it.deinit();
|
||||||
|
|
||||||
|
// skip own executable name
|
||||||
|
_ = arg_it.skip();
|
||||||
|
|
||||||
|
var app: App = .init(.{
|
||||||
|
.page = .about,
|
||||||
|
.document = .init(try std.fs.cwd().readFileAlloc("./doc/about.md", allocator, .unlimited)),
|
||||||
|
});
|
||||||
|
defer app.model.deinit(allocator);
|
||||||
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -18,71 +30,123 @@ pub fn main() !void {
|
|||||||
},
|
},
|
||||||
}, .{});
|
}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
var content_container: App.Container = undefined;
|
||||||
|
defer content_container.deinit();
|
||||||
|
|
||||||
var header: App.Container = try .init(allocator, .{
|
// header with navigation buttons and content's title
|
||||||
.size = .{
|
|
||||||
.dim = .{ .y = 1 },
|
|
||||||
.grow = .horizontal,
|
|
||||||
},
|
|
||||||
}, .{});
|
|
||||||
var navigation: App.Container = try .init(allocator, .{
|
|
||||||
.layout = .{
|
|
||||||
.separator = .{ .enabled = true },
|
|
||||||
},
|
|
||||||
}, .{});
|
|
||||||
// about navigation button
|
|
||||||
{
|
{
|
||||||
var button: App.Button(.about) = .init(&app.queue, .init(.default, "about"));
|
var header: App.Container = try .init(allocator, .{
|
||||||
try navigation.append(try .init(allocator, .{
|
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 5 + 2 },
|
.dim = .{ .y = 1 },
|
||||||
.grow = .vertical,
|
.grow = .horizontal,
|
||||||
},
|
},
|
||||||
}, button.element()));
|
.layout = .{
|
||||||
|
.separator = .{ .enabled = true },
|
||||||
|
},
|
||||||
|
}, .{});
|
||||||
|
// title
|
||||||
|
{
|
||||||
|
var title: Title = .{};
|
||||||
|
try header.append(try .init(allocator, .{}, title.element()));
|
||||||
|
}
|
||||||
|
// about navigation button
|
||||||
|
{
|
||||||
|
var button: NavigationButton(.about) = .init(&app.model, &app.queue);
|
||||||
|
try header.append(try .init(allocator, .{
|
||||||
|
.size = .{
|
||||||
|
.dim = .{ .x = 5 + 2 },
|
||||||
|
.grow = .vertical,
|
||||||
|
},
|
||||||
|
}, button.element()));
|
||||||
|
}
|
||||||
|
// blog navigation button
|
||||||
|
{
|
||||||
|
var button: NavigationButton(.blog) = .init(&app.model, &app.queue);
|
||||||
|
try header.append(try .init(allocator, .{
|
||||||
|
.size = .{
|
||||||
|
.dim = .{ .x = 4 + 2 },
|
||||||
|
.grow = .vertical,
|
||||||
|
},
|
||||||
|
}, button.element()));
|
||||||
|
}
|
||||||
|
try container.append(header);
|
||||||
}
|
}
|
||||||
// blog navigation button
|
// main actual tui_website page content
|
||||||
{
|
{
|
||||||
var button: App.Button(.blog) = .init(&app.queue, .init(.default, "blog"));
|
var content: Content = .init(allocator, &app.model.document);
|
||||||
try navigation.append(try .init(allocator, .{
|
content_container = try .init(allocator, .{}, content.element());
|
||||||
.size = .{
|
|
||||||
.dim = .{ .x = 4 + 2 },
|
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
||||||
.grow = .vertical,
|
// intermediate container for *padding* containing the scrollable `Content`
|
||||||
},
|
var scrollable_container: App.Container = try .init(allocator, .{
|
||||||
}, button.element()));
|
.layout = .{ .padding = .horizontal(2) },
|
||||||
|
}, .{});
|
||||||
|
try scrollable_container.append(try .init(allocator, .{}, scrollable.element()));
|
||||||
|
try container.append(scrollable_container);
|
||||||
|
}
|
||||||
|
// footer
|
||||||
|
{
|
||||||
|
var info_banner: InfoBanner = .{};
|
||||||
|
const footer: App.Container = try .init(allocator, .{
|
||||||
|
.size = .{
|
||||||
|
.dim = .{ .y = 1 },
|
||||||
|
.grow = .horizontal,
|
||||||
|
},
|
||||||
|
}, info_banner.element());
|
||||||
|
try container.append(footer);
|
||||||
}
|
}
|
||||||
try header.append(navigation);
|
|
||||||
try container.append(header);
|
|
||||||
var content: Content = .init(allocator);
|
|
||||||
try container.append(try .init(allocator, .{}, content.element()));
|
|
||||||
|
|
||||||
try app.start();
|
try app.start();
|
||||||
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
|
if (arg_it.next()) |path| {
|
||||||
|
// TODO check path is only pointing to allowed files?
|
||||||
|
// - only *markdown* files (file extension `.md`)
|
||||||
|
// - only in a specific path / directory?
|
||||||
|
// -> enforce a specific structure?
|
||||||
|
// -> in case an invalid path is provided the 404 error page shall be shown
|
||||||
|
// -> reporte an corresponding error! (such that I can see that in the log)
|
||||||
|
app.postEvent(.{ .blog = try allocator.dupe(u8, path) });
|
||||||
|
}
|
||||||
|
arg_it.deinit();
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
while (true) {
|
loop: while (true) {
|
||||||
|
// batch events since last iteration
|
||||||
|
const len = blk: {
|
||||||
|
app.queue.poll();
|
||||||
|
app.queue.lock();
|
||||||
|
defer app.queue.unlock();
|
||||||
|
break :blk app.queue.len();
|
||||||
|
};
|
||||||
|
|
||||||
// handle events
|
// handle events
|
||||||
const event = app.nextEvent();
|
for (0..len) |_| {
|
||||||
|
const event = app.queue.pop();
|
||||||
|
|
||||||
// pre event handling
|
// pre event handling
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key => |key| {
|
.key => |key| {
|
||||||
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit();
|
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit();
|
||||||
},
|
// test if the event handling is working correctly
|
||||||
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
if (key.eql(.{ .cp = zterm.input.Space })) app.postEvent(.{ .blog = allocator.dupe(u8, "./doc/test.md") catch unreachable });
|
||||||
else => {},
|
},
|
||||||
}
|
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
|
||||||
container.handle(&app.model, event) catch |err| app.postEvent(.{
|
container.handle(&app.model, event) catch |err| app.postEvent(.{
|
||||||
.err = .{
|
.err = .{
|
||||||
.err = err,
|
.err = err,
|
||||||
.msg = "Container Event handling failed",
|
.msg = "Container Event handling failed",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// post event handling
|
// post event handling
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.quit => break,
|
.quit => break :loop,
|
||||||
else => {},
|
else => {},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
container.resize(try renderer.resize());
|
container.resize(try renderer.resize());
|
||||||
@@ -94,21 +158,24 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub const panic = App.panic_handler;
|
pub const panic = App.panic_handler;
|
||||||
|
pub const std_options = zlog.std_options;
|
||||||
const log = std.log.scoped(.default);
|
const log = std.log.scoped(.default);
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
|
const zlog = @import("zlog");
|
||||||
const zterm = @import("zterm");
|
const zterm = @import("zterm");
|
||||||
const App = zterm.App(
|
const App = zterm.App(
|
||||||
Model,
|
Model,
|
||||||
union(enum) {
|
Model.Pages,
|
||||||
about,
|
|
||||||
blog,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const contents = @import("content.zig");
|
||||||
const Model = @import("model.zig");
|
const Model = @import("model.zig");
|
||||||
const Content = @import("content.zig").Content(App);
|
const Content = contents.Content(App);
|
||||||
|
const Title = contents.Title(App);
|
||||||
|
const InfoBanner = contents.InfoBanner(App);
|
||||||
|
const NavigationButton = @import("navigation.zig").NavigationButton(App);
|
||||||
|
|
||||||
test {
|
test {
|
||||||
std.testing.refAllDeclsRecursive(@This());
|
std.testing.refAllDeclsRecursive(@This());
|
||||||
|
|||||||
@@ -1,6 +1,71 @@
|
|||||||
page: Pages = .blog,
|
page: Pages,
|
||||||
|
document: Document,
|
||||||
|
|
||||||
const Pages = enum {
|
pub fn deinit(this: *@This(), allocator: Allocator) void {
|
||||||
|
this.page.deinit(allocator);
|
||||||
|
this.document.deinit(allocator);
|
||||||
|
|
||||||
|
this.page = undefined;
|
||||||
|
this.document = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Pages = union(enum) {
|
||||||
about,
|
about,
|
||||||
blog,
|
blog: ?[]const u8,
|
||||||
|
|
||||||
|
pub fn deinit(this: @This(), allocator: Allocator) void {
|
||||||
|
switch (this) {
|
||||||
|
.blog => |path| if (path) |p| allocator.free(p),
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const Document = struct {
|
||||||
|
// preemble:
|
||||||
|
title: ?[]const u8 = null,
|
||||||
|
date: ?[]const u8 = null,
|
||||||
|
content: []const u8 = undefined,
|
||||||
|
ptr: []const u8,
|
||||||
|
|
||||||
|
const Preemble = enum {
|
||||||
|
title,
|
||||||
|
date,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init(content: []const u8) @This() {
|
||||||
|
if (std.mem.startsWith(u8, content, "---\n")) {
|
||||||
|
var document: @This() = .{ .ptr = content };
|
||||||
|
// we assume the content includes a preemble
|
||||||
|
var iter = std.mem.splitSequence(u8, content, "---\n");
|
||||||
|
assert(iter.next().?.len == 0);
|
||||||
|
if (iter.next()) |preemble| {
|
||||||
|
var lines = std.mem.splitScalar(u8, preemble, '\n');
|
||||||
|
while (lines.next()) |line| {
|
||||||
|
var entry = std.mem.splitSequence(u8, line, ": ");
|
||||||
|
const key = entry.next().?;
|
||||||
|
const value = entry.next();
|
||||||
|
assert(entry.next() == null);
|
||||||
|
if (std.meta.stringToEnum(Preemble, key)) |k| switch (k) {
|
||||||
|
.title => document.title = value,
|
||||||
|
.date => document.date = value,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.content = iter.rest();
|
||||||
|
return document;
|
||||||
|
} else return .{
|
||||||
|
.content = content,
|
||||||
|
.ptr = content,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(this: *@This(), allocator: Allocator) void {
|
||||||
|
allocator.free(this.ptr);
|
||||||
|
this.* = .{ .ptr = undefined };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
|||||||
67
src/navigation.zig
Normal file
67
src/navigation.zig
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
pub fn NavigationButton(App: type) fn (std.meta.FieldEnum(App.Event)) type {
|
||||||
|
const navigation_struct = struct {
|
||||||
|
fn navigation_fn(page: std.meta.FieldEnum(App.Event)) type {
|
||||||
|
return struct {
|
||||||
|
text: []const u8,
|
||||||
|
highlight: bool,
|
||||||
|
queue: *App.Queue,
|
||||||
|
|
||||||
|
pub fn init(model: *const App.Model, queue: *App.Queue) @This() {
|
||||||
|
return .{
|
||||||
|
.text = @tagName(page),
|
||||||
|
.highlight = switch (page) {
|
||||||
|
.about => model.page == .about,
|
||||||
|
.blog => model.page == .blog,
|
||||||
|
else => @compileError("NavigationButton initiated with non page `App.Event` variant"),
|
||||||
|
},
|
||||||
|
.queue = queue,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn element(this: *@This()) App.Element {
|
||||||
|
return .{
|
||||||
|
.ptr = this,
|
||||||
|
.vtable = &.{
|
||||||
|
.handle = handle,
|
||||||
|
.content = content,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
|
switch (event) {
|
||||||
|
.about, .blog => this.highlight = event == page,
|
||||||
|
// TODO accept key input too?
|
||||||
|
.mouse => |mouse| if (mouse.button == .left and mouse.kind == .release) this.queue.push(switch (page) {
|
||||||
|
.about => .about,
|
||||||
|
.blog => .{ .blog = null },
|
||||||
|
else => @compileError("The provided navigation button event to trigger is not a `App.Model.Pages` enum variation."),
|
||||||
|
}),
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
|
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||||
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
assert(size.x == this.text.len + 2);
|
||||||
|
assert(size.y == 1);
|
||||||
|
|
||||||
|
for (1.., this.text) |idx, cp| {
|
||||||
|
cells[idx].style.fg = if (this.highlight) .black else .default;
|
||||||
|
cells[idx].style.bg = if (this.highlight) .green else .default;
|
||||||
|
cells[idx].style.emphasis = &.{.underline};
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return navigation_struct.navigation_fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
const zterm = @import("zterm");
|
||||||
Reference in New Issue
Block a user