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