Fix local editor raw mode and chrome

This commit is contained in:
slhx agent
2026-06-21 15:22:41 +02:00
parent f75ee1ddb4
commit 629605bcdc
2 changed files with 249 additions and 23 deletions
+41 -4
View File
@@ -235,6 +235,11 @@ fn runLocalEditor(
return;
}
client.enterInsertMode();
var raw_terminal = try RawTerminal.enable(stdin_file.handle);
defer raw_terminal.restore();
var stdin_buffer: [4096]u8 = undefined;
var stdin_reader = stdin_file.readerStreaming(io, &stdin_buffer);
const stdin = &stdin_reader.interface;
@@ -298,6 +303,35 @@ fn persistLocalSave(
last_client_saved.* = client_copy;
}
const RawTerminal = struct {
fd: std.posix.fd_t,
original: std.posix.termios,
fn enable(fd: std.posix.fd_t) !RawTerminal {
const original = try std.posix.tcgetattr(fd);
var raw = original;
raw.iflag.BRKINT = false;
raw.iflag.ICRNL = false;
raw.iflag.INPCK = false;
raw.iflag.ISTRIP = false;
raw.iflag.IXON = false;
raw.oflag.OPOST = false;
raw.cflag.CSIZE = .CS8;
raw.lflag.ECHO = false;
raw.lflag.ICANON = false;
raw.lflag.IEXTEN = false;
raw.lflag.ISIG = false;
raw.cc[@intFromEnum(std.posix.V.MIN)] = 1;
raw.cc[@intFromEnum(std.posix.V.TIME)] = 0;
try std.posix.tcsetattr(fd, .FLUSH, raw);
return .{ .fd = fd, .original = original };
}
fn restore(self: *RawTerminal) void {
std.posix.tcsetattr(self.fd, .FLUSH, self.original) catch {};
}
};
fn readEditorInputAlloc(allocator: std.mem.Allocator, stdin: *std.Io.Reader) !?[]u8 {
const first = stdin.takeByte() catch |err| switch (err) {
error.EndOfStream => return null,
@@ -337,7 +371,7 @@ fn renderLocalFrame(allocator: std.mem.Allocator, io: std.Io, stdout: std.Io.Fil
try stdout.writeStreamingAll(io, if (is_dir)
"\nDirectory browser. Space opens commands; o/Enter opens panel items where available; q quits when clean.\n"
else
"\nEditor. Type to insert; arrows move; Space shows commands; Space w saves; Space q quits when clean.\n");
"\nEditor starts in insert mode. Esc or Space n enters normal mode; Space commands work in normal mode; Space w saves; Space q quits when clean.\n");
}
fn printLocalContext(allocator: std.mem.Allocator, io: std.Io, args: *std.process.Args.Iterator, stdout: std.Io.File) !void {
@@ -514,12 +548,15 @@ test "regular: local editor client exposes save and dirty quit guards" {
try std.testing.expectEqualStrings("hello", edited);
try client.handleInput(" ");
try std.testing.expectEqualStrings("insert_space", client.pendingRailName());
try client.handleInput("n");
try client.handleInput("x");
const with_space = try client.snapshotBytesAlloc(std.testing.allocator);
defer std.testing.allocator.free(with_space);
try std.testing.expectEqualStrings("hello x", with_space);
try client.handleTraceLine("key escape");
try std.testing.expectEqualStrings("normal", client.modeName());
try client.handleInput(" ");
try client.handleInput("w");
try std.testing.expectEqualStrings("hello", try client.saved());
try std.testing.expectEqualStrings("hello x", try client.saved());
try client.handleInput("i");
try client.handleInput("!");