aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs111
1 files changed, 109 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs
index c1b00ae..3532f80 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -16,7 +16,7 @@ pub struct Config {
pub muted: u32,
pub border: u32,
pub show_status_bar: bool,
- pub status_bar_toggle: String,
+ pub shortcuts: HashMap<String, String>,
pub width: f32,
pub height: f32,
pub scrollback: usize,
@@ -44,9 +44,51 @@ pub fn color(s: &str) -> Result<u32> {
Ok(u32::from_str_radix(s, 16)?)
}
impl Config {
+ pub fn action_for(&self, key: &gpui::Keystroke) -> Option<String> {
+ self.shortcuts.iter().find_map(|(binding, action)| {
+ let parsed = gpui::Keystroke::parse(binding).ok()?;
+ (parsed.modifiers == key.modifiers && parsed.key.eq_ignore_ascii_case(&key.key))
+ .then(|| action.clone())
+ })
+ }
+ pub fn shortcut(&self, action: &str) -> String {
+ let mut keys = self
+ .shortcuts
+ .iter()
+ .filter(|(_, a)| a.as_str() == action)
+ .map(|(k, _)| k.clone())
+ .collect::<Vec<_>>();
+ keys.sort();
+ keys.first()
+ .map(|k| {
+ k.split('-')
+ .map(|part| match part {
+ "ctrl" => "Ctrl".into(),
+ "alt" => "Alt".into(),
+ "shift" => "Shift".into(),
+ s => s.to_uppercase(),
+ })
+ .collect::<Vec<String>>()
+ .join("+")
+ })
+ .unwrap_or_else(|| "unbound".into())
+ }
pub fn load() -> Result<Self> {
let dir = directory();
std::fs::create_dir_all(&dir)?;
+ let themes = dir.join("themes");
+ std::fs::create_dir_all(&themes)?;
+ for (name, source) in [
+ ("ember", include_str!("../assets/themes/ember.lua")),
+ ("glacier", include_str!("../assets/themes/glacier.lua")),
+ ("paper", include_str!("../assets/themes/paper.lua")),
+ ("lagoon", include_str!("../assets/themes/lagoon.lua")),
+ ] {
+ let path = themes.join(format!("{name}.lua"));
+ if !path.exists() {
+ std::fs::write(path, source)?;
+ }
+ }
let prompt = dir.join("shell.bash");
if !prompt.exists() {
std::fs::write(&prompt, include_str!("../assets/shell.bash"))?;
@@ -139,7 +181,7 @@ impl Config {
.map_err(|e| anyhow::anyhow!(e.to_string()))?,
)?,
show_status_bar: get!("show_status_bar"),
- status_bar_toggle: get!("status_bar_toggle"),
+ shortcuts: get!("shortcuts"),
width: get!("width"),
height: get!("height"),
scrollback: get!("scrollback"),
@@ -166,6 +208,32 @@ impl Config {
"invalid font, spacing, window dimensions, scrollback, or palette (requires 16 colors)"
);
}
+ for (key, action) in &c.shortcuts {
+ gpui::Keystroke::parse(key)
+ .map_err(|e| anyhow::anyhow!("invalid shortcut {key}: {e}"))?;
+ let valid = matches!(
+ action.as_str(),
+ "new_tab"
+ | "close_tab"
+ | "copy"
+ | "paste"
+ | "reload"
+ | "toggle_status"
+ | "interrupt"
+ | "next_tab"
+ | "previous_tab"
+ | "font_larger"
+ | "font_smaller"
+ | "scroll_up"
+ | "scroll_down"
+ ) || action
+ .strip_prefix("tab:")
+ .and_then(|s| s.parse::<usize>().ok())
+ .is_some_and(|i| i > 0);
+ if !valid {
+ bail!("unknown shortcut action: {action}");
+ }
+ }
Ok(c)
}
}
@@ -184,6 +252,45 @@ mod tests {
assert_eq!(c.palette.len(), 16);
}
#[test]
+ fn lua_shortcuts_separate_copy_interrupt_and_tab_selection() {
+ let c = Config::parse(DEFAULT, std::path::Path::new("/tmp")).unwrap();
+ for (key, action) in [
+ ("ctrl-c", "copy"),
+ ("ctrl-alt-c", "interrupt"),
+ ("alt-4", "tab:4"),
+ ("ctrl-r", "reload"),
+ ] {
+ assert_eq!(
+ c.action_for(&gpui::Keystroke::parse(key).unwrap())
+ .as_deref(),
+ Some(action)
+ );
+ }
+ let custom = Config::parse(
+ "return {shortcuts={['alt-x']='copy'}}",
+ std::path::Path::new("/tmp"),
+ )
+ .unwrap();
+ assert_eq!(
+ custom
+ .action_for(&gpui::Keystroke::parse("alt-x").unwrap())
+ .as_deref(),
+ Some("copy")
+ );
+ assert!(
+ custom
+ .action_for(&gpui::Keystroke::parse("ctrl-c").unwrap())
+ .is_none()
+ );
+ assert!(
+ Config::parse(
+ "return {shortcuts={['alt-x']='typo'}}",
+ std::path::Path::new("/tmp")
+ )
+ .is_err()
+ );
+ }
+ #[test]
fn prompt_path_uses_platform_config_directory() {
let dir = std::path::Path::new("/Users/example/Library/Application Support/ducky");
let c = Config::parse(DEFAULT, dir).unwrap();