terminal: avoid reading reset memory for preserving prompt

Fixes #1400
This commit is contained in:
Mitchell Hashimoto
2024-01-28 08:58:43 -08:00
parent 06ff385e0c
commit 4b607a07fe

View File

@ -1047,13 +1047,18 @@ fn printWrap(self: *Terminal) !void {
const row = self.screen.getRow(.{ .active = self.screen.cursor.y }); const row = self.screen.getRow(.{ .active = self.screen.cursor.y });
row.setWrapped(true); row.setWrapped(true);
// Get the old semantic prompt so we can extend it to the next
// line. We need to do this before we index() because we may
// modify memory.
const old_prompt = row.getSemanticPrompt();
// Move to the next line // Move to the next line
try self.index(); try self.index();
self.screen.cursor.x = self.scrolling_region.left; self.screen.cursor.x = self.scrolling_region.left;
// New line must inherit semantic prompt of the old line // New line must inherit semantic prompt of the old line
const new_row = self.screen.getRow(.{ .active = self.screen.cursor.y }); const new_row = self.screen.getRow(.{ .active = self.screen.cursor.y });
new_row.setSemanticPrompt(row.getSemanticPrompt()); new_row.setSemanticPrompt(old_prompt);
} }
fn clearWideSpacerHead(self: *Terminal) void { fn clearWideSpacerHead(self: *Terminal) void {
@ -2244,6 +2249,16 @@ test "Terminal: zero-width character at start" {
try testing.expectEqual(@as(usize, 0), t.screen.cursor.x); try testing.expectEqual(@as(usize, 0), t.screen.cursor.x);
} }
// https://github.com/mitchellh/ghostty/issues/1400
test "Terminal: print single very long line" {
var t = try init(testing.allocator, 5, 5);
defer t.deinit(testing.allocator);
// This would crash for issue 1400. So the assertion here is
// that we simply do not crash.
for (0..500) |_| try t.print('x');
}
test "Terminal: print over wide char at 0,0" { test "Terminal: print over wide char at 0,0" {
var t = try init(testing.allocator, 80, 80); var t = try init(testing.allocator, 80, 80);
defer t.deinit(testing.allocator); defer t.deinit(testing.allocator);