Compare commits
6 Commits
ba15f4e93f
...
0.0.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
28afdc0ff9
|
|||
|
b228c69ae5
|
|||
|
489d958ac5
|
|||
|
f197416b43
|
|||
|
c599bc55c7
|
|||
|
c5d674ac5c
|
@@ -7,7 +7,7 @@ pub fn build(b: *std.Build) void {
|
||||
const zlog = b.dependency("zlog", .{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.timestamp = false,
|
||||
.timestamp = true,
|
||||
.stderr = false,
|
||||
.file = "log",
|
||||
});
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
.{
|
||||
.name = .tui_website,
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
.version = "0.0.2",
|
||||
.version = "0.0.3",
|
||||
.fingerprint = 0x93d98a4d9d000e9c, // Changing this has security and trust implications.
|
||||
.minimum_zig_version = "0.16.0-dev.463+f624191f9",
|
||||
.dependencies = .{
|
||||
.zterm = .{
|
||||
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#b3dc8096d76d6e246d48f9034a65997c0047c3c6",
|
||||
.hash = "zterm-0.3.0-1xmmENP4GwBw65udohsoaxc9Kp4Yo7kORt4okY5pLslr",
|
||||
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#ad32e46bc9fd6d1d9b7a3615979a3b80877c9300",
|
||||
.hash = "zterm-0.3.0-1xmmEO8PHABP4tE6BnEZllJTh6Hpza_5C9wS8s2vjwou",
|
||||
},
|
||||
.zlog = .{
|
||||
.url = "git+https://gitea.yves-biener.de/yves-biener/zlog#f43034cea9a0863e618c3d0a43706ce38c8791cf",
|
||||
|
||||
@@ -3,10 +3,10 @@ pub fn Content(App: type) type {
|
||||
allocator: Allocator,
|
||||
document: *const App.Model.Document,
|
||||
|
||||
pub fn init(allocator: Allocator) @This() {
|
||||
pub fn init(allocator: Allocator, document: *const App.Model.Document) @This() {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.document = undefined,
|
||||
.document = document,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn Content(App: type) type {
|
||||
var new_size: zterm.Point = .{ .x = size.x };
|
||||
|
||||
for (0..text.len) |_| rows: {
|
||||
for (0..size.x) |_| {
|
||||
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;
|
||||
|
||||
76
src/main.zig
76
src/main.zig
@@ -1,3 +1,4 @@
|
||||
// usage: tui_website <path>
|
||||
pub fn main() !void {
|
||||
errdefer |err| log.err("Application Error: {any}", .{err});
|
||||
|
||||
@@ -6,6 +7,12 @@ pub fn main() !void {
|
||||
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
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)),
|
||||
@@ -66,10 +73,10 @@ pub fn main() !void {
|
||||
}
|
||||
// main actual tui_website page content
|
||||
{
|
||||
var content: Content = .init(allocator);
|
||||
var content: Content = .init(allocator, &app.model.document);
|
||||
content_container = try .init(allocator, .{}, content.element());
|
||||
|
||||
var scrollable: App.Scrollable = .init(content_container, .enabled(.green));
|
||||
var scrollable: App.Scrollable = .init(content_container, .enabled(.green, false));
|
||||
// intermediate container for *padding* containing the scrollable `Content`
|
||||
var scrollable_container: App.Container = try .init(allocator, .{
|
||||
.layout = .{ .padding = .horizontal(2) },
|
||||
@@ -92,33 +99,54 @@ pub fn main() !void {
|
||||
try app.start();
|
||||
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
|
||||
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
|
||||
const event = app.nextEvent();
|
||||
for (0..len) |_| {
|
||||
const event = app.queue.pop();
|
||||
|
||||
// pre event handling
|
||||
switch (event) {
|
||||
.key => |key| {
|
||||
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) 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 });
|
||||
},
|
||||
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
||||
else => {},
|
||||
}
|
||||
// pre event handling
|
||||
switch (event) {
|
||||
.key => |key| {
|
||||
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) 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 });
|
||||
},
|
||||
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
||||
else => {},
|
||||
}
|
||||
|
||||
container.handle(&app.model, event) catch |err| app.postEvent(.{
|
||||
.err = .{
|
||||
.err = err,
|
||||
.msg = "Container Event handling failed",
|
||||
},
|
||||
});
|
||||
container.handle(&app.model, event) catch |err| app.postEvent(.{
|
||||
.err = .{
|
||||
.err = err,
|
||||
.msg = "Container Event handling failed",
|
||||
},
|
||||
});
|
||||
|
||||
// post event handling
|
||||
switch (event) {
|
||||
.quit => break,
|
||||
else => {},
|
||||
// post event handling
|
||||
switch (event) {
|
||||
.quit => break :loop,
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
container.resize(try renderer.resize());
|
||||
|
||||
Reference in New Issue
Block a user