From ccf62a4960ba8662bc1a55beabecac845a027c8f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 10 Aug 2024 11:03:56 -0700 Subject: [PATCH] stylistic nitpicks --- src/Surface.zig | 23 +++++++++++++-------- src/apprt/surface.zig | 2 ++ src/terminal/csi.zig | 15 +++++++++++--- src/terminal/main.zig | 3 ++- src/terminal/stream.zig | 39 ++++++++++++++--------------------- src/termio/stream_handler.zig | 2 +- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 69b185de0..6aa39142f 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -714,17 +714,22 @@ pub fn handleMessage(self: *Surface, msg: Message) !void { }, .report_title => |style| { - const title: ?[:0]const u8 = if (@hasDecl(apprt.runtime.Surface, "getTitle")) - self.rt_surface.getTitle() - else - // If the apprt does not implement getTitle, report a - // blank title. - ""; - - const data = switch (style) { - .csi_21_t => try std.fmt.allocPrint(self.alloc, "\x1b]l{s}\x1b\\", .{title orelse ""}), + const title: ?[:0]const u8 = title: { + if (!@hasDecl(apprt.runtime.Surface, "getTitle")) break :title null; + break :title self.rt_surface.getTitle(); }; + const data = switch (style) { + .csi_21_t => try std.fmt.allocPrint( + self.alloc, + "\x1b]l{s}\x1b\\", + .{title orelse ""}, + ), + }; + + // We always use an allocating message because we don't know + // the length of the title and this isn't a performance critical + // path. self.io.queueMessage(.{ .write_alloc = .{ .alloc = self.alloc, diff --git a/src/apprt/surface.zig b/src/apprt/surface.zig index aefe6fb97..b05da88ff 100644 --- a/src/apprt/surface.zig +++ b/src/apprt/surface.zig @@ -63,6 +63,8 @@ pub const Message = union(enum) { pub const ReportTitleStyle = enum { csi_21_t, + + // This enum is a placeholder for future title styles. }; }; diff --git a/src/terminal/csi.zig b/src/terminal/csi.zig index 877f5986e..d2ee4da46 100644 --- a/src/terminal/csi.zig +++ b/src/terminal/csi.zig @@ -1,4 +1,4 @@ -// Modes for the ED CSI command. +/// Modes for the ED CSI command. pub const EraseDisplay = enum(u8) { below = 0, above = 1, @@ -10,7 +10,7 @@ pub const EraseDisplay = enum(u8) { scroll_complete = 22, }; -// Modes for the EL CSI command. +/// Modes for the EL CSI command. pub const EraseLine = enum(u8) { right = 0, left = 1, @@ -22,7 +22,7 @@ pub const EraseLine = enum(u8) { _, }; -// Modes for the TBC (tab clear) command. +/// Modes for the TBC (tab clear) command. pub const TabClear = enum(u8) { current = 0, all = 3, @@ -31,3 +31,12 @@ pub const TabClear = enum(u8) { // user-generated. _, }; + +/// Style formats for terminal size reports. +pub const SizeReportStyle = enum { + // XTWINOPS + csi_14_t, + csi_16_t, + csi_18_t, + csi_21_t, +}; diff --git a/src/terminal/main.zig b/src/terminal/main.zig index 8d31e6916..cdf80a54b 100644 --- a/src/terminal/main.zig +++ b/src/terminal/main.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); pub usingnamespace @import("sanitize.zig"); const charsets = @import("charsets.zig"); -pub const stream = @import("stream.zig"); +const stream = @import("stream.zig"); const ansi = @import("ansi.zig"); const csi = @import("csi.zig"); const hyperlink = @import("hyperlink.zig"); @@ -38,6 +38,7 @@ pub const Pin = PageList.Pin; pub const Screen = @import("Screen.zig"); pub const ScreenType = Terminal.ScreenType; pub const Selection = @import("Selection.zig"); +pub const SizeReportStyle = csi.SizeReportStyle; pub const StringMap = @import("StringMap.zig"); pub const Style = style.Style; pub const Terminal = @import("Terminal.zig"); diff --git a/src/terminal/stream.zig b/src/terminal/stream.zig index bfb6a4206..6adfa3280 100644 --- a/src/terminal/stream.zig +++ b/src/terminal/stream.zig @@ -24,13 +24,6 @@ const log = std.log.scoped(.stream); /// do something else. const debug = false; -pub const ReportStyle = enum { - csi_14_t, - csi_16_t, - csi_18_t, - csi_21_t, -}; - /// Returns a type that can process a stream of tty control characters. /// This will call various callback functions on type T. Type T only has to /// implement the callbacks it cares about; any unimplemented callbacks will @@ -1123,8 +1116,8 @@ pub fn Stream(comptime Handler: type) type { switch (input.params[0]) { 14 => if (input.params.len == 1) { // report the text area size in pixels - if (@hasDecl(T, "sendReport")) { - self.handler.sendReport(.csi_14_t); + if (@hasDecl(T, "sendSizeReport")) { + self.handler.sendSizeReport(.csi_14_t); } else log.warn( "ignoring unimplemented CSI 14 t", .{}, @@ -1135,8 +1128,8 @@ pub fn Stream(comptime Handler: type) type { ), 16 => if (input.params.len == 1) { // report cell size in pixels - if (@hasDecl(T, "sendReport")) { - self.handler.sendReport(.csi_16_t); + if (@hasDecl(T, "sendSizeReport")) { + self.handler.sendSizeReport(.csi_16_t); } else log.warn( "ignoring unimplemented CSI 16 t", .{}, @@ -1147,8 +1140,8 @@ pub fn Stream(comptime Handler: type) type { ), 18 => if (input.params.len == 1) { // report screen size in characters - if (@hasDecl(T, "sendReport")) { - self.handler.sendReport(.csi_18_t); + if (@hasDecl(T, "sendSizeReport")) { + self.handler.sendSizeReport(.csi_18_t); } else log.warn( "ignoring unimplemented CSI 18 t", .{}, @@ -1159,8 +1152,8 @@ pub fn Stream(comptime Handler: type) type { ), 21 => if (input.params.len == 1) { // report window title - if (@hasDecl(T, "sendReport")) { - self.handler.sendReport(.csi_21_t); + if (@hasDecl(T, "sendSizeReport")) { + self.handler.sendSizeReport(.csi_21_t); } else log.warn( "ignoring unimplemented CSI 21 t", .{}, @@ -2126,9 +2119,9 @@ test "stream: csi param too long" { test "stream: send report with CSI t" { const H = struct { - style: ?ReportStyle = null, + style: ?csi.SizeReportStyle = null, - pub fn sendReport(self: *@This(), style: ReportStyle) void { + pub fn sendSizeReport(self: *@This(), style: csi.SizeReportStyle) void { self.style = style; } }; @@ -2136,23 +2129,23 @@ test "stream: send report with CSI t" { var s: Stream(H) = .{ .handler = .{} }; try s.nextSlice("\x1b[14t"); - try testing.expectEqual(ReportStyle.csi_14_t, s.handler.style); + try testing.expectEqual(csi.SizeReportStyle.csi_14_t, s.handler.style); try s.nextSlice("\x1b[16t"); - try testing.expectEqual(ReportStyle.csi_16_t, s.handler.style); + try testing.expectEqual(csi.SizeReportStyle.csi_16_t, s.handler.style); try s.nextSlice("\x1b[18t"); - try testing.expectEqual(ReportStyle.csi_18_t, s.handler.style); + try testing.expectEqual(csi.SizeReportStyle.csi_18_t, s.handler.style); try s.nextSlice("\x1b[21t"); - try testing.expectEqual(ReportStyle.csi_21_t, s.handler.style); + try testing.expectEqual(csi.SizeReportStyle.csi_21_t, s.handler.style); } test "stream: invalid CSI t" { const H = struct { - style: ?ReportStyle = null, + style: ?csi.SizeReportStyle = null, - pub fn sendReport(self: *@This(), style: ReportStyle) void { + pub fn sendSizeReport(self: *@This(), style: csi.SizeReportStyle) void { self.style = style; } }; diff --git a/src/termio/stream_handler.zig b/src/termio/stream_handler.zig index 48f4858f2..648efb6fb 100644 --- a/src/termio/stream_handler.zig +++ b/src/termio/stream_handler.zig @@ -1261,7 +1261,7 @@ pub const StreamHandler = struct { } /// Send a report to the pty. - pub fn sendReport(self: *StreamHandler, style: terminal.stream.ReportStyle) void { + pub fn sendSizeReport(self: *StreamHandler, style: terminal.SizeReportStyle) void { switch (style) { .csi_14_t => self.messageWriter(.{ .size_report = .csi_14_t }), .csi_16_t => self.messageWriter(.{ .size_report = .csi_16_t }),