atuin_dotfiles/shell/
fish.rs

1// Configuration for fish
2use std::path::PathBuf;
3
4use crate::store::{var::VarStore, AliasStore};
5
6async fn cached_aliases(path: PathBuf, store: &AliasStore) -> String {
7    match tokio::fs::read_to_string(path).await {
8        Ok(aliases) => aliases,
9        Err(r) => {
10            // we failed to read the file for some reason, but the file does exist
11            // fallback to generating new aliases on the fly
12
13            store.posix().await.unwrap_or_else(|e| {
14                format!("echo 'Atuin: failed to read and generate aliases: \n{r}\n{e}'",)
15            })
16        }
17    }
18}
19
20async fn cached_vars(path: PathBuf, store: &VarStore) -> String {
21    match tokio::fs::read_to_string(path).await {
22        Ok(vars) => vars,
23        Err(r) => {
24            // we failed to read the file for some reason, but the file does exist
25            // fallback to generating new vars on the fly
26
27            store.posix().await.unwrap_or_else(|e| {
28                format!("echo 'Atuin: failed to read and generate vars: \n{r}\n{e}'",)
29            })
30        }
31    }
32}
33
34/// Return fish dotfile config
35///
36/// Do not return an error. We should not prevent the shell from starting.
37///
38/// In the worst case, Atuin should not function but the shell should start correctly.
39///
40/// While currently this only returns aliases, it will be extended to also return other synced dotfiles
41pub async fn alias_config(store: &AliasStore) -> String {
42    // First try to read the cached config
43    let aliases = atuin_common::utils::dotfiles_cache_dir().join("aliases.fish");
44
45    if aliases.exists() {
46        return cached_aliases(aliases, store).await;
47    }
48
49    if let Err(e) = store.build().await {
50        return format!("echo 'Atuin: failed to generate aliases: {}'", e);
51    }
52
53    cached_aliases(aliases, store).await
54}
55
56pub async fn var_config(store: &VarStore) -> String {
57    // First try to read the cached config
58    let vars = atuin_common::utils::dotfiles_cache_dir().join("vars.fish");
59
60    if vars.exists() {
61        return cached_vars(vars, store).await;
62    }
63
64    if let Err(e) = store.build().await {
65        return format!("echo 'Atuin: failed to generate vars: {}'", e);
66    }
67
68    cached_vars(vars, store).await
69}