allow specifying key bindings in wezterm.toml
refs: https://github.com/wez/wezterm/issues/32
This commit is contained in:
1 parent
45596f44be
commit
3b94cddf8e
3 files changed
+242
-3
No files matched your search
+229
-1
@@ -1,13 +1,17 @@
|
||||
//! Configuration for the gui portion of the terminal
|
||||
|
||||
use crate::font::FontSystemSelection;
|
||||
use crate::frontend::guicommon::host::KeyAssignment;
|
||||
use crate::frontend::FrontEndSelection;
|
||||
use crate::get_shell;
|
||||
use failure::{bail, err_msg, format_err, Error};
|
||||
use failure::{bail, err_msg, format_err, Error, Fallible};
|
||||
use lazy_static::lazy_static;
|
||||
use portable_pty::{CommandBuilder, PtySystemSelection};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use serde_derive::*;
|
||||
use std;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs;
|
||||
use std::io::prelude::*;
|
||||
@@ -15,6 +19,7 @@ use std::path::PathBuf;
|
||||
use term;
|
||||
use term::color::RgbColor;
|
||||
use termwiz::hyperlink;
|
||||
use termwiz::input::{KeyCode, Modifiers};
|
||||
use toml;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
@@ -75,6 +80,213 @@ pub struct Config {
|
||||
/// When using the MuxServer, this specifies the path to the unix
|
||||
/// domain socket to use to communicate with the mux server.
|
||||
pub mux_server_unix_domain_socket_path: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub keys: Vec<Key>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct Key {
|
||||
#[serde(deserialize_with = "de_keycode")]
|
||||
pub key: KeyCode,
|
||||
#[serde(deserialize_with = "de_modifiers")]
|
||||
pub mods: Modifiers,
|
||||
pub action: KeyAction,
|
||||
pub arg: Option<String>,
|
||||
}
|
||||
|
||||
impl std::convert::TryInto<KeyAssignment> for &Key {
|
||||
type Error = Error;
|
||||
fn try_into(self) -> Result<KeyAssignment, Error> {
|
||||
Ok(match self.action {
|
||||
KeyAction::SpawnTab => KeyAssignment::SpawnTab,
|
||||
KeyAction::SpawnWindow => KeyAssignment::SpawnWindow,
|
||||
KeyAction::ToggleFullScreen => KeyAssignment::ToggleFullScreen,
|
||||
KeyAction::Copy => KeyAssignment::Copy,
|
||||
KeyAction::Paste => KeyAssignment::Paste,
|
||||
KeyAction::IncreaseFontSize => KeyAssignment::IncreaseFontSize,
|
||||
KeyAction::DecreaseFontSize => KeyAssignment::DecreaseFontSize,
|
||||
KeyAction::ResetFontSize => KeyAssignment::ResetFontSize,
|
||||
KeyAction::Nop => KeyAssignment::Nop,
|
||||
KeyAction::ActivateTab => KeyAssignment::ActivateTab(
|
||||
self.arg
|
||||
.as_ref()
|
||||
.ok_or_else(|| format_err!("missing arg for {:?}", self))?
|
||||
.parse()?,
|
||||
),
|
||||
KeyAction::ActivateTabRelative => KeyAssignment::ActivateTabRelative(
|
||||
self.arg
|
||||
.as_ref()
|
||||
.ok_or_else(|| format_err!("missing arg for {:?}", self))?
|
||||
.parse()?,
|
||||
),
|
||||
KeyAction::SendString => KeyAssignment::SendString(
|
||||
self.arg
|
||||
.as_ref()
|
||||
.ok_or_else(|| format_err!("missing arg for {:?}", self))?
|
||||
.to_owned(),
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub enum KeyAction {
|
||||
SpawnTab,
|
||||
SpawnWindow,
|
||||
ToggleFullScreen,
|
||||
Copy,
|
||||
Paste,
|
||||
ActivateTabRelative,
|
||||
IncreaseFontSize,
|
||||
DecreaseFontSize,
|
||||
ResetFontSize,
|
||||
ActivateTab,
|
||||
SendString,
|
||||
Nop,
|
||||
}
|
||||
|
||||
fn de_keycode<'de, D>(deserializer: D) -> Result<KeyCode, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
|
||||
macro_rules! m {
|
||||
($($val:ident),* $(,)?) => {
|
||||
$(
|
||||
if s == stringify!($val) {
|
||||
return Ok(KeyCode::$val);
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
m!(
|
||||
Hyper,
|
||||
Super,
|
||||
Meta,
|
||||
Cancel,
|
||||
Backspace,
|
||||
Tab,
|
||||
Clear,
|
||||
Enter,
|
||||
Shift,
|
||||
Escape,
|
||||
LeftShift,
|
||||
RightShift,
|
||||
Control,
|
||||
LeftControl,
|
||||
RightControl,
|
||||
Alt,
|
||||
LeftAlt,
|
||||
RightAlt,
|
||||
Menu,
|
||||
LeftMenu,
|
||||
RightMenu,
|
||||
Pause,
|
||||
CapsLock,
|
||||
PageUp,
|
||||
PageDown,
|
||||
End,
|
||||
Home,
|
||||
LeftArrow,
|
||||
RightArrow,
|
||||
UpArrow,
|
||||
DownArrow,
|
||||
Select,
|
||||
Print,
|
||||
Execute,
|
||||
PrintScreen,
|
||||
Insert,
|
||||
Delete,
|
||||
Help,
|
||||
LeftWindows,
|
||||
RightWindows,
|
||||
Applications,
|
||||
Sleep,
|
||||
Numpad0,
|
||||
Numpad1,
|
||||
Numpad2,
|
||||
Numpad3,
|
||||
Numpad4,
|
||||
Numpad5,
|
||||
Numpad6,
|
||||
Numpad7,
|
||||
Numpad8,
|
||||
Numpad9,
|
||||
Multiply,
|
||||
Add,
|
||||
Separator,
|
||||
Subtract,
|
||||
Decimal,
|
||||
Divide,
|
||||
NumLock,
|
||||
ScrollLock,
|
||||
BrowserBack,
|
||||
BrowserForward,
|
||||
BrowserRefresh,
|
||||
BrowserStop,
|
||||
BrowserSearch,
|
||||
BrowserFavorites,
|
||||
BrowserHome,
|
||||
VolumeMute,
|
||||
VolumeDown,
|
||||
VolumeUp,
|
||||
MediaNextTrack,
|
||||
MediaPrevTrack,
|
||||
MediaStop,
|
||||
MediaPlayPause,
|
||||
ApplicationLeftArrow,
|
||||
ApplicationRightArrow,
|
||||
ApplicationUpArrow,
|
||||
ApplicationDownArrow,
|
||||
);
|
||||
|
||||
if s.len() > 1 && s.starts_with('F') {
|
||||
let num: u8 = s[1..].parse().map_err(|_| {
|
||||
serde::de::Error::custom(format!(
|
||||
"expected F<NUMBER> function key string, got: {}",
|
||||
s
|
||||
))
|
||||
})?;
|
||||
return Ok(KeyCode::Function(num));
|
||||
}
|
||||
|
||||
let chars: Vec<char> = s.chars().collect();
|
||||
if chars.len() == 1 {
|
||||
Ok(KeyCode::Char(chars[0]))
|
||||
} else {
|
||||
Err(serde::de::Error::custom(format!(
|
||||
"invalid KeyCode string {}",
|
||||
s
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
fn de_modifiers<'de, D>(deserializer: D) -> Result<Modifiers, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
let mut mods = Modifiers::NONE;
|
||||
for ele in s.split('|') {
|
||||
if ele == "SHIFT" {
|
||||
mods |= Modifiers::SHIFT;
|
||||
} else if ele == "ALT" || ele == "OPT" || ele == "META" {
|
||||
mods |= Modifiers::ALT;
|
||||
} else if ele == "CTRL" {
|
||||
mods |= Modifiers::CTRL;
|
||||
} else if ele == "SUPER" || ele == "CMD" || ele == "WIN" {
|
||||
mods |= Modifiers::SUPER;
|
||||
} else {
|
||||
return Err(serde::de::Error::custom(format!(
|
||||
"invalid modifier name {} in {}",
|
||||
ele, s
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(mods)
|
||||
}
|
||||
|
||||
fn default_hyperlink_rules() -> Vec<hyperlink::Rule> {
|
||||
@@ -114,6 +326,7 @@ impl Default for Config {
|
||||
term: default_term(),
|
||||
default_prog: None,
|
||||
mux_server_unix_domain_socket_path: None,
|
||||
keys: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -337,6 +550,10 @@ impl Config {
|
||||
|
||||
let cfg: Self = toml::from_str(&s)
|
||||
.map_err(|e| format_err!("Error parsing TOML from {}: {:?}", p.display(), e))?;
|
||||
|
||||
// Compute but discard the key bindings here so that we raise any
|
||||
// problems earlier than we use them.
|
||||
let _ = cfg.key_bindings()?;
|
||||
return Ok(cfg.compute_extra_defaults());
|
||||
}
|
||||
|
||||
@@ -347,6 +564,17 @@ impl Config {
|
||||
Self::default().compute_extra_defaults()
|
||||
}
|
||||
|
||||
pub fn key_bindings(&self) -> Fallible<HashMap<(KeyCode, Modifiers), KeyAssignment>> {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for k in &self.keys {
|
||||
let value = k.try_into()?;
|
||||
map.insert((k.key, k.mods), value);
|
||||
}
|
||||
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
/// In some cases we need to compute expanded values based
|
||||
/// on those provided by the user. This is where we do that.
|
||||
fn compute_extra_defaults(&self) -> Self {
|
||||
|
||||
@@ -8,12 +8,14 @@ use failure::Fallible;
|
||||
use failure::{format_err, Error};
|
||||
use portable_pty::PtySize;
|
||||
use promise::Future;
|
||||
use std::collections::HashMap;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use term::{KeyCode, KeyModifiers};
|
||||
use termwiz::hyperlink::Hyperlink;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum KeyAssignment {
|
||||
SpawnTab,
|
||||
SpawnWindow,
|
||||
@@ -42,6 +44,7 @@ pub struct HostImpl<H: HostHelper> {
|
||||
/// macOS gets unhappy if we set up the clipboard too early,
|
||||
/// so we use an Option to defer it until we use it
|
||||
clipboard: Option<ClipboardContext>,
|
||||
keys: HashMap<(KeyCode, KeyModifiers), KeyAssignment>,
|
||||
}
|
||||
|
||||
const PASTE_CHUNK_SIZE: usize = 1024;
|
||||
@@ -85,9 +88,14 @@ fn trickle_paste(tab_id: TabId, text: String) {
|
||||
|
||||
impl<H: HostHelper> HostImpl<H> {
|
||||
pub fn new(helper: H) -> Self {
|
||||
let mux = Mux::get().unwrap();
|
||||
Self {
|
||||
helper,
|
||||
clipboard: None,
|
||||
keys: mux
|
||||
.config()
|
||||
.key_bindings()
|
||||
.expect("keys section of config to be valid"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,7 +228,10 @@ impl<H: HostHelper> HostImpl<H> {
|
||||
mods: KeyModifiers,
|
||||
key: KeyCode,
|
||||
) -> Result<bool, Error> {
|
||||
if let Some(assignment) = self.key_assignment(mods, key) {
|
||||
if let Some(assignment) = self.keys.get(&(key, mods)).cloned() {
|
||||
self.perform_key_assignment(tab, &assignment)?;
|
||||
Ok(true)
|
||||
} else if let Some(assignment) = self.key_assignment(mods, key) {
|
||||
self.perform_key_assignment(tab, &assignment)?;
|
||||
Ok(true)
|
||||
} else {
|
||||
|
||||
@@ -75,7 +75,7 @@ pub struct KeyEvent {
|
||||
/// Which key is pressed. Not all of these are probable to appear
|
||||
/// on most systems. A lot of this list is @wez trawling docs and
|
||||
/// making an entry for things that might be possible in this first pass.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum KeyCode {
|
||||
/// The decoded unicode character
|
||||
Char(char),
|
||||
|
||||
Reference in new issue
Block a user