summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig116
1 files changed, 66 insertions, 50 deletions
diff --git a/src/main.zig b/src/main.zig
index 85298e5..cf99cad 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -5,63 +5,28 @@
 const Allocator = std.mem.Allocator;
 const DebugAllocator = std.heap.DebugAllocator;
 const builtin = @import("builtin");
+const eql = std.mem.eql;
+const ns_per_s = std.time.ns_per_s;
 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 Key = vaxis.Key;
+const Loop = vaxis.Loop;
+const Tty = vaxis.Tty;
+const Unicode = vaxis.Unicode;
+const Winsize = vaxis.Winsize;
+const gwidth = vaxis.gwidth.gwidth;
 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,
-        };
-    }
+const Event = union(enum) {
+    key_press: Key,
+    winsize: Winsize,
 };
 
 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) {
@@ -69,8 +34,59 @@ pub fn main() !void {
         .ReleaseFast, .ReleaseSmall => smp_allocator,
     };
 
-    var app = try App.init(allocator);
-    defer app.deinit();
-    var state = State{};
-    try app.run(state.widget(), .{});
+    var tty = try Tty.init();
+    defer tty.deinit();
+    var vx = try vaxis.init(allocator, .{});
+    defer vx.deinit(allocator, tty.anyWriter());
+    var loop = Loop(Event){ .tty = &tty, .vaxis = &vx };
+    try loop.init();
+    try loop.start();
+    defer loop.stop();
+
+    try vx.enterAltScreen(tty.anyWriter());
+    // For the alternate screen
+    try vx.queryTerminal(tty.anyWriter(), 1 * ns_per_s);
+
+    const text = "int main()\n{\n\treturn 0;\n}\n";
+    var tokens = try Token.ize(text, languages.c);
+    defer tokens.deinit();
+
+    while (true) {
+        switch (loop.nextEvent()) {
+            .key_press => |key| {
+                if (key.matches('c', .{ .ctrl = true })) {
+                    break;
+                }
+            },
+            .winsize => |ws| try vx.resize(allocator, tty.anyWriter(), ws),
+        }
+        const window = vx.window();
+        window.clear();
+        var col: u16 = 0;
+        var row: u16 = 0;
+        tokens.reset();
+        while (tokens.next()) |token| {
+            var graphemes = vx.unicode.graphemeIterator(token.text);
+            while (graphemes.next()) |grapheme| {
+                const bytes = grapheme.bytes(token.text);
+                if (eql(u8, bytes, "\r\n")
+                    or eql(u8, bytes, "\r")
+                    or eql(u8, bytes, "\n")) {
+                    col = 0;
+                    row += 1;
+                    continue;
+                }
+                const width: u8 = if (eql(u8, bytes, "\t"))
+                    8 // TODO: make configurable
+                else
+                    @intCast(gwidth(bytes, vx.caps.unicode,
+                             &vx.unicode.width_data));
+                defer col += width;
+                window.writeCell(col, row, .{
+                    .char = .{ .grapheme = bytes, .width = width },
+                });
+            }
+        }
+        try vx.render(tty.anyWriter());
+    }
 }