summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--build.zig2
-rw-r--r--build.zig.zon4
-rw-r--r--src/main.zig60
3 files changed, 65 insertions, 1 deletions
diff --git a/build.zig b/build.zig
index 6b61dae..310cdfc 100644
--- a/build.zig
+++ b/build.zig
@@ -60,7 +60,7 @@ pub fn build(b: *Build) !void {
         .target = b.standardTargetOptions(.{}),
         .optimize = b.standardOptimizeOption(.{}),
     });
-    inline for (.{ "tree-sitter" }) |lib|
+    inline for (.{ "tree-sitter", "vaxis" }) |lib|
         mod.addImport(lib, b.dependency(lib, .{}).module(lib));
     try linkTreeSitterGrammars(b, mod);
 
diff --git a/build.zig.zon b/build.zig.zon
index fddbfcb..51bce35 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -8,6 +8,10 @@
             .url = "git+https://github.com/tree-sitter/zig-tree-sitter#b4b72c903e69998fc88e27e154a5e3cc9166551b",
             .hash = "tree_sitter-0.25.0-8heIf51vAQConvVIgvm-9mVIbqh7yabZYqPXfOpS3YoG",
         },
+        .vaxis = .{
+            .url = "git+https://github.com/rockorager/libvaxis#cc9154d5f4afa3fdfd289157f195ec67804ef437",
+            .hash = "vaxis-0.5.1-BWNV_MYNCQDO92x2PTpsfv6GwDHmjL98rf6iL3pbMLj4",
+        },
     },
     .paths = .{
         "LICENSES",
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(), .{});
 }