mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 08:46:08 +03:00
terminal/new: save cursor, protected modes
This commit is contained in:
@ -5334,6 +5334,7 @@ test "Terminal: eraseChars wide character" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// X
|
||||||
test "Terminal: eraseChars protected attributes respected with iso" {
|
test "Terminal: eraseChars protected attributes respected with iso" {
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
var t = try init(alloc, 5, 5);
|
var t = try init(alloc, 5, 5);
|
||||||
@ -5351,6 +5352,7 @@ test "Terminal: eraseChars protected attributes respected with iso" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// X
|
||||||
test "Terminal: eraseChars protected attributes ignored with dec most recent" {
|
test "Terminal: eraseChars protected attributes ignored with dec most recent" {
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
var t = try init(alloc, 5, 5);
|
var t = try init(alloc, 5, 5);
|
||||||
@ -5370,6 +5372,7 @@ test "Terminal: eraseChars protected attributes ignored with dec most recent" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// X
|
||||||
test "Terminal: eraseChars protected attributes ignored with dec set" {
|
test "Terminal: eraseChars protected attributes ignored with dec set" {
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
var t = try init(alloc, 5, 5);
|
var t = try init(alloc, 5, 5);
|
||||||
@ -5592,6 +5595,23 @@ test "Terminal: saveCursor resize" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// X
|
||||||
|
test "Terminal: saveCursor protected pen" {
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
var t = try init(alloc, 10, 5);
|
||||||
|
defer t.deinit(alloc);
|
||||||
|
|
||||||
|
t.setProtectedMode(.iso);
|
||||||
|
try testing.expect(t.screen.cursor.pen.attrs.protected);
|
||||||
|
t.setCursorPos(1, 10);
|
||||||
|
t.saveCursor();
|
||||||
|
t.setProtectedMode(.off);
|
||||||
|
try testing.expect(!t.screen.cursor.pen.attrs.protected);
|
||||||
|
t.restoreCursor();
|
||||||
|
try testing.expect(t.screen.cursor.pen.attrs.protected);
|
||||||
|
}
|
||||||
|
|
||||||
|
// X
|
||||||
test "Terminal: setProtectedMode" {
|
test "Terminal: setProtectedMode" {
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
var t = try init(alloc, 3, 3);
|
var t = try init(alloc, 3, 3);
|
||||||
|
@ -3,6 +3,7 @@ const Screen = @This();
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
|
const ansi = @import("../ansi.zig");
|
||||||
const sgr = @import("../sgr.zig");
|
const sgr = @import("../sgr.zig");
|
||||||
const unicode = @import("../../unicode/main.zig");
|
const unicode = @import("../../unicode/main.zig");
|
||||||
const PageList = @import("PageList.zig");
|
const PageList = @import("PageList.zig");
|
||||||
@ -25,6 +26,13 @@ cursor: Cursor,
|
|||||||
/// The saved cursor
|
/// The saved cursor
|
||||||
saved_cursor: ?SavedCursor = null,
|
saved_cursor: ?SavedCursor = null,
|
||||||
|
|
||||||
|
/// The current or most recent protected mode. Once a protection mode is
|
||||||
|
/// set, this will never become "off" again until the screen is reset.
|
||||||
|
/// The current state of whether protection attributes should be set is
|
||||||
|
/// set on the Cell pen; this is only used to determine the most recent
|
||||||
|
/// protection mode since some sequences such as ECH depend on this.
|
||||||
|
protected_mode: ansi.ProtectedMode = .off,
|
||||||
|
|
||||||
/// The cursor position.
|
/// The cursor position.
|
||||||
pub const Cursor = struct {
|
pub const Cursor = struct {
|
||||||
// The x/y position within the viewport.
|
// The x/y position within the viewport.
|
||||||
@ -35,6 +43,10 @@ pub const Cursor = struct {
|
|||||||
/// next character print will force a soft-wrap.
|
/// next character print will force a soft-wrap.
|
||||||
pending_wrap: bool = false,
|
pending_wrap: bool = false,
|
||||||
|
|
||||||
|
/// The protected mode state of the cursor. If this is true then
|
||||||
|
/// all new characters printed will have the protected state set.
|
||||||
|
protected: bool = false,
|
||||||
|
|
||||||
/// The currently active style. This is the concrete style value
|
/// The currently active style. This is the concrete style value
|
||||||
/// that should be kept up to date. The style ID to use for cell writing
|
/// that should be kept up to date. The style ID to use for cell writing
|
||||||
/// is below.
|
/// is below.
|
||||||
@ -58,6 +70,7 @@ pub const SavedCursor = struct {
|
|||||||
x: size.CellCountInt,
|
x: size.CellCountInt,
|
||||||
y: size.CellCountInt,
|
y: size.CellCountInt,
|
||||||
style: style.Style,
|
style: style.Style,
|
||||||
|
protected: bool,
|
||||||
pending_wrap: bool,
|
pending_wrap: bool,
|
||||||
origin: bool,
|
origin: bool,
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -544,6 +544,7 @@ fn printCell(
|
|||||||
.content = .{ .codepoint = c },
|
.content = .{ .codepoint = c },
|
||||||
.style_id = self.screen.cursor.style_id,
|
.style_id = self.screen.cursor.style_id,
|
||||||
.wide = wide,
|
.wide = wide,
|
||||||
|
.protected = self.screen.cursor.protected,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle the style ref count handling
|
// Handle the style ref count handling
|
||||||
@ -783,6 +784,7 @@ pub fn saveCursor(self: *Terminal) void {
|
|||||||
.x = self.screen.cursor.x,
|
.x = self.screen.cursor.x,
|
||||||
.y = self.screen.cursor.y,
|
.y = self.screen.cursor.y,
|
||||||
.style = self.screen.cursor.style,
|
.style = self.screen.cursor.style,
|
||||||
|
.protected = self.screen.cursor.protected,
|
||||||
.pending_wrap = self.screen.cursor.pending_wrap,
|
.pending_wrap = self.screen.cursor.pending_wrap,
|
||||||
.origin = self.modes.get(.origin),
|
.origin = self.modes.get(.origin),
|
||||||
//TODO
|
//TODO
|
||||||
@ -799,6 +801,7 @@ pub fn restoreCursor(self: *Terminal) !void {
|
|||||||
.x = 0,
|
.x = 0,
|
||||||
.y = 0,
|
.y = 0,
|
||||||
.style = .{},
|
.style = .{},
|
||||||
|
.protected = false,
|
||||||
.pending_wrap = false,
|
.pending_wrap = false,
|
||||||
.origin = false,
|
.origin = false,
|
||||||
// TODO
|
// TODO
|
||||||
@ -814,12 +817,36 @@ pub fn restoreCursor(self: *Terminal) !void {
|
|||||||
//self.screen.charset = saved.charset;
|
//self.screen.charset = saved.charset;
|
||||||
self.modes.set(.origin, saved.origin);
|
self.modes.set(.origin, saved.origin);
|
||||||
self.screen.cursor.pending_wrap = saved.pending_wrap;
|
self.screen.cursor.pending_wrap = saved.pending_wrap;
|
||||||
|
self.screen.cursor.protected = saved.protected;
|
||||||
self.screen.cursorAbsolute(
|
self.screen.cursorAbsolute(
|
||||||
@min(saved.x, self.cols - 1),
|
@min(saved.x, self.cols - 1),
|
||||||
@min(saved.y, self.rows - 1),
|
@min(saved.y, self.rows - 1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the character protection mode for the terminal.
|
||||||
|
pub fn setProtectedMode(self: *Terminal, mode: ansi.ProtectedMode) void {
|
||||||
|
switch (mode) {
|
||||||
|
.off => {
|
||||||
|
self.screen.cursor.protected = false;
|
||||||
|
|
||||||
|
// screen.protected_mode is NEVER reset to ".off" because
|
||||||
|
// logic such as eraseChars depends on knowing what the
|
||||||
|
// _most recent_ mode was.
|
||||||
|
},
|
||||||
|
|
||||||
|
.iso => {
|
||||||
|
self.screen.cursor.protected = true;
|
||||||
|
self.screen.protected_mode = .iso;
|
||||||
|
},
|
||||||
|
|
||||||
|
.dec => {
|
||||||
|
self.screen.cursor.protected = true;
|
||||||
|
self.screen.protected_mode = .dec;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Horizontal tab moves the cursor to the next tabstop, clearing
|
/// Horizontal tab moves the cursor to the next tabstop, clearing
|
||||||
/// the screen to the left the tabstop.
|
/// the screen to the left the tabstop.
|
||||||
pub fn horizontalTab(self: *Terminal) !void {
|
pub fn horizontalTab(self: *Terminal) !void {
|
||||||
@ -1441,6 +1468,12 @@ pub fn deleteChars(self: *Terminal, count: usize) void {
|
|||||||
pub fn eraseChars(self: *Terminal, count_req: usize) void {
|
pub fn eraseChars(self: *Terminal, count_req: usize) void {
|
||||||
const count = @max(count_req, 1);
|
const count = @max(count_req, 1);
|
||||||
|
|
||||||
|
// This resets the soft-wrap of this line
|
||||||
|
self.screen.cursor.page_row.wrap = false;
|
||||||
|
|
||||||
|
// This resets the pending wrap state
|
||||||
|
self.screen.cursor.pending_wrap = false;
|
||||||
|
|
||||||
// Our last index is at most the end of the number of chars we have
|
// Our last index is at most the end of the number of chars we have
|
||||||
// in the current line.
|
// in the current line.
|
||||||
const end = end: {
|
const end = end: {
|
||||||
@ -1459,38 +1492,32 @@ pub fn eraseChars(self: *Terminal, count_req: usize) void {
|
|||||||
|
|
||||||
// Clear the cells
|
// Clear the cells
|
||||||
const cells: [*]Cell = @ptrCast(self.screen.cursor.page_cell);
|
const cells: [*]Cell = @ptrCast(self.screen.cursor.page_cell);
|
||||||
self.blankCells(
|
|
||||||
&self.screen.cursor.page_offset.page.data,
|
|
||||||
self.screen.cursor.page_row,
|
|
||||||
cells[0..end],
|
|
||||||
);
|
|
||||||
|
|
||||||
// This resets the soft-wrap of this line
|
// If we never had a protection mode, then we can assume no cells
|
||||||
self.screen.cursor.page_row.wrap = false;
|
// are protected and go with the fast path. If the last protection
|
||||||
|
// mode was not ISO we also always ignore protection attributes.
|
||||||
|
if (self.screen.protected_mode != .iso) {
|
||||||
|
self.blankCells(
|
||||||
|
&self.screen.cursor.page_offset.page.data,
|
||||||
|
self.screen.cursor.page_row,
|
||||||
|
cells[0..end],
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// This resets the pending wrap state
|
// SLOW PATH
|
||||||
self.screen.cursor.pending_wrap = false;
|
// We had a protection mode at some point. We must go through each
|
||||||
|
// cell and check its protection attribute.
|
||||||
// TODO: protected mode, see below for old logic
|
for (0..end) |x| {
|
||||||
//
|
const cell_multi: [*]Cell = @ptrCast(cells + x);
|
||||||
// const pen: Screen.Cell = .{
|
const cell: *Cell = @ptrCast(&cell_multi[0]);
|
||||||
// .bg = self.screen.cursor.pen.bg,
|
if (cell.protected) continue;
|
||||||
// };
|
self.blankCells(
|
||||||
//
|
&self.screen.cursor.page_offset.page.data,
|
||||||
// // If we never had a protection mode, then we can assume no cells
|
self.screen.cursor.page_row,
|
||||||
// // are protected and go with the fast path. If the last protection
|
cell_multi[0..1],
|
||||||
// // mode was not ISO we also always ignore protection attributes.
|
);
|
||||||
// if (self.screen.protected_mode != .iso) {
|
}
|
||||||
// row.fillSlice(pen, self.screen.cursor.x, end);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // We had a protection mode at some point. We must go through each
|
|
||||||
// // cell and check its protection attribute.
|
|
||||||
// for (self.screen.cursor.x..end) |x| {
|
|
||||||
// const cell = row.getCellPtr(x);
|
|
||||||
// if (cell.attrs.protected) continue;
|
|
||||||
// cell.* = pen;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Blank the given cells. The cells must be long to the given row and page.
|
/// Blank the given cells. The cells must be long to the given row and page.
|
||||||
@ -3560,6 +3587,59 @@ test "Terminal: eraseChars handles refcounted styles" {
|
|||||||
try testing.expectEqual(@as(usize, 0), page.styles.count(page.memory));
|
try testing.expectEqual(@as(usize, 0), page.styles.count(page.memory));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "Terminal: eraseChars protected attributes respected with iso" {
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
var t = try init(alloc, 5, 5);
|
||||||
|
defer t.deinit(alloc);
|
||||||
|
|
||||||
|
t.setProtectedMode(.iso);
|
||||||
|
for ("ABC") |c| try t.print(c);
|
||||||
|
t.setCursorPos(1, 1);
|
||||||
|
t.eraseChars(2);
|
||||||
|
|
||||||
|
{
|
||||||
|
const str = try t.plainString(testing.allocator);
|
||||||
|
defer testing.allocator.free(str);
|
||||||
|
try testing.expectEqualStrings("ABC", str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Terminal: eraseChars protected attributes ignored with dec most recent" {
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
var t = try init(alloc, 5, 5);
|
||||||
|
defer t.deinit(alloc);
|
||||||
|
|
||||||
|
t.setProtectedMode(.iso);
|
||||||
|
for ("ABC") |c| try t.print(c);
|
||||||
|
t.setProtectedMode(.dec);
|
||||||
|
t.setProtectedMode(.off);
|
||||||
|
t.setCursorPos(1, 1);
|
||||||
|
t.eraseChars(2);
|
||||||
|
|
||||||
|
{
|
||||||
|
const str = try t.plainString(testing.allocator);
|
||||||
|
defer testing.allocator.free(str);
|
||||||
|
try testing.expectEqualStrings(" C", str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Terminal: eraseChars protected attributes ignored with dec set" {
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
var t = try init(alloc, 5, 5);
|
||||||
|
defer t.deinit(alloc);
|
||||||
|
|
||||||
|
t.setProtectedMode(.dec);
|
||||||
|
for ("ABC") |c| try t.print(c);
|
||||||
|
t.setCursorPos(1, 1);
|
||||||
|
t.eraseChars(2);
|
||||||
|
|
||||||
|
{
|
||||||
|
const str = try t.plainString(testing.allocator);
|
||||||
|
defer testing.allocator.free(str);
|
||||||
|
try testing.expectEqualStrings(" C", str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test "Terminal: reverseIndex" {
|
test "Terminal: reverseIndex" {
|
||||||
const alloc = testing.allocator;
|
const alloc = testing.allocator;
|
||||||
var t = try init(alloc, 2, 5);
|
var t = try init(alloc, 2, 5);
|
||||||
@ -5586,3 +5666,34 @@ test "Terminal: saveCursor origin mode" {
|
|||||||
try testing.expectEqualStrings("X", str);
|
try testing.expectEqualStrings("X", str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "Terminal: saveCursor protected pen" {
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
var t = try init(alloc, 10, 5);
|
||||||
|
defer t.deinit(alloc);
|
||||||
|
|
||||||
|
t.setProtectedMode(.iso);
|
||||||
|
try testing.expect(t.screen.cursor.protected);
|
||||||
|
t.setCursorPos(1, 10);
|
||||||
|
t.saveCursor();
|
||||||
|
t.setProtectedMode(.off);
|
||||||
|
try testing.expect(!t.screen.cursor.protected);
|
||||||
|
try t.restoreCursor();
|
||||||
|
try testing.expect(t.screen.cursor.protected);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Terminal: setProtectedMode" {
|
||||||
|
const alloc = testing.allocator;
|
||||||
|
var t = try init(alloc, 3, 3);
|
||||||
|
defer t.deinit(alloc);
|
||||||
|
|
||||||
|
try testing.expect(!t.screen.cursor.protected);
|
||||||
|
t.setProtectedMode(.off);
|
||||||
|
try testing.expect(!t.screen.cursor.protected);
|
||||||
|
t.setProtectedMode(.iso);
|
||||||
|
try testing.expect(t.screen.cursor.protected);
|
||||||
|
t.setProtectedMode(.dec);
|
||||||
|
try testing.expect(t.screen.cursor.protected);
|
||||||
|
t.setProtectedMode(.off);
|
||||||
|
try testing.expect(!t.screen.cursor.protected);
|
||||||
|
}
|
||||||
|
@ -537,7 +537,10 @@ pub const Cell = packed struct(u64) {
|
|||||||
/// and spacer properties of a cell.
|
/// and spacer properties of a cell.
|
||||||
wide: Wide = .narrow,
|
wide: Wide = .narrow,
|
||||||
|
|
||||||
_padding: u20 = 0,
|
/// Whether this was written with the protection flag set.
|
||||||
|
protected: bool = false,
|
||||||
|
|
||||||
|
_padding: u19 = 0,
|
||||||
|
|
||||||
pub const ContentTag = enum(u2) {
|
pub const ContentTag = enum(u2) {
|
||||||
/// A single codepoint, could be zero to be empty cell.
|
/// A single codepoint, could be zero to be empty cell.
|
||||||
|
Reference in New Issue
Block a user