summaryrefslogtreecommitdiff
path: root/src/terminal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/terminal.zig')
-rw-r--r--src/terminal.zig74
1 files changed, 54 insertions, 20 deletions
diff --git a/src/terminal.zig b/src/terminal.zig
index 3bb1fbda..33ad22e6 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -207,7 +207,7 @@ const LivePalette = struct {
var seed = GColor.default;
for (base, 0..) |rgb, i| seed[i] = rgb;
- self.colors = GColor.generate256Color(seed, .initEmpty(), bg, fg, false);
+ self.colors = GColor.generate256Color(seed, .empty, bg, fg, false);
self.key = key;
return &self.colors;
}
@@ -609,10 +609,10 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {
state.vt = try ghostty_vt.Terminal.init(terminalIo(), gpa, .{
.cols = cols,
.rows = rows,
- .max_scrollback = 16 * 1024 * 1024,
+ .max_scrollback_bytes = 16 * 1024 * 1024,
.kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0,
});
- state.stream = .initAlloc(gpa, .{ .inner = state.vt.vtHandler() });
+ state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa });
state.replay.head = 0;
state.replay.len = 0;
state.reply_len = 0;
@@ -1055,7 +1055,11 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void {
}
};
} else failed = true;
+ // Whether the program cleared the screen (ED 2, a switch of screens);
+ // pardes reads the flag nowhere else.
+ pane.terminal.?.vt.flags.dirty.clear = false;
ingest(pane, bytes);
+ const cleared = pane.terminal.?.vt.flags.dirty.clear;
// RIS can destroy the alternate screen and every pin it owned.
const alive = if (screens.generation(key) == generation) pages else null;
var moved = false;
@@ -1065,7 +1069,15 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void {
if (pin.garbage) pointer_rows_lost = true;
};
};
+ // The program cleared the screen, or the rows the live copy stood on are
+ // gone (pruned, a reset): the copy goes with them, as what the program
+ // drew next. Kept, ghostty would move its rows to the top of what is
+ // left, over the program's new text.
+ var copy_lost = cleared or alive == null;
for (spans[0..nspans]) |span| {
+ if (span.overlay == overlay) if (span.start.pin) |pin| if (alive != null and pin.garbage) {
+ copy_lost = true;
+ };
const start = pinnedRow(alive, span.start);
const rows = @max(1, pinnedRow(alive, span.end) -| start +| 1);
moved = moved or start != span.overlay.row or rows != span.overlay.rows;
@@ -1096,6 +1108,11 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void {
pane.clearPointerSelection(button);
pane.sel[button].state = .none;
};
+ if (copy_lost) if (pane.ovl) |copy| {
+ pane.gpa.free(copy.text);
+ pane.ovl = null;
+ moved = true;
+ };
if (moved) pane.nsel_snap = 0; // Saved regex-preview offsets name the old flat text.
if (failed) {
const message = "terminal edit position reset: out of memory";
@@ -1211,7 +1228,7 @@ pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !Comm
.whole = whole,
};
} else true;
- return .{ .text = try gpa.dupeZ(u8, ""), .whole = whole };
+ return .{ .text = try gpa.dupeSentinel(u8, "", 0), .whole = whole };
}
/// At a prompt with nothing typed on its line: no input left of the
@@ -1330,7 +1347,7 @@ pub fn forwardKey(p: *Pardes, id: usize, key: Key) void {
pub fn enterTty(p: *Pardes, id: usize) void {
const pane = p.panes[id] orelse return;
- const selected = pane.rawPointerText(pane.sel[@intFromEnum(config.select_button)]) != null;
+ const selected = pane.rawPointerText(pane.sel[@backingInt(config.select_button)]) != null;
if (comptime enabled) if (!selected and pane.terminal != null and pane.body.cur_pinned and pane.terminal.?.vt.cursorIsAtPrompt()) handoff: {
const screen = pane.terminal.?.vt.screens.active;
const goff: i32 = @intCast(screen.pages.scrollbar().offset);
@@ -2041,7 +2058,7 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16
const filtered: ?*FilteredColors = if (pane.tty_filter) blk: {
const tz_filter = tracy.zone(@src(), "filterInit");
defer tz_filter.end();
- filtered_storage = FilteredColors.init(p, pane);
+ filtered_storage = FilteredColors.init(p, &pane.terminal.?.vt);
break :blk &filtered_storage;
} else null;
// DECSCNM, read once: the filtered palette folds it in itself, the raw
@@ -2192,7 +2209,9 @@ fn cellStyle(p: *Pardes, ci: ghostty_vt.PageList.Cell, filtered: ?*FilteredColor
return cs;
}
-const FilteredColors = struct {
+/// A program's colours mapped to the theme (Filter): a terminal's cells, or
+/// a +Pager's spans (`vt` null, the program's palette xterm's own).
+pub const FilteredColors = if (enabled) struct {
source: GColor.Palette,
target: *const GColor.Palette,
theme_bg: GColor.RGB,
@@ -2212,16 +2231,18 @@ const FilteredColors = struct {
cache_key: [256]u8 = undefined,
cache_valid: [256]bool = @splat(false),
- fn init(p: *Pardes, pane: *const Pane) FilteredColors {
+ pub fn init(p: *Pardes, vt: ?*const ghostty_vt.Terminal) FilteredColors {
var source = GColor.default;
- for (&source, 0..) |*rgb, i|
- rgb.* = pane.terminal.?.vt.colorForXterm(.{ .palette = @intCast(i) }) orelse rgb.*;
+ if (vt) |t| {
+ for (&source, 0..) |*rgb, i|
+ rgb.* = t.colorForXterm(.{ .palette = @intCast(i) }) orelse rgb.*;
+ }
const theme = p.theme();
var theme_bg = asGhostRgb(theme.bg orelse theme.tag_bg);
var theme_fg = asGhostRgb(theme.fg orelse theme.tag_fg);
- var dynamic_bg = pane.terminal.?.vt.colorForXterm(.{ .dynamic = .background });
- var dynamic_fg = pane.terminal.?.vt.colorForXterm(.{ .dynamic = .foreground });
- if (pane.terminal.?.vt.modes.get(.reverse_colors)) {
+ var dynamic_bg = if (vt) |t| t.colorForXterm(.{ .dynamic = .background }) else null;
+ var dynamic_fg = if (vt) |t| t.colorForXterm(.{ .dynamic = .foreground }) else null;
+ if (vt != null and vt.?.modes.get(.reverse_colors)) {
std.mem.swap(GColor.RGB, &theme_bg, &theme_fg);
std.mem.swap(?GColor.RGB, &dynamic_bg, &dynamic_fg);
}
@@ -2280,6 +2301,18 @@ const FilteredColors = struct {
return asPardesColor(self.keyedRgb(style.bg(cell, &self.source).?));
}
+ /// A +Pager span's colour (pager.zig), as `fg` and `bg` map a cell's.
+ pub fn spanColor(self: *FilteredColors, color: pardes.Color, is_bg: bool) pardes.Color {
+ return switch (color) {
+ .default => color,
+ .index => |idx| if (is_bg) self.bg_for_palette[idx] else self.fg_for_palette[idx],
+ .rgb => |rgb| blk: {
+ const keyed = self.keyedRgb(asGhostRgb(rgb));
+ break :blk asPardesColor(if (is_bg) keyed else self.legible(keyed));
+ },
+ };
+ }
+
fn legible(self: *const FilteredColors, rgb: GColor.RGB) GColor.RGB {
if (contrastOf(luminanceOf(rgb), self.default_bg_luminance) >=
config.tty_filter_min_contrast) return rgb;
@@ -2317,7 +2350,7 @@ const FilteredColors = struct {
self.cache_valid[slot] = true;
return best;
}
-};
+} else struct {};
const channel_luminance: [256]f64 = blk: {
@setEvalBranchQuota(20000);
@@ -2570,7 +2603,7 @@ test "terminal Filter maps the default roles before it maps anything else" {
// the null-page fallback that only the legacy dark theme exercises.
for (0..9) |t| {
p.settings.theme = @intCast(t);
- const stage_one = FilteredColors.init(p, pane);
+ const stage_one = FilteredColors.init(p, &pane.terminal.?.vt);
// `dark` declares no background of its own, which is exactly why the
// resolver reads the tag colours as the fallback rather than `.?`.
const theme = p.theme();
@@ -2584,7 +2617,7 @@ test "terminal Filter maps the default roles before it maps anything else" {
p.settings.theme = 0;
p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]11;#5f5f5f\x1b\\" } });
- var moved = FilteredColors.init(p, pane);
+ var moved = FilteredColors.init(p, &pane.terminal.?.vt);
try testing.expect(!moved.default_bg.eql(moved.theme_bg));
try testing.expect(moved.default_bg.eql(moved.keyedRgb(.{ .r = 0x5f, .g = 0x5f, .b = 0x5f })));
}
@@ -2609,7 +2642,7 @@ test "terminal Filter refuses a foreground that would collapse onto the page" {
for (0..9) |t| {
p.settings.theme = @intCast(t);
- var fc = FilteredColors.init(p, pane);
+ var fc = FilteredColors.init(p, &pane.terminal.?.vt);
const page = asPardesColor(fc.default_bg);
const rescued = asPardesColor(fc.fallback_fg);
_ = frame.reset(.retain_capacity);
@@ -2661,7 +2694,7 @@ test "terminal Filter holds every projected foreground off the page" {
for (0..9) |t| {
p.settings.theme = @intCast(t);
- var fc = FilteredColors.init(p, pane);
+ var fc = FilteredColors.init(p, &pane.terminal.?.vt);
var refused: usize = 0;
for (fc.target, 0..) |projected, key| {
const ink = fc.legible(projected);
@@ -2797,7 +2830,7 @@ pub fn redo(p: *Pardes, pane: *Pane) void {
pane.ensureCursorVisible();
}
-pub fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: [:0]const u8) void {
+pub fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: []const u8) void {
const state: *State = @alignCast(@fieldParentPtr("vt", handler.terminal));
const room = state.reply.len - state.reply_len;
const n = @min(room, data.len);
@@ -2952,7 +2985,8 @@ test "a program that asked for focus reports hears CSI I and CSI O as its pane t
/// over two cells).
inline fn kittyTransmit(comptime extra: []const u8) []const u8 {
return comptime blk: {
- const pixels = [_]u8{ 255, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 255 } ** 2;
+ const row = [_]u8{ 255, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 255 };
+ const pixels = row ++ row;
var buf: [std.base64.standard.Encoder.calcSize(pixels.len)]u8 = undefined;
const b64 = std.base64.standard.Encoder.encode(&buf, &pixels)[0..buf.len].*;
break :blk "\x1b_Ga=T,f=24,s=4,v=2,i=7," ++ extra ++ ";" ++ b64 ++ "\x1b\\";