Compare commits
9 Commits
0.0.3
...
db8485d88e
| Author | SHA1 | Date | |
|---|---|---|---|
| db8485d88e | |||
| 8199a23566 | |||
| 83d83c17fa | |||
| 1b4c1bbab8 | |||
| 361623d0f1 | |||
| 6bf4c98d44 | |||
| bfdaf82626 | |||
|
b4836b8d2b
|
|||
|
5ee2c6662e
|
@@ -15,9 +15,15 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Setup zig installation
|
- name: Setup zig installation
|
||||||
uses: mlugg/setup-zig@v2
|
uses: https://codeberg.org/mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: master
|
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
|
- name: Run tests
|
||||||
run: zig build --release=fast
|
run: zig build --release=fast
|
||||||
- name: Release build artifacts
|
- name: Release build artifacts
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Setup zig installation
|
- name: Setup zig installation
|
||||||
uses: mlugg/setup-zig@v2
|
uses: https://codeberg.org/mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: master
|
version: latest
|
||||||
- name: Lint check
|
- name: Lint check
|
||||||
run: zig fmt --check .
|
run: zig fmt --check .
|
||||||
- name: Spell checking
|
- name: Spell checking
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ This is my terminal based website. It is served as a tui application via ssh and
|
|||||||
|
|
||||||
It contains information about me and my projects as well as blog entries about something I feel like writing something about.
|
It contains information about me and my projects as well as blog entries about something I feel like writing something about.
|
||||||
|
|
||||||
> [!caution]
|
|
||||||
> Only builds using the zig master version are tested to work.
|
|
||||||
|
|
||||||
## zterm
|
## zterm
|
||||||
|
|
||||||
This tui application also serves as an example implementation of the [zterm](https://gitea.yves-biener.de/yves-biener/zterm) library.
|
This tui application also serves as an example implementation of the [zterm](https://gitea.yves-biener.de/yves-biener/zterm) library.
|
||||||
|
|||||||
@@ -1,17 +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.3",
|
.version = "0.0.4",
|
||||||
.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#ad32e46bc9fd6d1d9b7a3615979a3b80877c9300",
|
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#4874252e8c3a5d645ac2c37bbfd0e669964912fc",
|
||||||
.hash = "zterm-0.3.0-1xmmEO8PHABP4tE6BnEZllJTh6Hpza_5C9wS8s2vjwou",
|
.hash = "zterm-0.3.0-1xmmEH82HABfsn5imAK8lw93LevIaqNV10g6xfkNj_OT",
|
||||||
},
|
},
|
||||||
.zlog = .{
|
.zlog = .{
|
||||||
.url = "git+https://gitea.yves-biener.de/yves-biener/zlog#f43034cea9a0863e618c3d0a43706ce38c8791cf",
|
.url = "git+https://gitea.yves-biener.de/yves-biener/zlog#40ced30a574118cd76202c07dc4b2291c3321d3a",
|
||||||
.hash = "zlog-0.16.0-6JSlRx1JAADasbK5FS6Qaf0Iq1SCQaH5VZRxT2SRE2xs",
|
.hash = "zlog-0.16.0-6JSlR-BIAAAZ4fSbV54zbZZ9hnZSeOPnnFtiVjmIy3ID",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
|
|||||||
109
src/content.zig
109
src/content.zig
@@ -1,74 +1,67 @@
|
|||||||
pub fn Content(App: type) type {
|
pub fn Website(App: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
allocator: Allocator,
|
gpa: Allocator,
|
||||||
document: *const App.Model.Document,
|
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, document: *const App.Model.Document) @This() {
|
pub fn init(gpa: Allocator) @This() {
|
||||||
return .{
|
return .{ .gpa = gpa };
|
||||||
.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,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.init => this.document = &model.document,
|
|
||||||
.about => {
|
.about => {
|
||||||
model.page.deinit(this.allocator);
|
model.page.deinit(this.gpa);
|
||||||
model.page = .about;
|
model.page = .about;
|
||||||
|
|
||||||
model.document.deinit(this.allocator);
|
model.document.deinit(this.gpa);
|
||||||
model.document = .init(try std.fs.cwd().readFileAlloc("./doc/about.md", this.allocator, .unlimited));
|
model.document = .init(try std.fs.cwd().readFileAlloc(this.gpa, "./doc/about.md", std.math.maxInt(usize)));
|
||||||
this.document = &model.document;
|
|
||||||
},
|
},
|
||||||
.blog => |path| {
|
.blog => |path| {
|
||||||
model.page.deinit(this.allocator);
|
model.page.deinit(this.gpa);
|
||||||
model.page = .{ .blog = path };
|
model.document.deinit(this.gpa);
|
||||||
|
errdefer {
|
||||||
|
if (path) |p| this.gpa.free(p);
|
||||||
|
model.document = .invalidPage;
|
||||||
|
model.page = .{ .blog = null };
|
||||||
|
}
|
||||||
|
|
||||||
model.document.deinit(this.allocator);
|
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(if (path) |p| p else "./doc/blog.md", this.allocator, .unlimited));
|
model.page = .{ .blog = path };
|
||||||
this.document = &model.document;
|
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
pub fn Content(App: type) type {
|
||||||
_ = ctx;
|
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));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
const text = model.document.content;
|
const text = model.document.content;
|
||||||
@@ -87,6 +80,10 @@ pub fn Content(App: type) type {
|
|||||||
else => cp,
|
else => cp,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (text[index - 1] != '\n') {
|
||||||
|
// go to the end of the line
|
||||||
|
while (index < text.len and text[index] != '\n') index += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -103,9 +100,7 @@ pub fn Title(App: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
fn content(_: *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));
|
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
||||||
|
|
||||||
if (model.document.title) |text| {
|
if (model.document.title) |text| {
|
||||||
@@ -123,10 +118,10 @@ pub fn Title(App: type) type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn InfoBanner(App: type) type {
|
pub fn InfoBanner(App: type) type {
|
||||||
return struct {
|
const left_text: []const u8 = "Build with zig";
|
||||||
left_text: []const u8 = "Build with zig",
|
const right_text: []const u8 = "Yves Biener (@yves-biener)";
|
||||||
right_text: []const u8 = "Yves Biener (@yves-biener)",
|
|
||||||
|
|
||||||
|
return struct {
|
||||||
pub fn element(this: *@This()) App.Element {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{
|
return .{
|
||||||
.ptr = this,
|
.ptr = this,
|
||||||
@@ -136,11 +131,10 @@ pub fn InfoBanner(App: type) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
fn content(_: *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));
|
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`
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
if (idx == cells.len) break;
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
@@ -153,7 +147,7 @@ pub fn InfoBanner(App: type) type {
|
|||||||
.about => {},
|
.about => {},
|
||||||
.blog => |path| if (path) |p| page: {
|
.blog => |path| if (path) |p| page: {
|
||||||
const start_idx = (size.x -| p.len) / 2;
|
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| {
|
for (start_idx.., p) |idx, cp| {
|
||||||
// NOTE do not write over the contents of this `Container`'s `Size`
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
@@ -165,10 +159,10 @@ pub fn InfoBanner(App: type) type {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var start_idx = size.x -| this.right_text.len;
|
var start_idx = size.x -| right_text.len;
|
||||||
if (start_idx <= this.left_text.len) start_idx = this.left_text.len + 1;
|
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`
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
||||||
if (idx == cells.len) break;
|
if (idx == cells.len) break;
|
||||||
|
|
||||||
@@ -184,3 +178,4 @@ const std = @import("std");
|
|||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const zterm = @import("zterm");
|
const zterm = @import("zterm");
|
||||||
|
const Model = @import("model.zig");
|
||||||
|
|||||||
39
src/main.zig
39
src/main.zig
@@ -1,4 +1,11 @@
|
|||||||
// usage: tui_website <path>
|
// TODO
|
||||||
|
// - should the path containing the contents of the tui-website reside in the
|
||||||
|
// hardcode `./doc` directory or shall this become a compilation argument to
|
||||||
|
// allow for custom directory locations?
|
||||||
|
|
||||||
|
// usage: tui_website <location>
|
||||||
|
// <location>: partial path to the document without the file extension and the beginning `./doc` path
|
||||||
|
// ending `/` (as used from the web-world) are trimmed automatically
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
errdefer |err| log.err("Application Error: {any}", .{err});
|
errdefer |err| log.err("Application Error: {any}", .{err});
|
||||||
|
|
||||||
@@ -13,25 +20,25 @@ pub fn main() !void {
|
|||||||
// skip own executable name
|
// skip own executable name
|
||||||
_ = arg_it.skip();
|
_ = arg_it.skip();
|
||||||
|
|
||||||
var app: App = .init(.{
|
var app: App = .init(.{}, .{
|
||||||
.page = .about,
|
.page = .about,
|
||||||
.document = .init(try std.fs.cwd().readFileAlloc("./doc/about.md", allocator, .unlimited)),
|
.document = .init(try std.fs.cwd().readFileAlloc(allocator, "./doc/about.md", std.math.maxInt(usize))),
|
||||||
});
|
});
|
||||||
defer app.model.deinit(allocator);
|
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();
|
||||||
|
|
||||||
|
var website: Website = .init(allocator);
|
||||||
var container = try App.Container.init(allocator, .{
|
var container = try App.Container.init(allocator, .{
|
||||||
.layout = .{
|
.layout = .{
|
||||||
.padding = .horizontal(2),
|
.padding = .horizontal(2),
|
||||||
.direction = .vertical,
|
.direction = .vertical,
|
||||||
.separator = .{ .enabled = true },
|
.separator = .{ .enabled = true },
|
||||||
},
|
},
|
||||||
}, .{});
|
}, website.element());
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
var content_container: App.Container = undefined;
|
var content_container: App.Container = undefined;
|
||||||
defer content_container.deinit();
|
|
||||||
|
|
||||||
// header with navigation buttons and content's title
|
// header with navigation buttons and content's title
|
||||||
{
|
{
|
||||||
@@ -73,7 +80,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
// main actual tui_website page content
|
// main actual tui_website page content
|
||||||
{
|
{
|
||||||
var content: Content = .init(allocator, &app.model.document);
|
var content: Content = .{};
|
||||||
content_container = try .init(allocator, .{}, content.element());
|
content_container = try .init(allocator, .{}, content.element());
|
||||||
|
|
||||||
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
||||||
@@ -106,7 +113,11 @@ pub fn main() !void {
|
|||||||
// -> enforce a specific structure?
|
// -> enforce a specific structure?
|
||||||
// -> in case an invalid path is provided the 404 error page shall be shown
|
// -> 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)
|
// -> reporte an corresponding error! (such that I can see that in the log)
|
||||||
app.postEvent(.{ .blog = try allocator.dupe(u8, path) });
|
var blog = path[0..path.len];
|
||||||
|
blog = std.mem.trimStart(u8, blog, "./");
|
||||||
|
blog = std.mem.trimEnd(u8, blog, ".md");
|
||||||
|
blog = std.mem.trimEnd(u8, blog, "/");
|
||||||
|
app.postEvent(.{ .blog = try std.fmt.allocPrint(allocator, "./doc/{s}.md", .{blog}) });
|
||||||
}
|
}
|
||||||
arg_it.deinit();
|
arg_it.deinit();
|
||||||
|
|
||||||
@@ -127,10 +138,12 @@ pub fn main() !void {
|
|||||||
// 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 } }) 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 = 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"}),
|
||||||
.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 => {},
|
||||||
}
|
}
|
||||||
@@ -149,9 +162,8 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
container.resize(try renderer.resize());
|
container.resize(&app.model, try renderer.resize());
|
||||||
container.reposition(.{});
|
container.reposition(&app.model, .{});
|
||||||
|
|
||||||
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
|
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
|
||||||
try renderer.flush();
|
try renderer.flush();
|
||||||
}
|
}
|
||||||
@@ -161,6 +173,8 @@ pub const panic = App.panic_handler;
|
|||||||
pub const std_options = zlog.std_options;
|
pub const std_options = zlog.std_options;
|
||||||
const log = std.log.scoped(.default);
|
const log = std.log.scoped(.default);
|
||||||
|
|
||||||
|
const ResourceRequestFormat = "Requesting resource: `{s}`";
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const zlog = @import("zlog");
|
const zlog = @import("zlog");
|
||||||
@@ -172,6 +186,7 @@ const App = zterm.App(
|
|||||||
|
|
||||||
const contents = @import("content.zig");
|
const contents = @import("content.zig");
|
||||||
const Model = @import("model.zig");
|
const Model = @import("model.zig");
|
||||||
|
const Website = contents.Website(App);
|
||||||
const Content = contents.Content(App);
|
const Content = contents.Content(App);
|
||||||
const Title = contents.Title(App);
|
const Title = contents.Title(App);
|
||||||
const InfoBanner = contents.InfoBanner(App);
|
const InfoBanner = contents.InfoBanner(App);
|
||||||
|
|||||||
@@ -33,6 +33,12 @@ pub const Document = struct {
|
|||||||
date,
|
date,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const invalidPage: @This() = .{
|
||||||
|
.title = "Page not found",
|
||||||
|
.content = "Requested page does not exist",
|
||||||
|
.ptr = undefined,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn init(content: []const u8) @This() {
|
pub fn init(content: []const u8) @This() {
|
||||||
if (std.mem.startsWith(u8, content, "---\n")) {
|
if (std.mem.startsWith(u8, content, "---\n")) {
|
||||||
var document: @This() = .{ .ptr = content };
|
var document: @This() = .{ .ptr = content };
|
||||||
|
|||||||
Reference in New Issue
Block a user