feat: use terminal cursors for mini mode and overlay input boxes

closes: #80
This commit is contained in:
CJ van den Berg 2024-12-17 19:15:20 +01:00
parent ff0987c108
commit 32d67a3972
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 35 additions and 14 deletions

View file

@ -36,12 +36,18 @@ pub fn Options(context: type) type {
}
if (self.cursor) |cursor| {
const pos: c_int = @intCast(cursor);
const tui_ = tui.current();
if (tui_.config.enable_terminal_cursor) {
const y, const x = self.plane.rel_yx_to_abs(0, pos + 1);
tui_.rdr.cursor_enable(y, x, .default) catch {};
} else {
self.plane.cursor_move_yx(0, pos + 1) catch return false;
var cell = self.plane.cell_init();
_ = self.plane.at_cursor_cell(&cell) catch return false;
cell.set_style(theme.editor_cursor);
_ = self.plane.putc(&cell) catch {};
}
}
return false;
}

View file

@ -843,15 +843,18 @@ pub const Editor = struct {
}
fn render_terminal_cursor(self: *const Self, cursor_: *const Cursor) !void {
const tui_ = tui.current();
if (tui_.is_mini_or_overlay_enabled())
return;
if (self.screen_cursor(cursor_)) |cursor| {
const y, const x = self.plane.rel_yx_to_abs(@intCast(cursor.row), @intCast(cursor.col));
const shape = if (tui.current().input_mode) |mode|
const shape = if (tui_.input_mode) |mode|
mode.cursor_shape
else
.block;
tui.current().rdr.cursor_enable(y, x, tui.translate_cursor_shape(shape)) catch {};
tui_.rdr.cursor_enable(y, x, tui.translate_cursor_shape(shape)) catch {};
} else {
tui.current().rdr.cursor_disable();
tui_.rdr.cursor_disable();
}
}

View file

@ -101,16 +101,22 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
fn render_mini_mode(plane: *Plane, theme: *const Widget.Theme) void {
plane.off_styles(style.italic);
const mini_mode = tui.current().mini_mode orelse return;
const tui_ = tui.current();
const mini_mode = tui_.mini_mode orelse return;
_ = plane.print(" {s}", .{mini_mode.text}) catch {};
if (mini_mode.cursor) |cursor| {
const pos: c_int = @intCast(cursor);
if (tui_.config.enable_terminal_cursor) {
const y, const x = plane.rel_yx_to_abs(0, pos + 1);
tui_.rdr.cursor_enable(y, x, .default) catch {};
} else {
plane.cursor_move_yx(0, pos + 1) catch return;
var cell = plane.cell_init();
_ = plane.at_cursor_cell(&cell) catch return;
cell.set_style(theme.editor_cursor);
_ = plane.putc(&cell) catch {};
}
}
return;
}

View file

@ -585,6 +585,10 @@ pub fn save_config(self: *const Self) !void {
try root.write_config(self.config, self.allocator);
}
pub fn is_mini_or_overlay_enabled(self: *const Self) bool {
return !(self.mini_mode == null or self.input_mode_outer == null);
}
fn enter_overlay_mode(self: *Self, mode: type) command.Result {
command.executeName("disable_fast_scroll", .{}) catch {};
command.executeName("disable_jump_mode", .{}) catch {};
@ -767,6 +771,7 @@ const cmds = struct {
pub const change_file_type_meta = .{ .description = "Change file type" };
pub fn exit_overlay_mode(self: *Self, _: Ctx) Result {
self.rdr.cursor_disable();
if (self.input_mode_outer == null) return;
if (self.input_mode) |*mode| mode.deinit();
self.input_mode = self.input_mode_outer;
@ -818,6 +823,7 @@ const cmds = struct {
}
pub fn exit_mini_mode(self: *Self, _: Ctx) Result {
self.rdr.cursor_disable();
if (self.mini_mode) |_| {} else return;
if (self.input_mode) |*mode| mode.deinit();
self.input_mode = self.input_mode_outer;