2024-06-06 23:41:32 +02:00
|
|
|
const std = @import("std");
|
|
|
|
const vaxis = @import("vaxis");
|
|
|
|
const Cell = vaxis.Cell;
|
|
|
|
|
|
|
|
const Event = union(enum) {
|
|
|
|
key_press: vaxis.Key,
|
|
|
|
winsize: vaxis.Winsize,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const panic = vaxis.panic_handler;
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
defer {
|
|
|
|
const deinit_status = gpa.deinit();
|
|
|
|
//fail test; can't try in defer as defer is executed after we return
|
|
|
|
if (deinit_status == .leak) {
|
|
|
|
std.log.err("memory leak", .{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const alloc = gpa.allocator();
|
|
|
|
|
|
|
|
var tty = try vaxis.Tty.init();
|
|
|
|
var vx = try vaxis.init(alloc, .{});
|
|
|
|
defer vx.deinit(alloc, tty.anyWriter());
|
|
|
|
|
|
|
|
var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx };
|
|
|
|
try loop.init();
|
|
|
|
|
|
|
|
try loop.start();
|
|
|
|
defer loop.stop();
|
|
|
|
|
2024-06-19 01:37:58 +02:00
|
|
|
var buffered = tty.bufferedWriter();
|
|
|
|
|
2024-06-06 23:41:32 +02:00
|
|
|
try vx.enterAltScreen(tty.anyWriter());
|
|
|
|
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
|
|
|
|
var env = try std.process.getEnvMap(alloc);
|
|
|
|
defer env.deinit();
|
|
|
|
|
|
|
|
const vt_opts: vaxis.widgets.Terminal.Options = .{
|
|
|
|
.winsize = .{
|
|
|
|
.rows = 24,
|
|
|
|
.cols = 100,
|
|
|
|
.x_pixel = 0,
|
|
|
|
.y_pixel = 0,
|
|
|
|
},
|
|
|
|
.scrollback_size = 0,
|
2024-06-19 14:11:02 +02:00
|
|
|
.initial_working_directory = env.get("HOME") orelse @panic("no $HOME"),
|
2024-06-06 23:41:32 +02:00
|
|
|
};
|
2024-06-12 02:09:55 +02:00
|
|
|
const shell = env.get("SHELL") orelse "bash";
|
2024-06-09 13:43:43 +02:00
|
|
|
const argv = [_][]const u8{shell};
|
|
|
|
var vt = try vaxis.widgets.Terminal.init(
|
2024-06-06 23:41:32 +02:00
|
|
|
alloc,
|
2024-06-09 13:43:43 +02:00
|
|
|
&argv,
|
2024-06-06 23:41:32 +02:00
|
|
|
&env,
|
|
|
|
&vx.unicode,
|
|
|
|
vt_opts,
|
|
|
|
);
|
2024-06-09 13:43:43 +02:00
|
|
|
defer vt.deinit();
|
|
|
|
try vt.spawn();
|
2024-06-06 23:41:32 +02:00
|
|
|
|
2024-06-13 02:29:19 +02:00
|
|
|
var redraw: bool = false;
|
2024-06-06 23:41:32 +02:00
|
|
|
while (true) {
|
2024-07-04 14:53:40 +02:00
|
|
|
std.debug.print("inside while loop before resize\n", .{});
|
2024-06-06 23:41:32 +02:00
|
|
|
std.time.sleep(8 * std.time.ns_per_ms);
|
2024-06-09 13:43:43 +02:00
|
|
|
// try vt events first
|
|
|
|
while (vt.tryEvent()) |event| {
|
2024-07-04 14:53:40 +02:00
|
|
|
std.debug.print("inside stryEventloop \n", .{});
|
2024-06-13 02:29:19 +02:00
|
|
|
redraw = true;
|
2024-06-09 13:43:43 +02:00
|
|
|
switch (event) {
|
|
|
|
.bell => {},
|
2024-06-10 21:18:55 +02:00
|
|
|
.title_change => {},
|
2024-06-09 13:43:43 +02:00
|
|
|
.exited => return,
|
2024-06-13 02:29:19 +02:00
|
|
|
.redraw => {},
|
2024-06-19 16:23:11 +02:00
|
|
|
.pwd_change => {},
|
2024-06-09 13:43:43 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-06 23:41:32 +02:00
|
|
|
while (loop.tryEvent()) |event| {
|
2024-07-04 14:53:40 +02:00
|
|
|
std.debug.print("inside loop.tryEvent\n", .{});
|
2024-06-13 02:29:19 +02:00
|
|
|
redraw = true;
|
2024-06-06 23:41:32 +02:00
|
|
|
switch (event) {
|
2024-06-09 13:43:43 +02:00
|
|
|
.key_press => |key| {
|
|
|
|
if (key.matches('c', .{ .ctrl = true })) return;
|
|
|
|
try vt.update(.{ .key_press = key });
|
|
|
|
},
|
2024-06-06 23:41:32 +02:00
|
|
|
.winsize => |ws| {
|
|
|
|
try vx.resize(alloc, tty.anyWriter(), ws);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-06-13 02:29:19 +02:00
|
|
|
if (!redraw) continue;
|
|
|
|
redraw = false;
|
2024-06-06 23:41:32 +02:00
|
|
|
|
|
|
|
const win = vx.window();
|
2024-06-10 21:18:55 +02:00
|
|
|
win.hideCursor();
|
2024-06-11 21:00:37 +02:00
|
|
|
win.clear();
|
2024-06-09 13:43:43 +02:00
|
|
|
const child = win.child(.{
|
|
|
|
.x_off = 4,
|
|
|
|
.y_off = 2,
|
|
|
|
.width = .{ .limit = win.width - 8 },
|
|
|
|
.height = .{ .limit = win.width - 6 },
|
2024-06-06 23:41:32 +02:00
|
|
|
.border = .{
|
2024-06-09 13:43:43 +02:00
|
|
|
.where = .all,
|
2024-06-06 23:41:32 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-06-09 13:43:43 +02:00
|
|
|
try vt.resize(.{
|
|
|
|
.rows = child.height,
|
|
|
|
.cols = child.width,
|
2024-06-06 23:41:32 +02:00
|
|
|
.x_pixel = 0,
|
|
|
|
.y_pixel = 0,
|
|
|
|
});
|
2024-06-09 13:43:43 +02:00
|
|
|
try vt.draw(child);
|
2024-06-06 23:41:32 +02:00
|
|
|
|
2024-06-19 01:37:58 +02:00
|
|
|
try vx.render(buffered.writer().any());
|
|
|
|
try buffered.flush();
|
2024-06-06 23:41:32 +02:00
|
|
|
}
|
|
|
|
}
|