diff --git a/examples/text_input.zig b/examples/text_input.zig index cb6df30..d3c56aa 100644 --- a/examples/text_input.zig +++ b/examples/text_input.zig @@ -28,6 +28,9 @@ pub fn main() !void { // Optionally enter the alternate screen try vx.enterAltScreen(); + // We'll adjust the color index every keypress for the border + var color_idx: u8 = 0; + var text_input: TextInput = .{}; // The main event loop. Vaxis provides a thread safe, blocking, buffered @@ -40,6 +43,7 @@ pub fn main() !void { // enum has the fields for those events (ie "key_press", "winsize") switch (event) { .key_press => |key| { + color_idx += 1; text_input.update(.{ .key_press = key }); if (key.codepoint == 'c' and key.mods.ctrl) { break :outer; @@ -61,7 +65,10 @@ pub fn main() !void { win.clear(); const child = win.initChild(win.width / 2 - 20, win.height / 2 - 3, .{ .limit = 40 }, .{ .limit = 3 }); // draw the text_input using a bordered window - text_input.draw(border.all(child, .{})); + const style: vaxis.Style = .{ + .fg = .{ .index = color_idx }, + }; + text_input.draw(border.all(child, style)); // Render the screen try vx.render(); diff --git a/src/ctlseqs.zig b/src/ctlseqs.zig index d75827b..5eee203 100644 --- a/src/ctlseqs.zig +++ b/src/ctlseqs.zig @@ -24,6 +24,9 @@ pub const show_cursor = "\x1b[?25h"; pub const smcup = "\x1b[?1049h"; pub const rmcup = "\x1b[?1049l"; +// sgr reset all +pub const sgr_reset = "\x1b[m"; + // colors pub const fg_base = "\x1b[3{d}m"; pub const fg_bright = "\x1b[9{d}m"; @@ -33,7 +36,7 @@ pub const bg_bright = "\x1b[10{d}m"; pub const fg_reset = "\x1b[39m"; pub const bg_reset = "\x1b[49m"; pub const ul_reset = "\x1b[59m"; -pub const fg_indexed = "\x1b[38;5;{d}m"; +pub const fg_indexed = "\x1b[38:5:{d}m"; pub const bg_indexed = "\x1b[48:5:{d}m"; pub const ul_indexed = "\x1b[58:5:{d}m"; pub const fg_rgb = "\x1b[38:2:{d}:{d}:{d}m"; diff --git a/src/main.zig b/src/main.zig index 502088d..53ff87a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,7 @@ pub const Options = @import("Options.zig"); const cell = @import("cell.zig"); pub const Cell = cell.Cell; +pub const Style = cell.Style; pub const Key = @import("Key.zig"); pub const Winsize = @import("Tty.zig").Winsize; diff --git a/src/vaxis.zig b/src/vaxis.zig index 865df8f..60cf7cb 100644 --- a/src/vaxis.zig +++ b/src/vaxis.zig @@ -196,6 +196,7 @@ pub fn Vaxis(comptime T: type) type { // and then reshow it if needed _ = try tty.write(ctlseqs.hide_cursor); _ = try tty.write(ctlseqs.home); + _ = try tty.write(ctlseqs.sgr_reset); // initialize some variables var reposition: bool = false; @@ -207,7 +208,6 @@ pub fn Vaxis(comptime T: type) type { while (i < self.screen.buf.len) : (i += 1) { const cell = self.screen.buf[i]; defer col += 1; - defer cursor = cell.style; if (col == self.screen.width) { row += 1; col = 0; @@ -223,6 +223,7 @@ pub fn Vaxis(comptime T: type) type { } continue; } + defer cursor = cell.style; // Set this cell in the last frame self.screen_last.buf[i] = cell; diff --git a/src/widgets/TextInput.zig b/src/widgets/TextInput.zig index 3f10803..0dd7498 100644 --- a/src/widgets/TextInput.zig +++ b/src/widgets/TextInput.zig @@ -24,17 +24,19 @@ buffer_idx: usize = 0, pub fn update(self: *TextInput, event: Event) void { switch (event) { .key_press => |key| { - log.info("key : {}", .{key}); if (key.text) |text| { @memcpy(self.buffer[self.buffer_idx .. self.buffer_idx + text.len], text); self.buffer_idx += text.len; + self.cursor_idx += strWidth(text, .full) catch 1; } switch (key.codepoint) { Key.backspace => { // TODO: this only works at the end of the array. Then // again, we don't have any means to move the cursor yet + // This also doesn't work with graphemes yet if (self.buffer_idx == 0) return; self.buffer_idx -= 1; + self.cursor_idx -= 1; }, else => {}, } @@ -57,4 +59,5 @@ pub fn draw(self: *TextInput, win: Window) void { }); col += w; } + win.showCursor(self.cursor_idx, 0); }