feat: accept argument for path to open
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m14s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m14s
This commit is contained in:
@@ -3,10 +3,10 @@ pub fn Content(App: type) type {
|
|||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
document: *const App.Model.Document,
|
document: *const App.Model.Document,
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) @This() {
|
pub fn init(allocator: Allocator, document: *const App.Model.Document) @This() {
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.document = undefined,
|
.document = document,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,6 +22,9 @@ pub fn Content(App: type) type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn minSize(ctx: *anyopaque, size: zterm.Point) zterm.Point {
|
fn minSize(ctx: *anyopaque, size: zterm.Point) zterm.Point {
|
||||||
|
// TODO what about that initial size? seems wrong!
|
||||||
|
if (size.x == 0 or size.y == 0) return size;
|
||||||
|
|
||||||
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
const this: *const @This() = @ptrCast(@alignCast(ctx));
|
||||||
const text = this.document.content;
|
const text = this.document.content;
|
||||||
var index: usize = 0;
|
var index: usize = 0;
|
||||||
|
|||||||
74
src/main.zig
74
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,6 +7,12 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
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(.{
|
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("./doc/about.md", allocator, .unlimited)),
|
||||||
@@ -66,7 +73,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
// main actual tui_website page content
|
// 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());
|
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));
|
||||||
@@ -92,33 +99,54 @@ pub fn main() !void {
|
|||||||
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
|
// 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 = allocator.dupe(u8, "./doc/test.md") catch unreachable });
|
||||||
},
|
},
|
||||||
.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 => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
||||||
|
|||||||
Reference in New Issue
Block a user