render: fix when we set current cursor to new style, update examples

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-22 11:19:28 -06:00
parent 70e0cafafe
commit c9c704d4a7
5 changed files with 19 additions and 4 deletions

View file

@ -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();

View file

@ -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";

View file

@ -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;

View file

@ -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;

View file

@ -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);
}