This commit is contained in:
Jeeves 2024-04-05 18:03:30 -06:00
parent 5e7c8bdae7
commit 9f0c0d4afb
2 changed files with 67 additions and 10 deletions

View file

@ -14,17 +14,16 @@ pub fn main() !void {
var term = try Terminal.init(allocator); var term = try Terminal.init(allocator);
defer term.deinit(); 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.print("poopoo", .{});
try term.cursorLeft(); try term.cursorLeft();
try term.cursorLeft(); try term.cursorLeft();
try term.print("ee", .{}); 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 { pub const Terminal = struct {
@ -87,9 +86,56 @@ pub const Terminal = struct {
try self.tty.writeAll(formatted); 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 { pub fn cursorLeft(self: *Terminal) !void {
try self.info.writeString(.cursor_left, self.tty.writer(), &[_]u32{}); 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"); pub const Info = @import("terminfo.zig");

View file

@ -190,7 +190,8 @@ strings: std.StringHashMap([]const u8),
/// Writes the formatted sequence to a given writer. /// Writes the formatted sequence to a given writer.
pub fn writeString(self: *Self, string: String, writer: anytype, arguments: []const u32) !void { 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| { if (output) |out| {
var formatted = std.ArrayList(u8).init(self.allocator); var formatted = std.ArrayList(u8).init(self.allocator);
defer formatted.deinit(); defer formatted.deinit();
@ -218,6 +219,7 @@ pub fn writeString(self: *Self, string: String, writer: anytype, arguments: []co
'9' => args[8], '9' => args[8],
else => return error.InvalidFormatChar, else => return error.InvalidFormatChar,
}; };
i += 1;
try fmt.format(stack.writer(), "{d}", .{arg}); try fmt.format(stack.writer(), "{d}", .{arg});
}, },
'P' => {}, 'P' => {},
@ -411,6 +413,7 @@ pub const String = enum {
exit_am_mode, exit_am_mode,
exit_attribute_mode, exit_attribute_mode,
exit_ca_mode, exit_ca_mode,
exit_bold_mode,
exit_delete_mode, exit_delete_mode,
exit_doublewide_mode, exit_doublewide_mode,
exit_insert_mode, exit_insert_mode,
@ -534,7 +537,7 @@ pub const String = enum {
xon_character, xon_character,
zero_motion, zero_motion,
pub fn toCapName(self: String) []const u8 { pub fn toCapName(self: String) ?[]const u8 {
return switch (self) { return switch (self) {
.acs_chars => "acsc", .acs_chars => "acsc",
.back_tab => "cbt", .back_tab => "cbt",
@ -604,6 +607,7 @@ pub const String = enum {
.exit_am_mode => "rmam", .exit_am_mode => "rmam",
.exit_attribute_mode => "sgr0", .exit_attribute_mode => "sgr0",
.exit_ca_mode => "rmcup", .exit_ca_mode => "rmcup",
.exit_bold_mode => null,
.exit_delete_mode => "rmdc", .exit_delete_mode => "rmdc",
.exit_doublewide_mode => "rwidm", .exit_doublewide_mode => "rwidm",
.exit_insert_mode => "rmir", .exit_insert_mode => "rmir",
@ -728,6 +732,13 @@ pub const String = enum {
// else => "", // else => "",
}; };
} }
pub fn default(self: String) ?[]const u8 {
return switch (self) {
.exit_bold_mode => "\x1b[22m",
else => null,
};
}
}; };
test "parse terminfo" { test "parse terminfo" {