From 894eaa6faff86e1839510e114427b949b2440d39 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Wed, 3 Apr 2024 10:19:24 +0100 Subject: perf(dotfiles): cache aliases and read straight from file (#1918) * cache aliases when set locally * handle rebuild on sync and tidy things a bit * support all shells except nu * make clippy happy * fmt * fix for no features --- atuin-dotfiles/src/store.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'atuin-dotfiles/src/store.rs') diff --git a/atuin-dotfiles/src/store.rs b/atuin-dotfiles/src/store.rs index 96e0fb32..425a5e1e 100644 --- a/atuin-dotfiles/src/store.rs +++ b/atuin-dotfiles/src/store.rs @@ -136,6 +136,54 @@ impl AliasStore { } } + pub async fn posix(&self) -> Result { + let aliases = self.aliases().await?; + + let mut config = String::new(); + + for alias in aliases { + config.push_str(&format!("alias {}='{}'\n", alias.name, alias.value)); + } + + Ok(config) + } + + pub async fn xonsh(&self) -> Result { + let aliases = self.aliases().await?; + + let mut config = String::new(); + + for alias in aliases { + config.push_str(&format!("aliases['{}'] ='{}'\n", alias.name, alias.value)); + } + + Ok(config) + } + + pub async fn build(&self) -> Result<()> { + let dir = atuin_common::utils::dotfiles_cache_dir(); + tokio::fs::create_dir_all(dir.clone()).await?; + + // Build for all supported shells + let posix = self.posix().await?; + let xonsh = self.xonsh().await?; + + // All the same contents, maybe optimize in the future or perhaps there will be quirks + // per-shell + // I'd prefer separation atm + let zsh = dir.join("aliases.zsh"); + let bash = dir.join("aliases.bash"); + let fish = dir.join("aliases.fish"); + let xsh = dir.join("aliases.xsh"); + + tokio::fs::write(zsh, &posix).await?; + tokio::fs::write(bash, &posix).await?; + tokio::fs::write(fish, &posix).await?; + tokio::fs::write(xsh, &xonsh).await?; + + Ok(()) + } + pub async fn set(&self, name: &str, value: &str) -> Result<()> { if name.len() + value.len() > CONFIG_SHELL_ALIAS_FIELD_MAX_LEN { return Err(eyre!( @@ -169,6 +217,9 @@ impl AliasStore { .push(&record.encrypt::(&self.encryption_key)) .await?; + // set mutates shell config, so build again + self.build().await?; + Ok(()) } @@ -202,6 +253,9 @@ impl AliasStore { .push(&record.encrypt::(&self.encryption_key)) .await?; + // delete mutates shell config, so build again + self.build().await?; + Ok(()) } -- cgit v1.3.1