mod: remove min_size argument from App.start

This commit is contained in:
2025-02-06 22:19:27 +01:00
parent 8586a05508
commit 11531e9d4a
4 changed files with 80 additions and 128 deletions

View File

@@ -44,7 +44,6 @@ pub fn App(comptime E: type) type {
quit_event: std.Thread.ResetEvent = .{},
termios: ?std.posix.termios = null,
attached_handler: bool = false,
min_size: ?Size = null,
prev_size: Size = .{ .cols = 0, .rows = 0 },
pub const SignalHandler = struct {
@@ -52,8 +51,7 @@ pub fn App(comptime E: type) type {
callback: *const fn (context: *anyopaque) void,
};
pub fn start(this: *@This(), min_size: ?Size) !void {
this.min_size = min_size;
pub fn start(this: *@This()) !void {
if (this.thread) |_| return;
if (!this.attached_handler) {
@@ -69,6 +67,9 @@ pub fn App(comptime E: type) type {
.callback = @This().winsizeCallback,
});
this.attached_handler = true;
// post init event (as the very first element to be in the queue - event loop)
this.postEvent(.init);
}
this.quit_event.reset();
@@ -76,14 +77,16 @@ pub fn App(comptime E: type) type {
var termios: std.posix.termios = undefined;
try terminal.enableRawMode(&termios);
if (this.termios) |_| {} else {
this.termios = termios;
}
if (this.termios) |_| {} else this.termios = termios;
try terminal.saveScreen();
try terminal.enterAltScreen();
try terminal.hideCursor();
// post init event (as the very first element to be in the queue - event loop)
this.postEvent(.init);
// send initial size afterwards
const size = terminal.getTerminalSize();
this.postEvent(.{ .resize = size });
this.prev_size = size;
}
pub fn interrupt(this: *@This()) !void {
@@ -127,15 +130,6 @@ pub fn App(comptime E: type) type {
fn winsizeCallback(ptr: *anyopaque) void {
const this: *@This() = @ptrCast(@alignCast(ptr));
const size = terminal.getTerminalSize();
// check for minimal size (if any was provided)
if (this.min_size) |min_size| {
if (size.cols < min_size.cols or size.rows < min_size.rows) {
this.postEvent(.{
.err = .{ .err = error.InsufficientSize, .msg = "Terminal size is too small for the requested minimal size" },
});
return;
}
}
if (size.cols != this.prev_size.cols or size.rows != this.prev_size.rows) {
this.postEvent(.{ .resize = size });
this.prev_size = size;
@@ -161,19 +155,7 @@ pub fn App(comptime E: type) type {
// send initial terminal size
// changes are handled by the winch signal handler
// see `App.start` and `App.registerWinch` for details
{
// TODO: what should happen if the initial window size is too small?
// -> currently the first render call will then crash the application (which happens anyway)
const size = terminal.getTerminalSize();
if (this.min_size) |min_size| {
if (size.cols < min_size.cols or size.rows < min_size.rows) {
this.postEvent(.{
.err = .{ .err = error.InsufficientSize, .msg = "Terminal size is too small for the requested minimal size" },
});
}
}
this.postEvent(.{ .resize = size });
}
{}
// thread to read user inputs
var buf: [256]u8 = undefined;
@@ -185,9 +167,8 @@ pub fn App(comptime E: type) type {
if (buf[0] == 0x1b and read_bytes > 1) {
switch (buf[1]) {
0x4F => { // ss3
if (read_bytes < 3) {
continue;
}
if (read_bytes < 3) continue;
const key: ?Key = switch (buf[2]) {
0x1B => null,
'A' => .{ .cp = Key.up },
@@ -203,14 +184,11 @@ pub fn App(comptime E: type) type {
'S' => .{ .cp = Key.f4 },
else => null,
};
if (key) |k| {
this.postEvent(.{ .key = k });
}
if (key) |k| this.postEvent(.{ .key = k });
},
0x5B => { // csi
if (read_bytes < 3) {
continue;
}
if (read_bytes < 3) continue;
// We start iterating at index 2 to get past the '['
const sequence = for (buf[2..], 2..) |b, i| {
switch (b) {
@@ -309,15 +287,6 @@ pub fn App(comptime E: type) type {
.rows = std.fmt.parseUnsigned(u16, height_char, 10) catch break,
.cols = std.fmt.parseUnsigned(u16, width_char, 10) catch break,
};
// check for minimal size (if any was provided)
if (this.min_size) |min_size| {
if (size.cols < min_size.cols or size.rows < min_size.rows) {
this.postEvent(.{
.err = .{ .err = error.InsufficientSize, .msg = "Terminal size is too small for the requested minimal size" },
});
break;
}
}
if (size.cols != this.prev_size.cols or size.rows != this.prev_size.rows) {
this.postEvent(.{ .resize = size });
this.prev_size = size;
@@ -355,9 +324,7 @@ pub fn App(comptime E: type) type {
0x7f => .{ .cp = Key.backspace },
else => {
var iter = terminal.code_point.Iterator{ .bytes = buf[0..read_bytes] };
while (iter.next()) |cp| {
this.postEvent(.{ .key = .{ .cp = cp.code } });
}
while (iter.next()) |cp| this.postEvent(.{ .key = .{ .cp = cp.code } });
continue;
},
};