From 9f0c0d4afbebabf329a1e2b4f34b7d38bb056189 Mon Sep 17 00:00:00 2001 From: Jeeves Date: Fri, 5 Apr 2024 18:03:30 -0600 Subject: [PATCH] a lot --- src/main.zig | 62 +++++++++++++++++++++++++++++++++++++++++------- src/terminfo.zig | 15 ++++++++++-- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/src/main.zig b/src/main.zig index 02f3297..0dc73ae 100644 --- a/src/main.zig +++ b/src/main.zig @@ -14,17 +14,16 @@ pub fn main() !void { var term = try Terminal.init(allocator); defer term.deinit(); - // var info = try Terminal.Info.init(allocator); - // defer info.deinit(); - - // var seq = Terminal.Info.Sequence.init(allocator, &info); - // defer seq.deinit(); - // try seq.cursorLeft(); try term.print("poopoo", .{}); try term.cursorLeft(); try term.cursorLeft(); try term.print("ee", .{}); - // try seq.writeOut(term.tty.writer()); //io.AnyWriter{ .context = &term.tty, .writeFn = &fs.File.write }); + // try term.cursorSet(12, 3); + try term.boldOn(); + try term.underlineOn(); + try term.print("AAAAAAAAAAAAAAAAAAA", .{}); + try term.boldOff(); + try term.underlineOff(); } pub const Terminal = struct { @@ -87,9 +86,56 @@ pub const Terminal = struct { try self.tty.writeAll(formatted); } + pub fn cursorUp(self: *Terminal) !void { + try self.info.writeString(.cursor_up, self.tty.writer(), &[_]u32{}); + } + + pub fn cursorDown(self: *Terminal) !void { + try self.info.writeString(.cursor_down, self.tty.writer(), &[_]u32{}); + } + pub fn cursorLeft(self: *Terminal) !void { try self.info.writeString(.cursor_left, self.tty.writer(), &[_]u32{}); - // try self.info.cursorLeft(self.tty.writer()); + } + + pub fn cursorRight(self: *Terminal) !void { + try self.info.writeString(.cursor_right, self.tty.writer(), &[_]u32{}); + } + + pub fn cursorSet(self: *Terminal, x: u32, y: u32) !void { + try self.info.writeString(.cursor_address, self.tty.writer(), &[_]u32{ y, x }); + } + + pub fn blinkOn(self: *Terminal) !void { + try self.info.writeString(.enter_blink_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn blinkOff(self: *Terminal) !void { + try self.info.writeString(.exit_blink_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn boldOn(self: *Terminal) !void { + try self.info.writeString(.enter_bold_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn boldOff(self: *Terminal) !void { + try self.info.writeString(.exit_bold_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn italicsOn(self: *Terminal) !void { + try self.info.writeString(.enter_italics_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn italicsOff(self: *Terminal) !void { + try self.info.writeString(.exit_italics_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn underlineOn(self: *Terminal) !void { + try self.info.writeString(.enter_underline_mode, self.tty.writer(), &[_]u32{}); + } + + pub fn underlineOff(self: *Terminal) !void { + try self.info.writeString(.exit_underline_mode, self.tty.writer(), &[_]u32{}); } pub const Info = @import("terminfo.zig"); diff --git a/src/terminfo.zig b/src/terminfo.zig index 60275f9..64b0603 100644 --- a/src/terminfo.zig +++ b/src/terminfo.zig @@ -190,7 +190,8 @@ strings: std.StringHashMap([]const u8), /// Writes the formatted sequence to a given writer. pub fn writeString(self: *Self, string: String, writer: anytype, arguments: []const u32) !void { - const output = self.strings.get(string.toCapName()); + const capname = string.toCapName(); + const output = if (capname) |cn| self.strings.get(cn) else string.default() orelse return error.NoDefaultString; if (output) |out| { var formatted = std.ArrayList(u8).init(self.allocator); defer formatted.deinit(); @@ -218,6 +219,7 @@ pub fn writeString(self: *Self, string: String, writer: anytype, arguments: []co '9' => args[8], else => return error.InvalidFormatChar, }; + i += 1; try fmt.format(stack.writer(), "{d}", .{arg}); }, 'P' => {}, @@ -411,6 +413,7 @@ pub const String = enum { exit_am_mode, exit_attribute_mode, exit_ca_mode, + exit_bold_mode, exit_delete_mode, exit_doublewide_mode, exit_insert_mode, @@ -534,7 +537,7 @@ pub const String = enum { xon_character, zero_motion, - pub fn toCapName(self: String) []const u8 { + pub fn toCapName(self: String) ?[]const u8 { return switch (self) { .acs_chars => "acsc", .back_tab => "cbt", @@ -604,6 +607,7 @@ pub const String = enum { .exit_am_mode => "rmam", .exit_attribute_mode => "sgr0", .exit_ca_mode => "rmcup", + .exit_bold_mode => null, .exit_delete_mode => "rmdc", .exit_doublewide_mode => "rwidm", .exit_insert_mode => "rmir", @@ -728,6 +732,13 @@ pub const String = enum { // else => "", }; } + + pub fn default(self: String) ?[]const u8 { + return switch (self) { + .exit_bold_mode => "\x1b[22m", + else => null, + }; + } }; test "parse terminfo" {