Merge pull request #235 from mitchellh/insert-mode

terminal: implement insert mode (mode = 4)
This commit is contained in:
Mitchell Hashimoto
2023-08-07 07:45:50 -07:00
committed by GitHub
3 changed files with 15 additions and 1 deletions

View File

@ -83,6 +83,7 @@ modes: packed struct {
const Self = @This(); const Self = @This();
cursor_keys: bool = false, // 1 cursor_keys: bool = false, // 1
insert: bool = false, // 4
reverse_colors: bool = false, // 5, reverse_colors: bool = false, // 5,
origin: bool = false, // 6 origin: bool = false, // 6
autowrap: bool = true, // 7 autowrap: bool = true, // 7
@ -107,7 +108,7 @@ modes: packed struct {
// We have this here so that we explicitly fail when we change the // We have this here so that we explicitly fail when we change the
// size of modes. The size of modes is NOT particularly important, // size of modes. The size of modes is NOT particularly important,
// we just want to be mentally aware when it happens. // we just want to be mentally aware when it happens.
try std.testing.expectEqual(2, @sizeOf(Self)); try std.testing.expectEqual(4, @sizeOf(Self));
} }
} = .{}, } = .{},
@ -689,6 +690,11 @@ pub fn print(self: *Terminal, c: u21) !void {
if (self.screen.cursor.pending_wrap and self.modes.autowrap) if (self.screen.cursor.pending_wrap and self.modes.autowrap)
try self.printWrap(); try self.printWrap();
// If we have insert mode enabled then we need to handle that. We
// only do insert mode if we're not at the end of the line.
if (self.modes.insert and self.screen.cursor.x + width < self.cols)
self.insertBlanks(width);
switch (width) { switch (width) {
// Single cell is very easy: just write in the cell // Single cell is very easy: just write in the cell
1 => _ = @call(.always_inline, printCell, .{ self, c }), 1 => _ = @call(.always_inline, printCell, .{ self, c }),

View File

@ -62,6 +62,10 @@ pub const Mode = enum(u16) {
/// wide. When unset, resizes to 80 columns. /// wide. When unset, resizes to 80 columns.
@"132_column" = 3, @"132_column" = 3,
/// Insert mode. This mode writes a character and pushes all existing
/// characters to the right. The existing contents are never wrapped.
insert = 4,
/// Reverses the foreground and background colors of all cells. /// Reverses the foreground and background colors of all cells.
reverse_colors = 5, reverse_colors = 5,

View File

@ -1182,6 +1182,10 @@ const StreamHandler = struct {
self.terminal.modes.cursor_keys = enabled; self.terminal.modes.cursor_keys = enabled;
}, },
.insert => {
self.terminal.modes.insert = enabled;
},
.reverse_colors => { .reverse_colors => {
self.terminal.modes.reverse_colors = enabled; self.terminal.modes.reverse_colors = enabled;