terminal: add easy debug mode for stream debugging

This commit is contained in:
Mitchell Hashimoto
2024-06-09 21:22:23 -04:00
parent c69dc0e0e1
commit e2b4ee99e5

View File

@ -16,6 +16,14 @@ const MouseShape = @import("mouse_shape.zig").MouseShape;
const log = std.log.scoped(.stream); const log = std.log.scoped(.stream);
/// Flip this to true when you want verbose debug output for
/// debugging terminal stream issues. In addition to louder
/// output this will also disable the SIMD optimizations in
/// order to make it easier to see every byte. So if you're
/// debugging an issue in the SIMD code then you'll need to
/// do something else.
const debug = false;
/// Returns a type that can process a stream of tty control characters. /// 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 /// This will call various callback functions on type T. Type T only has to
/// implement the callbacks it cares about; any unimplemented callbacks will /// implement the callbacks it cares about; any unimplemented callbacks will
@ -48,6 +56,12 @@ pub fn Stream(comptime Handler: type) type {
/// Process a string of characters. /// Process a string of characters.
pub fn nextSlice(self: *Self, input: []const u8) !void { pub fn nextSlice(self: *Self, input: []const u8) !void {
// Debug mode disables the SIMD optimizations
if (comptime debug) {
for (input) |c| try self.next(c);
return;
}
// This is the maximum number of codepoints we can decode // This is the maximum number of codepoints we can decode
// at one time for this function call. This is somewhat arbitrary // at one time for this function call. This is somewhat arbitrary
// so if someone can demonstrate a better number then we can switch. // so if someone can demonstrate a better number then we can switch.
@ -261,10 +275,7 @@ pub fn Stream(comptime Handler: type) type {
const actions = self.parser.next(c); const actions = self.parser.next(c);
for (actions) |action_opt| { for (actions) |action_opt| {
const action = action_opt orelse continue; const action = action_opt orelse continue;
if (comptime debug) log.info("action: {}", .{action});
// if (action != .print) {
// log.info("action: {}", .{action});
// }
// If this handler handles everything manually then we do nothing // If this handler handles everything manually then we do nothing
// if it can be processed. // if it can be processed.
@ -317,7 +328,7 @@ pub fn Stream(comptime Handler: type) type {
pub fn execute(self: *Self, c: u8) !void { pub fn execute(self: *Self, c: u8) !void {
const c0: ansi.C0 = @enumFromInt(c); const c0: ansi.C0 = @enumFromInt(c);
// log.info("execute: {}", .{c0}); if (comptime debug) log.info("execute: {}", .{c0});
switch (c0) { switch (c0) {
// We ignore SOH/STX: https://github.com/microsoft/terminal/issues/10786 // We ignore SOH/STX: https://github.com/microsoft/terminal/issues/10786
.NUL, .SOH, .STX => {}, .NUL, .SOH, .STX => {},