summary refs log tree commit diff
path: root/src/main.zig
blob: f34d8783e65cb1c2d2a916567d8ae0549aea13b2 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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 argsAlloc = std.process.argsAlloc;
const argsFree = std.process.argsFree;
const builtin = @import("builtin");
const cwd = std.fs.cwd;
const eql = std.mem.eql;
const maxInt = std.math.maxInt;
const ns_per_s = std.time.ns_per_s;
const smp_allocator = std.heap.smp_allocator;
const std = @import("std");

const Key = vaxis.Key;
const Loop = vaxis.Loop;
const Style = vaxis.Cell.Style;
const Tty = vaxis.Tty;
const Unicode = vaxis.Unicode;
const Window = vaxis.Window;
const Winsize = vaxis.Winsize;
const gwidth = vaxis.gwidth.gwidth;
const Vaxis = vaxis.Vaxis;
const vaxis = @import("vaxis");

const Parser = tree_sitter.Parser;
const tree_sitter = @import("tree-sitter");

const zsanett = @import("zsanett");
pub fn zsanettIntern(T: type) bool {
    return switch (T) {
        Environment, Key, Loop(Event) => true,
        else => false,
    };
}

const Config = @import("Config.zig");
const selection = @import("selection.zig");
const languages = @import("languages");

const Event = union(enum) {
    key_press: Key,
    winsize: Winsize,
};

const Environment = struct {
    allocator: Allocator,
    loop: *Loop(Event),
    parser: *Parser,
    segment: *selection.Segment,
    text: []const u8,
    tty: *Tty,
    vaxis: *Vaxis,

    pub fn nextEvent(self: Environment) !Event {
        while (true) {
            const event = self.loop.nextEvent();
            switch (event) {
                .key_press => return event,
                .winsize => |ws| {
                    try self.vaxis.resize(self.allocator,
                                          self.tty.anyWriter(), ws);
                    try self.render();
                },
            }
        }
    }

    pub fn render(self: Environment) !void {
        const window = self.vaxis.window();
        window.clear();
        var col: u16 = 0;
        var row: u16 = 0;
        var graphemes = self.vaxis.unicode.graphemeIterator(self.text);
        while (graphemes.next()) |grapheme| {
            const style = Style {
                .reverse = self.segment.contains(grapheme),
            };
            const bytes = grapheme.bytes(self.text);
            if (eql(u8, bytes, "\n")) {
                const width = window.gwidth("$");
                defer col = 0;
                defer row += 1;
                window.writeCell(col, row, .{
                    .char = .{ .grapheme = "$", .width = @intCast(width) },
                    .style = style,
                });
            } else if (eql(u8, bytes, "\t")) {
                var tab = self.vaxis.unicode.graphemeIterator("  ");
                while (tab.next()) |g| {
                    const b = g.bytes("  ");
                    const width = window.gwidth(b);
                    defer col += width;
                    window.writeCell(col, row, .{
                        .char = .{ .grapheme = b, .width = @intCast(width) },
                        .style = style,
                    });
                }
            } else {
                const width = window.gwidth(bytes);
                defer col += width;
                window.writeCell(col, row, .{
                    .char = .{ .grapheme = bytes, .width = @intCast(width) },
                    .style = style,
                });
            }
        }
        try self.vaxis.render(self.tty.anyWriter());
    }

    pub fn goUp(self: Environment) void {
        self.segment.goUp();
    }

    pub fn goRight(self: Environment) void {
        self.segment.goRight(self.vaxis.unicode.width_data.graphemes, self.text);
    }

    pub fn goLeft(self: Environment) void {
        self.segment.goLeft(self.vaxis.unicode.width_data.graphemes, self.text);
    }

    pub fn goDown(self: Environment) void {
        self.segment.goDown(self.vaxis.unicode.width_data.graphemes, self.text);
    }
};

pub fn main() !void {
    var debug_allocator = DebugAllocator(.{}).init;
    defer _ = debug_allocator.deinit();
    const allocator = switch (builtin.mode) {
        .Debug, .ReleaseSafe => debug_allocator.allocator(),
        .ReleaseFast, .ReleaseSmall => smp_allocator,
    };

    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();

    const parser = Parser.create();
    defer parser.destroy();

    const args = try argsAlloc(allocator);
    defer argsFree(allocator, args);
    const text = try cwd().readFileAlloc(allocator, args[1], maxInt(u32));
    defer allocator.free(text);
    try vx.enterAltScreen(tty.anyWriter());
    try vx.queryTerminal(tty.anyWriter(), 1 * ns_per_s); // for alt screen

    const language = languages.c();
    defer language.destroy();
    try parser.setLanguage(language);
    const tree = parser.parseString(text, null).?;
    defer tree.destroy();

    zsanett.init();
    defer zsanett.deinit();
    var segment = selection.Segment{
        .head = .{ .node = tree.rootNode() },
        .tail = .{ .node = tree.rootNode() },
    };
    try zsanett.def("env", Environment{
        .allocator = allocator,
        .loop = &loop,
        .parser = parser,
        .segment = &segment,
        .text = text,
        .tty = &tty,
        .vaxis = &vx,
    }, "eval environment");
    switch (try zsanett.eval(void, @embedFile("main.janet"), "main.janet")) {
        .ret => {},
        .err => |err| @panic(err),
    }
}