begin string formatting

This commit is contained in:
Jeeves 2024-04-05 17:08:08 -06:00
parent 4d2034d0e7
commit 5e7c8bdae7
2 changed files with 50 additions and 3 deletions

View file

@ -88,7 +88,7 @@ pub const Terminal = struct {
} }
pub fn cursorLeft(self: *Terminal) !void { pub fn cursorLeft(self: *Terminal) !void {
try self.info.writeString(.cursor_left, self.tty.writer()); try self.info.writeString(.cursor_left, self.tty.writer(), &[_]u32{});
// try self.info.cursorLeft(self.tty.writer()); // try self.info.cursorLeft(self.tty.writer());
} }

View file

@ -189,9 +189,56 @@ ints: std.StringHashMap(u32),
strings: std.StringHashMap([]const u8), 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) !void { pub fn writeString(self: *Self, string: String, writer: anytype, arguments: []const u32) !void {
const output = self.strings.get(string.toCapName()); const output = self.strings.get(string.toCapName());
if (output) |o| try writer.writeAll(o); if (output) |out| {
var formatted = std.ArrayList(u8).init(self.allocator);
defer formatted.deinit();
var stack = std.ArrayList(u8).init(self.allocator);
defer stack.deinit();
var args = try self.allocator.dupe(u32, arguments);
defer self.allocator.free(args);
var i: usize = 0;
while (i < out.len) {
if (out[i] == '%') {
switch (out[i + 1]) {
'%' => try formatted.append('%'),
'p' => {
const arg = switch (out[i + 2]) {
'1' => args[0],
'2' => args[1],
'3' => args[2],
'4' => args[3],
'5' => args[4],
'6' => args[5],
'7' => args[6],
'8' => args[7],
'9' => args[8],
else => return error.InvalidFormatChar,
};
try fmt.format(stack.writer(), "{d}", .{arg});
},
'P' => {},
'g' => {},
'i' => {
args[0] += 1;
args[1] += 1;
},
'd' => {
try formatted.appendSlice(stack.items);
stack.clearAndFree();
},
else => return error.InvalidFormatChar,
}
i += 1;
} else try formatted.append(out[i]);
i += 1;
}
try writer.writeAll(formatted.items);
}
} }
pub const Bool = enum { pub const Bool = enum {