stylistic nitpicks

This commit is contained in:
Mitchell Hashimoto
2024-08-10 11:03:56 -07:00
parent 8c44137711
commit ccf62a4960
6 changed files with 47 additions and 37 deletions

View File

@ -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,

View File

@ -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.
};
};

View File

@ -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,
};

View File

@ -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");

View File

@ -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;
}
};

View File

@ -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 }),