summaryrefslogtreecommitdiff
path: root/src/terminal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/terminal.zig')
-rw-r--r--src/terminal.zig261
1 files changed, 83 insertions, 178 deletions
diff --git a/src/terminal.zig b/src/terminal.zig
index 7cb429d9..9aa97f71 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -14,19 +14,18 @@ const Pane = panes.Pane;
const EditText = panes.EditText;
const File = panes.File;
-pub const enabled = pardes.terminal_panes;
-const ghostty_vt = if (enabled) @import("ghostty-vt") else struct {};
+const ghostty_vt = @import("ghostty-vt");
-pub const history_max = if (enabled) 64 else 8;
+pub const history_max = 64;
-pub const VtSlot = if (enabled) ghostty_vt.Terminal else void;
+pub const VtSlot = ghostty_vt.Terminal;
/// A cell of an emulator's pages, as `getCell` reads it (src/echo.zig).
-pub const PageCell = if (enabled) ghostty_vt.PageList.Cell else void;
+pub const PageCell = ghostty_vt.PageList.Cell;
/// Kitty graphics are drawn where pixel geometry is (pdf_enabled, as for
/// image panes' native placements).
-pub const images_enabled = enabled and pardes.pdf_enabled;
+pub const images_enabled = pardes.pdf_enabled;
pub const TerminalImages = if (images_enabled) @import("TerminalImages.zig") else void;
-pub const StreamSlot = if (enabled) ghostty_vt.Stream(MarkedHandler) else void;
+pub const StreamSlot = ghostty_vt.Stream(MarkedHandler);
/// The `aid` pardes's injected bash and fish marks carry (host_io.zig).
pub const prompt_aid = "pardes";
@@ -36,7 +35,7 @@ pub const prompt_aid = "pardes";
/// it, and /pane/<n>/pty/run waits on exactly that. Only marks carrying
/// pardes's own `aid` move it, so fish's native marks, a nested shell's
/// and a stray `133;D` in some file being printed all pass it by.
-pub const MarkedHandler = if (enabled) struct {
+pub const MarkedHandler = struct {
inner: ghostty_vt.TerminalStream.Handler,
phase: enum { none, prompt, input, running } = .none,
/// Counts commands started (C at the input phase).
@@ -142,7 +141,7 @@ pub const MarkedHandler = if (enabled) struct {
pub fn deinit(self: *@This()) void {
self.inner.deinit();
}
-} else void;
+};
pub const State = struct {
/// What the emulator and this struct are allocated with: the core's,
@@ -162,7 +161,7 @@ pub const State = struct {
reply_dropped: u32 = 0,
/// The grid cell the program last heard the mouse at, so motion is
/// reported once per cell.
- mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {},
+ mouse_cell: ?ghostty_vt.Coordinate = null,
/// The kitty graphics its program placed, as drawn (TerminalImages.zig).
images: TerminalImages = if (images_enabled) .{} else {},
/// Counts the emulator's writes (output, a resize, its view scrolled):
@@ -187,7 +186,6 @@ pub const State = struct {
/// a replica's terminal is the authority's screen state from phase 5
/// on (docs/next-daemon.md).
pub fn digest(s: *const State, h: *pardes.walk.Hasher) void {
- if (comptime !enabled) return;
// Under screen-state terminals the emulator is the authority's, and
// a replica's is what the last delta made it: compared as screens
// (test/replicas.zig), not step by step.
@@ -197,7 +195,6 @@ pub const State = struct {
/// `view`: where its view is too (a replica's speculation may move it).
pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher, view: bool) void {
- if (comptime !enabled) return;
const screen = s.vt.screens.active;
const sb = screen.pages.scrollbar();
const cur = screen.cursor;
@@ -244,7 +241,6 @@ pub const State = struct {
/// its program had begun and not ended is not kept: the snapshot is at
/// the ground state.
pub fn dumpWrite(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer) !void {
- if (comptime !enabled) return;
const h = &s.stream.handler;
const parts: Parts = .{
.reply = s.reply[0..s.reply_len],
@@ -274,7 +270,6 @@ pub const State = struct {
/// What `dumpRead` made, freed: a load that failed after it.
pub fn dumpFree(s: *State, gpa: std.mem.Allocator) void {
- if (comptime !enabled) return;
if (comptime images_enabled) s.images.deinit(gpa);
s.stream.deinit();
s.vt.deinit(gpa);
@@ -282,10 +277,6 @@ pub const State = struct {
/// `dumpWrite`'s back into `out`, a new State, as `create` makes one.
pub fn dumpRead(out: *State, gpa: std.mem.Allocator, bytes: []const u8) !void {
- if (comptime !enabled) {
- out.* = .{ .vt = {}, .stream = {} };
- return;
- }
if (bytes.len < 8) return error.EndOfDump;
const n = std.math.cast(usize, std.mem.bytesToValue(u64, bytes[0..8])) orelse return error.EndOfDump;
if (bytes.len - 8 < n) return error.EndOfDump;
@@ -352,7 +343,7 @@ const FilterPaletteKey = struct {
base: [16]GColor.RGB,
};
-pub const FilterPalette = if (enabled) LivePalette else struct {};
+pub const FilterPalette = LivePalette;
const LivePalette = struct {
key: ?FilterPaletteKey = null,
@@ -428,7 +419,6 @@ pub const PendingCommand = struct {
};
pub fn armShellSpawn(pane: *Pane) void {
- if (comptime !enabled) return;
std.debug.assert(pane.pending_command.bytes.len == 0);
pane.pending_command.wait = .spawn;
}
@@ -438,7 +428,6 @@ pub fn armShellSpawn(pane: *Pane) void {
/// ponytail: of several lines queued for a shell not up yet, only the last
/// is remembered, as the first of them.
pub fn noteCommand(pane: *Pane, line: []const u8) void {
- if (comptime !enabled) return;
const state = pane.terminal orelse return;
const word = std.mem.trim(u8, line, " \t")[0 .. std.mem.indexOfAny(u8, std.mem.trim(u8, line, " \t"), " \t") orelse std.mem.trim(u8, line, " \t").len];
var sent: @typeInfo(@FieldType(Pane, "sent_command")).optional.child = .{ .seq = state.stream.handler.started +% 1 };
@@ -449,7 +438,6 @@ pub fn noteCommand(pane: *Pane, line: []const u8) void {
/// Whether the command pardes last typed here is running now.
pub fn commandRunning(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
const sent = pane.sent_command orelse return false;
const marks = &state.stream.handler;
@@ -525,7 +513,6 @@ pub fn terminalIo() std.Io {
pub const Scrollbar = struct { total: usize = 0, offset: usize = 0, len: usize = 0 };
pub fn scrollbar(pane: *const Pane) Scrollbar {
- if (comptime !enabled) return .{};
const state = pane.terminal orelse return .{};
const sb = state.vt.screens.active.pages.scrollbar();
return .{ .total = sb.total, .offset = sb.offset, .len = sb.len };
@@ -541,14 +528,12 @@ pub fn gridOffset(pane: *const Pane) i32 {
pub const GridCursor = struct { x: u16 = 0, y: u16 = 0 };
pub fn gridCursor(pane: *const Pane) GridCursor {
- if (comptime !enabled) return .{};
const state = pane.terminal orelse return .{};
const cur = state.vt.screens.active.cursor;
return .{ .x = @intCast(cur.x), .y = @intCast(cur.y) };
}
pub fn visibleCursor(pane: *const Pane) ?GridCursor {
- if (comptime !enabled) return null;
const state = pane.terminal orelse return null;
if (!state.vt.modes.get(.cursor_visible)) return null;
const cur = state.vt.screens.active.cursor;
@@ -563,7 +548,6 @@ pub fn visibleCursor(pane: *const Pane) ?GridCursor {
/// many as keep its cursor's row in sight, as tmux pans a bigger window in
/// a smaller client. 0 in the view that sizes it, and in one tall enough.
pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 {
- if (comptime !enabled) return 0;
if (pane.sized_by == p.view or pane.collapsed or !pane.isTerminal()) return 0;
const shown = r.h -| pane.tag_rows;
if (shown == 0 or shown >= pane.rows) return 0;
@@ -584,7 +568,6 @@ pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 {
pub const marks_len = 1 + 4 * 3 + 2 + 4 + 4;
pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 {
- if (comptime !enabled) return &.{};
const m = &state.stream.handler;
var w: std.Io.Writer = .fixed(buf);
w.writeByte(@intFromEnum(m.phase)) catch unreachable;
@@ -597,7 +580,6 @@ pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 {
/// A replica's terminal takes its authority's marks (`marksOf`).
pub fn takeMarks(state: *State, bytes: []const u8) void {
- if (comptime !enabled) return;
if (bytes.len != marks_len) return;
const m = &state.stream.handler;
m.phase = @enumFromInt(bytes[0]);
@@ -646,7 +628,6 @@ fn viewOnly(comptime why: []const u8) bool {
/// history, or null following its output (a replica's rollback copy,
/// which holds no emulator, keeps it so).
pub fn viewOf(pane: *const Pane) ?usize {
- if (comptime !enabled) return null;
const state = pane.terminal orelse return null;
const pages = &state.vt.screens.active.pages;
if (pages.viewport == .active) return null;
@@ -654,14 +635,12 @@ pub fn viewOf(pane: *const Pane) ?usize {
}
pub fn viewTo(pane: *Pane, at: ?usize) void {
- if (comptime !enabled) return;
const state = pane.terminal orelse return;
state.vt.screens.active.scroll(if (at) |row| .{ .row = row } else .active);
}
/// Move the emulator's viewport by `delta` shell rows (negative scrolls back).
pub fn scrollGrid(pane: *Pane, delta: i32) void {
- if (comptime !enabled) return;
if (viewOnly("scroll a terminal")) return;
const state = pane.terminal orelse return;
state.vt.screens.active.scroll(.{ .delta_row = delta });
@@ -670,14 +649,12 @@ pub fn scrollGrid(pane: *Pane, delta: i32) void {
/// Is the viewport already showing the last row of output?
pub fn atBottom(pane: *const Pane) bool {
- if (comptime !enabled) return true;
const sb = scrollbar(pane);
return sb.offset + sb.len >= sb.total;
}
/// Snap the viewport back onto live output.
pub fn followOutput(pane: *Pane) void {
- if (comptime !enabled) return;
if (viewOnly("follow a terminal")) return;
const state = pane.terminal orelse return;
const moved = state.vt.screens.active.pages.viewport != .active;
@@ -688,7 +665,6 @@ pub fn followOutput(pane: *Pane) void {
/// Reflow the grid. A failed reflow keeps the grid it had rather than dropping
/// a scrollback; the next resize retries with the same numbers.
pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void {
- if (comptime !enabled) return;
if (authorityOnly("resize a terminal")) return;
const state = pane.terminal orelse return;
if (cols != pane.cols) for (0..pane.sel.len) |button| {
@@ -711,7 +687,6 @@ pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void {
/// The sizes steps gave terminals (`resizeGrid`), made: between units, on an
/// authority with screen-state replicas (replica.Peers.unit).
pub fn resizeDeferred(p: *Pardes) void {
- if (comptime !enabled) return;
for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (state.resize_to) |size| {
state.resize_to = null;
state.changes +%= 1;
@@ -722,14 +697,12 @@ pub fn resizeDeferred(p: *Pardes) void {
/// DECSET 2004: the program wants its pastes bracketed.
pub fn bracketedPaste(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return state.vt.modes.get(.bracketed_paste);
}
/// The program tracks the mouse itself, so a click in its body is its event.
pub fn reportsMouse(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return state.vt.flags.mouse_event != .none;
}
@@ -740,25 +713,17 @@ pub fn reportsMouse(pane: *const Pane) bool {
/// spaces, then the text goes inside the brackets when the program set
/// 2004, else with its newlines as Returns. `text` is rewritten in place.
pub fn encodePaste(pane: *const Pane, text: []u8) [3][]const u8 {
- if (comptime !enabled) {
- for (text) |*ch| if (ch.* == '\n') {
- ch.* = '\r';
- };
- return .{ "", text, "" };
- }
return ghostty_vt.input.encodePaste(text, .{ .bracketed = bracketedPaste(pane) });
}
/// DECSET 1004: the program wants to hear when it gains and loses focus.
pub fn reportsFocus(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return state.vt.modes.get(.focus_event);
}
/// ...every move of it, a button down or not (1003).
pub fn tracksAllMotion(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return state.vt.flags.mouse_event == .any;
}
@@ -767,7 +732,6 @@ pub fn tracksAllMotion(pane: *const Pane) bool {
/// on the alternate screen, which has no scrollback, with alternate scroll
/// (DECSET 1007, on unless turned off) asking for arrow keys instead.
pub fn ownsWheel(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return reportsMouse(pane) or
(state.vt.screens.active_key == .alternate and state.vt.modes.get(.mouse_alternate_scroll));
@@ -785,7 +749,6 @@ pub const MouseAction = enum { press, release, motion };
/// grid is reported clamped to its edge, motion only when the cell
/// changed. Empty when the program does not hear of it.
pub fn mouseBytes(pane: *Pane, buf: *[64]u8, action: MouseAction, button: ?MouseButton, mods: MouseMods, col: i32, row: i32, any_pressed: bool) []const u8 {
- if (comptime !enabled) return &.{};
const state = pane.terminal orelse return &.{};
const input = ghostty_vt.input;
var writer = std.Io.Writer.fixed(buf);
@@ -828,7 +791,6 @@ pub fn mouseBytes(pane: *Pane, buf: *[64]u8, action: MouseAction, button: ?Mouse
/// alternate screen, an arrow key in the cursor keys' mode (none for a
/// sideways turn). Null when the wheel is not the program's (`ownsWheel`).
pub fn wheelBytes(pane: *Pane, buf: *[64]u8, wheel: MouseButton, col: u16, row: u16, mods: MouseMods) ?[]const u8 {
- if (comptime !enabled) return null;
const state = pane.terminal orelse return null;
if (!ownsWheel(pane)) return null;
const m = &state.vt.modes;
@@ -845,7 +807,6 @@ pub fn wheelBytes(pane: *Pane, buf: *[64]u8, wheel: MouseButton, col: u16, row:
/// are joined again (ghostty's wrap flags, as pty/run's output is), and
/// the last line ends with a newline.
pub fn screenTextAlloc(pane: *Pane, gpa: std.mem.Allocator) ![]const u8 {
- if (comptime !enabled) return &.{};
var out: std.Io.Writer.Allocating = .init(gpa);
errdefer out.deinit();
try writeScreenText(pane, &out.writer);
@@ -853,7 +814,6 @@ pub fn screenTextAlloc(pane: *Pane, gpa: std.mem.Allocator) ![]const u8 {
}
fn writeScreenText(pane: *Pane, w: *std.Io.Writer) !void {
- if (comptime !enabled) return;
const state = pane.terminal orelse return;
const screen = state.vt.screens.active;
const br = screen.pages.getBottomRight(.screen) orelse return;
@@ -896,7 +856,6 @@ const TeeWriter = struct {
/// How long `screenTextAlloc` would be, counted without keeping it: the
/// length a terminal's body reads as, which its ctl line reports.
pub fn screenTextLen(pane: *Pane) usize {
- if (comptime !enabled) return 0;
var buf: [256]u8 = undefined;
var counting: std.Io.Writer.Discarding = .init(&buf);
writeScreenText(pane, &counting.writer) catch return 0;
@@ -905,7 +864,6 @@ pub fn screenTextLen(pane: *Pane) usize {
/// Release the emulator's heap. The Pane allocation itself is the core's.
pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void {
- if (comptime !enabled) return;
// A pane with none (a file's, a picker's, a preview's) closes in a
// speculation as anywhere.
const state = pane.terminal orelse return;
@@ -941,7 +899,6 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {
if (authorityOnly("open a terminal")) return error.OutOfMemory;
const pane = try createDoc(gpa, cols, rows);
errdefer gpa.destroy(pane);
- if (comptime !enabled) return pane;
const own = emulatorGpa(gpa);
const state = try own.create(State);
errdefer own.destroy(state);
@@ -972,7 +929,6 @@ pub const restored_banner = "\x1b[0m\r\n\x1b[2m\u{2500}\u{2500} restored history
/// history marked off from what the new shell prints, and the pane waiting
/// for it like any new terminal (a line queued for the old one goes to it).
pub fn reviveShell(pane: *Pane) void {
- if (comptime !enabled) return;
if (pane.terminal == null) return;
// The primary screen, keys, cursor and mouse as a shell starts with
// them, the cursor where it was (a soft reset, or leaving a primary
@@ -1004,7 +960,7 @@ test "replay ownership is terminal-only and construction rolls back on allocatio
const pane = if (terminal) try create(gpa, 40, 12) else try createDoc(gpa, 40, 12);
defer gpa.destroy(pane);
defer deinitEmulator(pane, gpa);
- try std.testing.expectEqual(terminal and enabled, pane.terminal != null);
+ try std.testing.expectEqual(terminal, pane.terminal != null);
}
};
try std.testing.expect(@sizeOf(Pane) < 128 * 1024);
@@ -1013,7 +969,6 @@ test "replay ownership is terminal-only and construction rolls back on allocatio
}
test "a terminal made fresh has no size waiting: one never resized keeps its grid when a unit is cut" {
- if (comptime !enabled) return error.SkipZigTest;
// Fresh memory as a Debug build's allocator hands it out: 0xAA. A field
// `create` left unset read a size of 43690x43690 (fixer 2's, 2026-10-30).
const Filling = struct {
@@ -1072,7 +1027,6 @@ test "document construction allocates only the pane and has inert terminal defau
}
test "terminal state keeps parser fragments and sends emulator replies through its owner" {
- if (comptime !enabled) return error.SkipZigTest;
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 });
defer p.deinit();
while (p.nextEffect()) |_| {}
@@ -1096,7 +1050,6 @@ test "terminal state keeps parser fragments and sends emulator replies through i
}
test "a terminal's dump restores the cells it drew, its history and where its view was" {
- if (comptime !enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 });
defer p.deinit();
@@ -1124,7 +1077,7 @@ test "a terminal's dump restores the cells it drew, its history and where its vi
}
test "a restored command pane comes back finished, its command not run again" {
- if (comptime !enabled or pardes.isolated) return error.SkipZigTest;
+ if (comptime pardes.isolated) return error.SkipZigTest;
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 });
defer p.deinit();
@@ -1154,7 +1107,7 @@ test "a restored command pane comes back finished, its command not run again" {
}
test "a restored terminal keeps its history above a marker and gets a shell where it was" {
- if (comptime !enabled or pardes.isolated) return error.SkipZigTest;
+ if (comptime pardes.isolated) return error.SkipZigTest;
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 });
defer p.deinit();
@@ -1191,7 +1144,6 @@ const positions_lost = "terminal edit position reset: out of memory";
/// follow it only when the body (possibly parked under a tag edit) is raw.
pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void {
// There are no pty reads at all without an emulator to parse them into.
- if (comptime !enabled) return;
if (pane.terminal == null) return;
if (authorityOnly("a terminal's output")) return;
const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or
@@ -1488,7 +1440,6 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) bool {
}
test "a position that cannot follow the output for want of memory is said on the message row, and logged" {
- if (comptime !enabled) return error.SkipZigTest;
var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{});
const p = try Pardes.init(failing.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
@@ -1509,7 +1460,6 @@ test "a position that cannot follow the output for want of memory is said on the
}
test "raw terminal output needs no position tracking allocations" {
- if (comptime !enabled) return error.SkipZigTest;
var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{});
const p = try Pardes.init(failing.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 });
defer p.deinit();
@@ -1527,7 +1477,6 @@ test "raw terminal output needs no position tracking allocations" {
/// A full-screen program has the emulator on its alternate screen.
pub fn onAlternateScreen(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return state.vt.screens.active_key == .alternate;
}
@@ -1539,7 +1488,6 @@ pub fn onAlternateScreen(pane: *const Pane) bool {
/// been typed first, and an `ls` then would go into it. A shell pardes
/// cannot mark has no first prompt to find, and is never greeted.
pub fn greeting(pane: *const Pane) enum { wait, now, never } {
- if (comptime !enabled) return .never;
if (pane.fs.unmarked) return .never;
// Text typed into the edit buffer that stands in for the prompt row.
if (pane.ovl) |o| if (o.text.len > 0) return .never;
@@ -1557,7 +1505,6 @@ pub fn greeting(pane: *const Pane) enum { wait, now, never } {
/// there (htop, `sig KILL`) left the emulator and the shell then drew its
/// prompt, marks and all. A full-screen program draws no prompt marks.
pub fn promptInputReady(pane: *const Pane) bool {
- if (comptime !enabled) return false;
const state = pane.terminal orelse return false;
return state.vt.screens.active.cursor.semantic_content == .input;
}
@@ -1580,7 +1527,6 @@ pub const CommandOutput = struct {
/// The shell exited in the middle of a command (its `exit 3`): no end mark
/// will come, so the command's output ends where the cursor is.
pub fn endOutputHere(pane: *Pane) void {
- if (comptime !enabled) return;
const state = pane.terminal orelse return;
const marks = &state.stream.handler;
if (marks.phase == .running and marks.out_end == null) marks.pinCursor(&marks.out_end, true);
@@ -1589,7 +1535,7 @@ pub fn endOutputHere(pane: *Pane) void {
pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !CommandOutput {
// Unless there is text to read, whether the nothing it printed is the
// whole of it.
- const whole = if (comptime enabled) printed: {
+ const whole = printed: {
const state = pane.terminal orelse break :printed true;
const marks = &state.stream.handler;
const start = marks.out_start orelse break :printed false;
@@ -1615,7 +1561,7 @@ pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !Comm
.text = try screen.selectionString(gpa, .{ .sel = ghostty_vt.Selection.init(from, last, false) }),
.whole = whole,
};
- } else true;
+ };
return .{ .text = try gpa.dupeSentinel(u8, "", 0), .whole = whole };
}
@@ -1624,7 +1570,6 @@ pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !Comm
/// cursor). Nothing further right counts: that is where fish draws a
/// right prompt, and fish suggests nothing on an empty line.
pub fn promptInputEmpty(pane: *const Pane) bool {
- if (comptime !enabled) return false;
if (!promptInputReady(pane)) return false;
const cursor = &pane.terminal.?.vt.screens.active.cursor;
const row = cursor.page_pin.cells(.all);
@@ -1636,107 +1581,75 @@ pub fn promptInputEmpty(pane: *const Pane) bool {
/// Encode one key for the program that owns a raw terminal and queue its pty
/// write, respecting the application's keyboard protocol and terminal modes.
pub fn forwardKey(p: *Pardes, id: usize, key: Key) void {
- if (comptime enabled) {
- const pane = p.panes[id] orelse return;
- const state = pane.terminal orelse return;
- const input = ghostty_vt.input;
- const function_keys = comptime keys: {
- var keys: [Key.f25 - Key.f1 + 1]input.Key = undefined;
- for (&keys, 1..) |*k, n| k.* = @field(input.Key, std.fmt.comptimePrint("f{d}", .{n}));
- break :keys keys;
- };
- const physical: input.Key = switch (key.cp) {
- Key.enter => .enter,
- Key.backspace => .backspace,
- Key.tab => .tab,
- Key.escape => .escape,
- Key.up => .arrow_up,
- Key.down => .arrow_down,
- Key.left => .arrow_left,
- Key.right => .arrow_right,
- Key.home => .home,
- Key.end => .end,
- Key.page_up => .page_up,
- Key.page_down => .page_down,
- Key.delete => .delete,
- Key.insert => .insert,
- Key.print_screen => .print_screen,
- Key.scroll_lock => .scroll_lock,
- Key.pause => .pause,
- Key.menu => .context_menu,
- Key.f1...Key.f25 => function_keys[key.cp - Key.f1],
- else => if (key.cp < 128) input.Key.fromASCII(@intCast(key.cp)) orelse .unidentified else .unidentified,
- };
- var utf8: [4]u8 = undefined;
- const text_cp = if (key.shift and key.cp >= 'a' and key.cp <= 'z') key.cp - 'a' + 'A' else key.cp;
- // A key with no text of its own types its code point only when that
- // is a character: the kitty keyboard protocol numbers function and
- // modifier keys in the private use area (F3 is U+E016), and a shell
- // was typed those as junk.
- const text = if (key.text.len > 0) key.text else if (key.cp >= 32 and key.cp < Key.up and key.cp != Key.backspace and !(key.cp >= 0xE000 and key.cp <= 0xF8FF))
- utf8[0 .. std.unicode.utf8Encode(text_cp, &utf8) catch return]
- else
- "";
- var buffer: [256]u8 = undefined;
- var writer = std.Io.Writer.fixed(&buffer);
- input.encodeKey(&writer, .{
- .key = physical,
- .mods = .{ .ctrl = key.ctrl, .alt = key.alt, .shift = key.shift },
- .consumed_mods = .{ .shift = key.text.len > 0 and key.shift },
- .utf8 = text,
- .unshifted_codepoint = if (key.cp >= 'A' and key.cp <= 'Z') key.cp - 'A' + 'a' else if (key.cp < Key.up) key.cp else 0,
- }, options: {
- var opts = input.KeyEncodeOptions.fromTerminal(&state.vt);
- // fromTerminal cannot know this one and says so, defaulting to
- // "option is not alt" — right for an app that lets macOS do the
- // unicode translation and hands the encoder the composed text.
- // Every pardes shell resolves the modifier itself and hands the
- // core the BASE key with the alt bit and no text at all (see
- // PardesView.keyDown, and app.mjs doing the same with the same
- // two bits), so by the time this runs alt already means alt.
- // Left at the default, a macOS build drops the ESC prefix on
- // the floor and forwards Alt-n to the child as a bare `n`.
- opts.macos_option_as_alt = .true;
- break :options opts;
- }) catch return;
- if (writer.end > 0) p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(writer.buffered()) } });
- return;
- }
- var control: [1]u8 = undefined;
- const bytes: ?[]const u8 = blk: {
- if (key.ctrl) {
- if (key.cp >= 'a' and key.cp <= 'z') {
- control[0] = @intCast(key.cp - 0x60);
- break :blk control[0..1];
- }
- // ASCII @, A-Z, [, \, ], ^ and _ are one contiguous control range.
- if (key.cp >= '@' and key.cp <= '_') {
- control[0] = @intCast(key.cp - 0x40);
- break :blk control[0..1];
- }
- }
- if (key.text.len > 0) break :blk key.text;
- break :blk switch (key.cp) {
- Key.enter => "\r",
- Key.backspace => "\x7f",
- Key.tab => "\t",
- Key.escape => "\x1b",
- Key.up => "\x1b[A",
- Key.down => "\x1b[B",
- Key.right => "\x1b[C",
- Key.left => "\x1b[D",
- Key.delete => "\x1b[3~",
- else => null,
- };
+ const pane = p.panes[id] orelse return;
+ const state = pane.terminal orelse return;
+ const input = ghostty_vt.input;
+ const function_keys = comptime keys: {
+ var keys: [Key.f25 - Key.f1 + 1]input.Key = undefined;
+ for (&keys, 1..) |*k, n| k.* = @field(input.Key, std.fmt.comptimePrint("f{d}", .{n}));
+ break :keys keys;
};
- if (bytes) |encoded|
- p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(encoded) } });
+ const physical: input.Key = switch (key.cp) {
+ Key.enter => .enter,
+ Key.backspace => .backspace,
+ Key.tab => .tab,
+ Key.escape => .escape,
+ Key.up => .arrow_up,
+ Key.down => .arrow_down,
+ Key.left => .arrow_left,
+ Key.right => .arrow_right,
+ Key.home => .home,
+ Key.end => .end,
+ Key.page_up => .page_up,
+ Key.page_down => .page_down,
+ Key.delete => .delete,
+ Key.insert => .insert,
+ Key.print_screen => .print_screen,
+ Key.scroll_lock => .scroll_lock,
+ Key.pause => .pause,
+ Key.menu => .context_menu,
+ Key.f1...Key.f25 => function_keys[key.cp - Key.f1],
+ else => if (key.cp < 128) input.Key.fromASCII(@intCast(key.cp)) orelse .unidentified else .unidentified,
+ };
+ var utf8: [4]u8 = undefined;
+ const text_cp = if (key.shift and key.cp >= 'a' and key.cp <= 'z') key.cp - 'a' + 'A' else key.cp;
+ // A key with no text of its own types its code point only when that
+ // is a character: the kitty keyboard protocol numbers function and
+ // modifier keys in the private use area (F3 is U+E016), and a shell
+ // was typed those as junk.
+ const text = if (key.text.len > 0) key.text else if (key.cp >= 32 and key.cp < Key.up and key.cp != Key.backspace and !(key.cp >= 0xE000 and key.cp <= 0xF8FF))
+ utf8[0 .. std.unicode.utf8Encode(text_cp, &utf8) catch return]
+ else
+ "";
+ var buffer: [256]u8 = undefined;
+ var writer = std.Io.Writer.fixed(&buffer);
+ input.encodeKey(&writer, .{
+ .key = physical,
+ .mods = .{ .ctrl = key.ctrl, .alt = key.alt, .shift = key.shift },
+ .consumed_mods = .{ .shift = key.text.len > 0 and key.shift },
+ .utf8 = text,
+ .unshifted_codepoint = if (key.cp >= 'A' and key.cp <= 'Z') key.cp - 'A' + 'a' else if (key.cp < Key.up) key.cp else 0,
+ }, options: {
+ var opts = input.KeyEncodeOptions.fromTerminal(&state.vt);
+ // fromTerminal cannot know this one and says so, defaulting to
+ // "option is not alt" — right for an app that lets macOS do the
+ // unicode translation and hands the encoder the composed text.
+ // Every pardes shell resolves the modifier itself and hands the
+ // core the BASE key with the alt bit and no text at all (see
+ // PardesView.keyDown, and app.mjs doing the same with the same
+ // two bits), so by the time this runs alt already means alt.
+ // Left at the default, a macOS build drops the ESC prefix on
+ // the floor and forwards Alt-n to the child as a bare `n`.
+ opts.macos_option_as_alt = .true;
+ break :options opts;
+ }) catch return;
+ if (writer.end > 0) p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(writer.buffered()) } });
}
pub fn enterTty(p: *Pardes, id: usize) void {
const pane = p.panes[id] orelse return;
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: {
+ 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);
const vp_row = pane.gridRow(pane.body.cur_row) - goff;
@@ -1759,7 +1672,7 @@ pub fn enterTty(p: *Pardes, id: usize) void {
const moves = screen.promptClickMove(click_pin);
for (0..moves.left) |_| p.emitWrite(id, "\x1b[D");
for (0..moves.right) |_| p.emitWrite(id, "\x1b[C");
- };
+ }
pane.body.mode = .tty;
followOutput(pane); // entering raw mode is a request for the live shell
@@ -1845,7 +1758,6 @@ fn promptCut(pin: ghostty_vt.Pin) PromptCut {
}
pub fn promptPrefixBytes(pane: *Pane, row: i32, raw: []const u8) usize {
- if (comptime !enabled) return 0;
if (row < 0) return 0;
const state = pane.terminal orelse return 0;
const pin = state.vt.screens.active.pages.pin(.{ .screen = .{ .x = 0, .y = @intCast(row) } }) orelse return 0;
@@ -1896,7 +1808,6 @@ fn dumpedBytes(pin: ghostty_vt.Pin, cell: *const ghostty_vt.Cell) usize {
}
pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 {
- if (comptime !enabled) return &empty_grid;
if (pane.terminal == null) return &empty_grid;
pardes.tape.touchTerminal();
const c = &p.shell_rows;
@@ -1924,7 +1835,6 @@ pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 {
}
test "rows asked for after a pane's cached rows were dropped, before the sweep, leak nothing" {
- if (comptime !enabled) return error.SkipZigTest;
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const pane = p.panes[0].?;
@@ -2109,7 +2019,7 @@ fn buildRows(alloc: std.mem.Allocator, p: *Pardes, pane: *Pane) !Rows {
}
pub fn bodyText(arena: std.mem.Allocator, pane: *Pane) ![]const u8 {
- const vp: []const []const u8 = if (comptime !enabled) &.{} else vp: {
+ const vp: []const []const u8 = vp: {
const state = pane.terminal orelse break :vp &.{};
const screen = state.vt.screens.active;
var tl = screen.pages.getTopLeft(.viewport);
@@ -2351,7 +2261,6 @@ const Anchor = struct {
pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void {
// No emulator, no ANSI cells: the whole pass — and the 256-colour theme
// projection behind it — is compiled out.
- if (comptime !enabled) return;
if (pane.terminal == null) return;
const body_y = p.bodyTop(pane, r);
var filtered_storage: FilteredColors = undefined;
@@ -2490,7 +2399,7 @@ fn cellAt(pages: *const ghostty_vt.PageList, row: ghostty_vt.Pin, x: u16) ?ghost
// A cell's underline styles are SGR's, in SGR's order (`off` its `none`): a
// cast takes one to the other (cellStyle, pager.zig).
comptime {
- if (enabled) for (@typeInfo(@FieldType(pardes.CellStyle, "ul")).@"enum".field_names, 0..) |name, i|
+ for (@typeInfo(@FieldType(pardes.CellStyle, "ul")).@"enum".field_names, 0..) |name, i|
if (@intFromEnum(@field(ghostty_vt.sgr.Attribute.Underline, if (std.mem.eql(u8, name, "off")) "none" else name)) != i) @compileError("a cell's underline styles are not SGR's");
}
@@ -2521,7 +2430,7 @@ fn cellStyle(p: *Pardes, ci: ghostty_vt.PageList.Cell, filtered: ?*FilteredColor
/// 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 {
+pub const FilteredColors = struct {
source: GColor.Palette,
target: *const GColor.Palette,
theme_bg: GColor.RGB,
@@ -2660,7 +2569,7 @@ pub const FilteredColors = if (enabled) struct {
self.cache_valid[slot] = true;
return best;
}
-} else struct {};
+};
const channel_luminance: [256]f64 = blk: {
@setEvalBranchQuota(20000);
@@ -3106,7 +3015,6 @@ fn ghostColor(p: *Pardes, color: ghostty_vt.Style.Color, is_bg: bool) pardes.Col
pub fn padOutputBelowEdits(p: *Pardes, id: usize) void {
// Nothing to pad away from: with no emulator there is no prompt and no
// child whose output could land on top of the edit buffer.
- if (comptime !enabled) return;
const pane = p.panes[id] orelse return;
const o = pane.ovl orelse return;
if (!pane.isTerminal()) return;
@@ -3256,7 +3164,6 @@ fn ptyWritesByPane(p: *Pardes, out: *std.ArrayList(u8)) ![]const u8 {
}
test "a program that asked for focus reports hears CSI I and CSI O as its pane takes and gives up the keyboard, and as the window does" {
- if (comptime !enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .cols = 90, .rows = 30, .shells = 3 });
defer p.deinit();
@@ -3397,7 +3304,6 @@ test "ED 3 takes away a kitty placement whose row it erased: it never comes back
}
test "a finished command's output erased by a later ED 3 is not called whole, and no rows stand in for it" {
- if (comptime !enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 6 });
defer p.deinit();
@@ -3420,7 +3326,6 @@ test "a finished command's output erased by a later ED 3 is not called whole, an
}
test "a selection whose row ED 3 erased goes; one on the screen stays" {
- if (comptime !enabled) return error.SkipZigTest;
const gpa = std.testing.allocator;
for ([_]bool{ true, false }) |in_history| {
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 6 });