diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-04-03 10:19:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-03 10:19:24 +0100 |
| commit | 894eaa6faff86e1839510e114427b949b2440d39 (patch) | |
| tree | 7bbda2eebb75b79292f3095017c44ebc97387654 /atuin-dotfiles/src/store.rs | |
| parent | feat: add 'ctrl-a a' to jump to beginning of line (#1917) (diff) | |
| download | atuin-894eaa6faff86e1839510e114427b949b2440d39.zip | |
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
Diffstat (limited to 'atuin-dotfiles/src/store.rs')
| -rw-r--r-- | atuin-dotfiles/src/store.rs | 54 |
1 files changed, 54 insertions, 0 deletions
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<String> { + 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<String> { + 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::<PASETO_V4>(&self.encryption_key)) .await?; + // set mutates shell config, so build again + self.build().await?; + Ok(()) } @@ -202,6 +253,9 @@ impl AliasStore { .push(&record.encrypt::<PASETO_V4>(&self.encryption_key)) .await?; + // delete mutates shell config, so build again + self.build().await?; + Ok(()) } |
