summary refs log tree commit diff
path: root/src/main.zig
blob: 85298e51c938a910d93bf75c8d362eedeca0454f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Entry point
// 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(), .{});
}