diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.zig | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index c55186b..85298e5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,15 +2,75 @@ // SPDX-FileCopyrightText: 2025 Nguyễn Gia Phong // SPDX-License-Identifier: GPL-3.0-or-later +const Allocator = std.mem.Allocator; +const DebugAllocator = std.heap.DebugAllocator; +const builtin = @import("builtin"); +const smp_allocator = std.heap.smp_allocator; const std = @import("std"); +const App = vaxis.vxfw.App; +const DrawContext = vaxis.vxfw.DrawContext; +const Event = vaxis.vxfw.Event; +const EventContext = vaxis.vxfw.EventContext; +const Surface = vaxis.vxfw.Surface; +const Widget = vaxis.vxfw.Widget; +const vaxis = @import("vaxis"); + const Token = @import("Token.zig"); const languages = @import("languages"); +const State = struct { + pub fn draw(data: *anyopaque, ctx: DrawContext) Allocator.Error!Surface { + const self: *State = @alignCast(@ptrCast(data)); + const max = ctx.max.size(); + return .{ + .size = max, + .widget = self.widget(), + .buffer = &.{}, + .children = &.{}, + }; + } + + pub fn handleEvent(data: *anyopaque, ctx: *EventContext, + event: Event) !void { + const self: *State = @alignCast(@ptrCast(data)); + _ = self; + switch (event) { + .key_press => |key| { + if (key.matches('c', .{ .ctrl = true })) { + ctx.quit = true; + return; + } + }, + else => {}, + } + } + + pub fn widget(self: *State) Widget { + return .{ + .userdata = self, + .eventHandler = State.handleEvent, + .drawFn = State.draw, + }; + } +}; + pub fn main() !void { const text = "int main()\n{\n\treturn 0;\n}\n"; var tokens = try Token.ize(text, languages.c); defer tokens.deinit(); while (tokens.next()) |token| std.debug.print("{s}", .{ token.text }); + + var debug_allocator = DebugAllocator(.{}).init; + defer _ = debug_allocator.deinit(); + const allocator = switch (builtin.mode) { + .Debug, .ReleaseSafe => debug_allocator.allocator(), + .ReleaseFast, .ReleaseSmall => smp_allocator, + }; + + var app = try App.init(allocator); + defer app.deinit(); + var state = State{}; + try app.run(state.widget(), .{}); } |