This commit is contained in:
Jeeves 2024-04-09 10:25:10 -06:00
parent 6065695af9
commit c849374f44

View file

@ -15,6 +15,7 @@ pub fn main() !void {
var term = try Terminal.init(allocator);
defer term.deinit();
try term.clearScreen();
try term.cursorHide();
var box1 = Terminal.Box.init(allocator);
defer box1.deinit();
@ -27,6 +28,7 @@ pub fn main() !void {
var box2 = Terminal.Box.init(allocator);
defer box2.deinit();
box2.border_bg = try Terminal.Color.init("#000000c0");
box2.content = "hi";
box2.top = 1;
box2.bottom = 3;
box2.left = 20;
@ -35,6 +37,9 @@ pub fn main() !void {
var box3 = Terminal.Box.init(allocator);
defer box3.deinit();
box3.border_bg = try Terminal.Color.init("#48d5eaa0");
box3.content = "hello";
box3.content_halign = .center;
box3.content_valign = .center;
box3.top = 2;
box3.bottom = 1;
box3.left = 20;
@ -47,6 +52,7 @@ pub fn main() !void {
while (true) {
const events = try term.getEvents();
_ = events;
// for (events) |ev| if (ev.system == .int) break;
try term.draw();
std.time.sleep(1000);
}
@ -83,6 +89,7 @@ pub const Terminal = struct {
self.events.deinit();
self.box.deinit();
self.cook() catch @panic("failed to restore termios");
self.cursorShow() catch @panic("failed to unhide cursor");
self.tty.close();
self.info.deinit();
}
@ -121,7 +128,7 @@ pub const Terminal = struct {
.mask = posix.empty_sigset,
.flags = 0,
};
// try posix.sigaction(posix.SIG.INT, &act, null);
try posix.sigaction(posix.SIG.INT, &act, null);
try posix.sigaction(posix.SIG.USR1, &act, null);
try posix.sigaction(posix.SIG.USR2, &act, null);
try posix.sigaction(posix.SIG.WINCH, &act, null);
@ -148,7 +155,7 @@ pub const Terminal = struct {
fn handlerFn(sig: i32) callconv(.C) void {
switch (sig) {
// posix.SIG.INT => ev = Event{ .system = .int },
posix.SIG.INT => ev = Event{ .system = .int },
posix.SIG.USR1 => ev = Event{ .system = .usr1 },
posix.SIG.USR2 => ev = Event{ .system = .usr2 },
posix.SIG.WINCH => ev = Event{ .system = .winch },
@ -159,7 +166,7 @@ pub const Terminal = struct {
pub const Event = union(enum) {
system: enum {
// int,
int,
usr1,
usr2,
winch,
@ -192,6 +199,14 @@ pub const Terminal = struct {
try self.tty.writeAll(formatted);
}
pub fn cursorShow(self: *Terminal) !void {
try self.info.writeString(.cursor_visible, self.tty.writer(), &[_]u32{});
}
pub fn cursorHide(self: *Terminal) !void {
try self.info.writeString(.cursor_invisible, self.tty.writer(), &[_]u32{});
}
pub fn cursorUp(self: *Terminal) !void {
try self.info.writeString(.cursor_up, self.tty.writer(), &[_]u32{});
}
@ -295,6 +310,8 @@ pub const Terminal = struct {
border_fg: Color = Color{ .r = 1.0, .g = 1.0, .b = 1.0 },
content: []const u8 = "",
content_halign: enum { left, center, right } = .left,
content_valign: enum { top, center, bottom } = .top,
pub fn init(allocator: mem.Allocator) Box {
return .{
@ -380,6 +397,19 @@ pub const Terminal = struct {
var x: u32 = 0;
while (x < rect.w) : (x += 1) try term.print(" ", .{});
}
try term.cursorSet(
switch (self.content_halign) {
.left => rect.x,
.center => rect.x + (rect.w / 2) - (@as(u32, @intCast(self.content.len)) / 2),
.right => rect.x + rect.w - (@as(u32, @intCast(self.content.len))),
},
switch (self.content_valign) {
.top => rect.y,
.center => rect.y + (rect.h / 2),
.bottom => rect.y + rect.h - 1,
},
);
try term.print("{s}", .{self.content});
},
}
self.dirty = false;