1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 22:01:47 +03:00

refactor: move encode_modifiers to Modifiers::encode_xterm

This commit is contained in:
Wez Furlong 2023-04-15 19:23:21 -07:00
parent a562cb5a92
commit 2b9ee388e3
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
2 changed files with 23 additions and 23 deletions

View File

@ -433,7 +433,7 @@ impl KeyCode {
|| mods.contains(Modifiers::SHIFT)
|| mods.contains(Modifiers::CTRL)
{
write!(buf, "{}1;{}{}", CSI, 1 + encode_modifiers(mods), c)?;
write!(buf, "{}1;{}{}", CSI, 1 + mods.encode_xterm(), c)?;
} else {
write!(buf, "{}{}", csi_or_ss3, c)?;
}
@ -452,7 +452,7 @@ impl KeyCode {
|| mods.contains(Modifiers::SHIFT)
|| mods.contains(Modifiers::CTRL)
{
write!(buf, "\x1b[{};{}~", c, 1 + encode_modifiers(mods))?;
write!(buf, "\x1b[{};{}~", c, 1 + mods.encode_xterm())?;
} else {
write!(buf, "\x1b[{}~", c)?;
}
@ -481,7 +481,7 @@ impl KeyCode {
4 => 'S',
_ => unreachable!("wat?"),
};
write!(buf, "\x1b[1;{}{code}", 1 + encode_modifiers(mods))?;
write!(buf, "\x1b[1;{}{code}", 1 + mods.encode_xterm())?;
} else {
// Higher numbered F-keys using CSI instead of SS3.
let intro = match n {
@ -499,7 +499,7 @@ impl KeyCode {
12 => "\x1b[24",
_ => bail!("unhandled fkey number {}", n),
};
let encoded_mods = encode_modifiers(mods);
let encoded_mods = mods.encode_xterm();
if encoded_mods == 0 {
// If no modifiers are held, don't send the modifier
// sequence, as the modifier encoding is a CSI-u extension.
@ -519,7 +519,7 @@ impl KeyCode {
_ => unreachable!(),
};
let encoded_mods = encode_modifiers(mods);
let encoded_mods = mods.encode_xterm();
if encoded_mods == 0 {
// If no modifiers are held, don't send the modifier
// sequence, as the modifier encoding is a CSI-u extension.
@ -541,12 +541,12 @@ impl KeyCode {
_ => unreachable!(),
};
let encoded_mods = encode_modifiers(mods);
let encoded_mods = mods.encode_xterm();
if encoded_mods == 0 {
// If no modifiers are held, don't send the modifier
write!(buf, "{}{}", CSI, c)?;
} else {
write!(buf, "{}1;{}{}", CSI, 1 + encode_modifiers(mods), c)?;
write!(buf, "{}1;{}{}", CSI, 1 + encoded_mods, c)?;
}
}
@ -567,20 +567,6 @@ impl KeyCode {
}
}
fn encode_modifiers(mods: Modifiers) -> u8 {
let mut number = 0;
if mods.contains(Modifiers::SHIFT) {
number |= 1;
}
if mods.contains(Modifiers::ALT) {
number |= 2;
}
if mods.contains(Modifiers::CTRL) {
number |= 4;
}
number
}
/// characters that when masked for CTRL could be an ascii control character
/// or could be a key that a user legitimately wants to process in their
/// terminal application
@ -602,7 +588,7 @@ fn csi_u_encode(
modes: &KeyCodeEncodeModes,
) -> Result<()> {
if modes.encoding == KeyboardEncoding::CsiU && is_ascii(c) {
write!(buf, "\x1b[{};{}u", c as u32, 1 + encode_modifiers(mods))?;
write!(buf, "\x1b[{};{}u", c as u32, 1 + mods.encode_xterm())?;
return Ok(());
}
@ -612,7 +598,7 @@ fn csi_u_encode(
// Exclude well-known keys from modifyOtherKeys mode 1
}
(c, Some(_)) => {
write!(buf, "\x1b[27;{};{}~", 1 + encode_modifiers(mods), c as u32)?;
write!(buf, "\x1b[27;{};{}~", 1 + mods.encode_xterm(), c as u32)?;
return Ok(());
}
_ => {}

View File

@ -527,6 +527,20 @@ pub struct ModifierToStringArgs<'a> {
}
impl Modifiers {
pub fn encode_xterm(self) -> u8 {
let mut number = 0;
if self.contains(Self::SHIFT) {
number |= 1;
}
if self.contains(Self::ALT) {
number |= 2;
}
if self.contains(Self::CTRL) {
number |= 4;
}
number
}
pub fn to_string_with_separator(&self, args: ModifierToStringArgs) -> String {
let mut s = String::new();
if args.want_none && *self == Self::NONE {