From 36d9e13c8184c0cfee1b18dcd9ef7a0ae88a68c7 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 13 Apr 2023 14:25:47 -0700 Subject: [PATCH] refactor: dedup ctrl_mapping function --- termwiz/src/input.rs | 51 +--------------------------------- wezterm-input-types/src/lib.rs | 4 +-- 2 files changed, 2 insertions(+), 53 deletions(-) diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index c8227b001..faef8980a 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -11,6 +11,7 @@ use bitflags::bitflags; #[cfg(feature = "use_serde")] use serde::{Deserialize, Serialize}; use std::fmt::Write; +use wezterm_input_types::ctrl_mapping; pub use wezterm_input_types::Modifiers; @@ -592,56 +593,6 @@ fn is_ambiguous_ascii_ctrl(c: char) -> bool { } } -/// Map c to its Ctrl equivalent. -/// In theory, this mapping is simply translating alpha characters -/// to upper case and then masking them by 0x1f, but xterm inherits -/// some built-in translation from legacy X11 so that are some -/// aliased mappings and a couple that might be technically tied -/// to US keyboard layout (particularly the punctuation characters -/// produced in combination with SHIFT) that may not be 100% -/// the right thing to do here for users with non-US layouts. -fn ctrl_mapping(c: char) -> Option { - // Please also sync with the copy of this function that - // lives in wezterm-input-types :-/ - // FIXME: move this to wezterm-input-types and take a dep on it? - Some(match c { - '@' | '`' | ' ' | '2' => '\x00', - 'A' | 'a' => '\x01', - 'B' | 'b' => '\x02', - 'C' | 'c' => '\x03', - 'D' | 'd' => '\x04', - 'E' | 'e' => '\x05', - 'F' | 'f' => '\x06', - 'G' | 'g' => '\x07', - 'H' | 'h' => '\x08', - 'I' | 'i' => '\x09', - 'J' | 'j' => '\x0a', - 'K' | 'k' => '\x0b', - 'L' | 'l' => '\x0c', - 'M' | 'm' => '\x0d', - 'N' | 'n' => '\x0e', - 'O' | 'o' => '\x0f', - 'P' | 'p' => '\x10', - 'Q' | 'q' => '\x11', - 'R' | 'r' => '\x12', - 'S' | 's' => '\x13', - 'T' | 't' => '\x14', - 'U' | 'u' => '\x15', - 'V' | 'v' => '\x16', - 'W' | 'w' => '\x17', - 'X' | 'x' => '\x18', - 'Y' | 'y' => '\x19', - 'Z' | 'z' => '\x1a', - '[' | '3' | '{' => '\x1b', - '\\' | '4' | '|' => '\x1c', - ']' | '5' | '}' => '\x1d', - '^' | '6' | '~' => '\x1e', - '_' | '7' | '/' => '\x1f', - '8' | '?' => '\x7f', // `Delete` - _ => return None, - }) -} - fn is_ascii(c: char) -> bool { (c as u32) < 0x80 } diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index e7c9bcdac..dca9a7505 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -2108,9 +2108,7 @@ fn us_layout_unshift(c: char) -> char { /// to US keyboard layout (particularly the punctuation characters /// produced in combination with SHIFT) that may not be 100% /// the right thing to do here for users with non-US layouts. -fn ctrl_mapping(c: char) -> Option { - // Please also sync with the copy of this function that - // lives in termwiz :-/ +pub fn ctrl_mapping(c: char) -> Option { Some(match c { '@' | '`' | ' ' | '2' => '\x00', 'A' | 'a' => '\x01',