wezterm: remove KeyAction toml translation helper

Now that the toml file support is removed, we can directly
express the key actions as enum variants so remove this
intermediate type and use the enum directly.
This commit is contained in:
Wez Furlong committed 2020-05-21 08:46:40 -07:00
1 parent 448fa084a9
commit f6743086ac
6 files changed
+15 -129

No files matched your search

+4
View File
@@ -12,6 +12,10 @@ brief notes about them may accumulate here.
* Windows: Fixed AltGr handling for European layouts
* Removed old TOML config file parsing code
* Removed old `arg="something"` key binding parameter. This was a remnant from
the TOML based configuration. You're unlikely to notice this unless you
followed an example from the docs; migrate instead to using eg:
`action=wezterm.action{ActivateTab=i-1}` to pass the integer argument.
### 20200517-122836-92c201c6
+2 -4
View File
@@ -154,14 +154,12 @@ for i = 1, 8 do
table.insert(mykeys, {
key=tostring(i),
mods="CTRL|ALT",
action="ActivateTab",
arg=tostring(i-1)
action=wezterm.action{ActivateTab=i-1},
})
-- F1 through F8 to activate that tab
table.insert(mykeys, {
key="F" .. tostring(i),
action="ActivateTab",
arg=tostring(i-1)
action=wezterm.action{ActivateTab=i-1},
})
end
+2 -118
View File
@@ -1,5 +1,4 @@
use crate::keyassignment::{KeyAssignment, SpawnTabDomain};
use anyhow::{anyhow, Error};
use crate::keyassignment::KeyAssignment;
use serde::{Deserialize, Deserializer, Serialize};
use termwiz::input::{KeyCode, Modifiers};
@@ -9,125 +8,10 @@ pub struct Key {
pub key: KeyCode,
#[serde(deserialize_with = "de_modifiers", default)]
pub mods: Modifiers,
// TODO: migrate this to using KeyAssignment directly.
// We're preserving KeyAction here for a release or
// two to allow for a slightly more graceful migration
// for user configs.
pub action: KeyAction,
// TODO: once action is switched to KeyAssignment, remove
// the arg field. Maybe we can use the presence of this
// field to warn users that they need to migrate?
#[serde(default)]
pub arg: Option<String>,
pub action: KeyAssignment,
}
impl_lua_conversion!(Key);
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(SpawnTabDomain::DefaultDomain),
KeyAction::SpawnTabInCurrentTabDomain => {
KeyAssignment::SpawnTab(SpawnTabDomain::CurrentTabDomain)
}
KeyAction::SpawnTabInDomain => {
let arg = self
.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?;
if let Ok(id) = arg.parse() {
KeyAssignment::SpawnTab(SpawnTabDomain::Domain(id))
} else {
KeyAssignment::SpawnTab(SpawnTabDomain::DomainName(arg.to_string()))
}
}
KeyAction::SpawnWindow => KeyAssignment::SpawnWindow,
KeyAction::ToggleFullScreen => KeyAssignment::ToggleFullScreen,
KeyAction::Copy => KeyAssignment::Copy,
KeyAction::Paste => KeyAssignment::Paste,
KeyAction::Hide => KeyAssignment::Hide,
KeyAction::HideApplication => KeyAssignment::HideApplication,
KeyAction::QuitApplication => KeyAssignment::QuitApplication,
KeyAction::Show => KeyAssignment::Show,
KeyAction::IncreaseFontSize => KeyAssignment::IncreaseFontSize,
KeyAction::DecreaseFontSize => KeyAssignment::DecreaseFontSize,
KeyAction::ResetFontSize => KeyAssignment::ResetFontSize,
KeyAction::Nop => KeyAssignment::Nop,
KeyAction::CloseCurrentTab => KeyAssignment::CloseCurrentTab,
KeyAction::ActivateTab => KeyAssignment::ActivateTab(
self.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?
.parse()?,
),
KeyAction::ActivateTabRelative => KeyAssignment::ActivateTabRelative(
self.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?
.parse()?,
),
KeyAction::SendString => KeyAssignment::SendString(
self.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?
.to_owned(),
),
KeyAction::ReloadConfiguration => KeyAssignment::ReloadConfiguration,
KeyAction::MoveTab => KeyAssignment::MoveTab(
self.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?
.parse()?,
),
KeyAction::MoveTabRelative => KeyAssignment::MoveTabRelative(
self.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?
.parse()?,
),
KeyAction::ScrollByPage => KeyAssignment::ScrollByPage(
self.arg
.as_ref()
.ok_or_else(|| anyhow!("missing arg for {:?}", self))?
.parse()?,
),
KeyAction::ShowTabNavigator => KeyAssignment::ShowTabNavigator,
KeyAction::Action(ref a) => a.clone(),
})
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub enum KeyAction {
SpawnTab,
SpawnTabInCurrentTabDomain,
SpawnTabInDomain,
SpawnWindow,
ToggleFullScreen,
Copy,
Paste,
ActivateTabRelative,
IncreaseFontSize,
DecreaseFontSize,
ResetFontSize,
ActivateTab,
SendString,
Nop,
Hide,
HideApplication,
QuitApplication,
Show,
CloseCurrentTab,
ReloadConfiguration,
MoveTab,
MoveTabRelative,
ScrollByPage,
ShowTabNavigator,
Action(KeyAssignment),
}
impl_lua_conversion!(KeyAction);
fn de_keycode<'de, D>(deserializer: D) -> Result<KeyCode, D::Error>
where
D: Deserializer<'de>,
+1 -3
View File
@@ -12,7 +12,6 @@ use portable_pty::{CommandBuilder, PtySize};
use serde::{Deserialize, Serialize};
use std;
use std::collections::HashMap;
use std::convert::TryInto;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::prelude::*;
@@ -652,8 +651,7 @@ impl Config {
let mut map = HashMap::new();
for k in &self.keys {
let value = k.try_into()?;
map.insert((k.key, k.mods), value);
map.insert((k.key, k.mods), k.action.clone());
}
Ok(map)
+1
View File
@@ -81,6 +81,7 @@ pub enum KeyAssignment {
SpawnCommandInNewWindow(SpawnCommand),
ShowLauncher,
}
impl_lua_conversion!(KeyAssignment);
pub struct KeyMap(HashMap<(KeyCode, KeyModifiers), KeyAssignment>);
+5 -4
View File
@@ -212,10 +212,11 @@ fn font_with_fallback<'lua>(
/// }
/// }
/// ```
fn action<'lua>(lua: &'lua Lua, action: Table<'lua>) -> mlua::Result<crate::config::KeyAction> {
let wrapper = lua.create_table()?;
wrapper.set("Action", action)?;
Ok(from_lua_value(Value::Table(wrapper))?)
fn action<'lua>(
_lua: &'lua Lua,
action: Table<'lua>,
) -> mlua::Result<crate::keyassignment::KeyAssignment> {
Ok(from_lua_value(Value::Table(action))?)
}
fn read_dir<'lua>(_: &'lua Lua, path: String) -> mlua::Result<Vec<String>> {