From eb653907d67911dfd22d62c26a3de7be5454a7ef Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 2 Feb 2024 12:13:13 -0800 Subject: [PATCH] config: grapheme-width-method sets mode 2027 Fixes #1403 This changes the behavior of `grapheme-width-method = unicode` to change the default state of mode 2027 to true. Prior to this, setting this config would force grapheme clustering regardless of mode 2027. Now, this only sets the default and running TUI programs can disable it if they want. --- src/config/Config.zig | 2 +- src/terminal/Terminal.zig | 12 ++---------- src/termio/Exec.zig | 10 +++++++--- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 4012c5cf8..a4604d465 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -208,7 +208,7 @@ const c = @cImport({ /// reset, this configuration will be used again. /// /// This configuration can be changed at runtime but will not affect existing -/// printed cells. Only new cells will use the new configuration. +/// terminals. Only new terminals will use the new configuration. @"grapheme-width-method": GraphemeWidthMethod = .unicode, /// A named theme to use. The available themes are currently hardcoded to the diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index bd30a9f1e..66643fbd9 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -119,9 +119,6 @@ flags: packed struct { /// then we want to capture the shift key for the mouse protocol /// if the configuration allows it. mouse_shift_capture: enum { null, false, true } = .null, - - /// If true, we perform grapheme clustering even if mode 2027 is disabled. - default_grapheme_cluster: bool = false, } = .{}, /// The event types that can be reported for mouse-related activities. @@ -746,10 +743,7 @@ pub fn print(self: *Terminal, c: u21) !void { // This is MUCH slower than the normal path so the conditional below is // purposely ordered in least-likely to most-likely so we can drop out // as quickly as possible. - if (c > 255 and - (self.modes.get(.grapheme_cluster) or self.flags.default_grapheme_cluster) and - self.screen.cursor.x > 0) - grapheme: { + if (c > 255 and self.modes.get(.grapheme_cluster) and self.screen.cursor.x > 0) grapheme: { const row = self.screen.getRow(.{ .active = self.screen.cursor.y }); // We need the previous cell to determine if we're at a grapheme @@ -891,9 +885,7 @@ pub fn print(self: *Terminal, c: u21) !void { // If we have grapheme clustering enabled, we don't blindly attach // any zero width character to our cells and we instead just ignore // it. - if (self.modes.get(.grapheme_cluster) or - self.flags.default_grapheme_cluster) - return; + if (self.modes.get(.grapheme_cluster)) return; // If we're at cell zero, then this is malformed data and we don't // print anything or even store this. Zero-width characters are ALWAYS diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 1580309b1..e98365fd8 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -135,7 +135,13 @@ pub fn init(alloc: Allocator, opts: termio.Options) !Exec { errdefer term.deinit(alloc); term.default_palette = opts.config.palette; term.color_palette.colors = opts.config.palette; - term.flags.default_grapheme_cluster = opts.config.grapheme_width_method == .unicode; + + // Setup our initial grapheme cluster support if enabled. We use a + // switch to ensure we get a compiler error if more cases are added. + switch (opts.config.grapheme_width_method) { + .unicode => term.modes.set(.grapheme_cluster, true), + .wcswidth => {}, + } // Set the image size limits try term.screen.kitty_images.setLimit(alloc, opts.config.image_storage_limit); @@ -373,8 +379,6 @@ pub fn changeConfig(self: *Exec, config: *DerivedConfig) !void { // - command, working-directory: we never restart the underlying // process so we don't care or need to know about these. - self.terminal.flags.default_grapheme_cluster = config.grapheme_width_method == .unicode; - // Update the default palette. Note this will only apply to new colors drawn // since we decode all palette colors to RGB on usage. self.terminal.default_palette = config.palette;