Skip to content

Commit

Permalink
feat(dotfiles): add enable setting to dotfiles, disable by default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie committed Mar 5, 2024
1 parent 60a09f9 commit 14be7e0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 9 deletions.
5 changes: 5 additions & 0 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub const LATEST_VERSION_FILENAME: &str = "latest_version";
pub const HOST_ID_FILENAME: &str = "host_id";
static EXAMPLE_CONFIG: &str = include_str!("../config.toml");

mod dotfiles;

#[derive(Clone, Debug, Deserialize, Copy, ValueEnum, PartialEq)]
pub enum SearchMode {
#[serde(rename = "prefix")]
Expand Down Expand Up @@ -393,6 +395,9 @@ pub struct Settings {
#[serde(default)]
pub keys: Keys,

#[serde(default)]
pub dotfiles: dotfiles::Settings,

// This is automatically loaded when settings is created. Do not set in
// config! Keep secrets and settings apart.
#[serde(skip)]
Expand Down
6 changes: 6 additions & 0 deletions atuin-client/src/settings/dotfiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Settings {
pub enabled: bool,
}
6 changes: 6 additions & 0 deletions atuin/src/command/client/dotfiles/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ impl Cmd {
}

pub async fn run(&self, settings: &Settings, store: SqliteStore) -> Result<()> {
if !settings.dotfiles.enabled {
eprintln!("Dotfiles are not enabled. Add\n\n[dotfiles]\nenabled = true\n\nto your configuration file to enable them.\n");
eprintln!("The default configuration file is located at ~/.config/atuin/config.toml.");
return Ok(());
}

let encryption_key: [u8; 32] = encryption::load_key(settings)
.context("could not load encryption key")?
.into();
Expand Down
32 changes: 31 additions & 1 deletion atuin/src/command/client/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,27 @@ $env.config = (
}
}

pub async fn run(self, settings: &Settings) -> Result<()> {
fn static_init(&self) {
match self.shell {
Shell::Zsh => {
zsh::init_static(self.disable_up_arrow, self.disable_ctrl_r);
}
Shell::Bash => {
bash::init_static(self.disable_up_arrow, self.disable_ctrl_r);
}
Shell::Fish => {
fish::init_static(self.disable_up_arrow, self.disable_ctrl_r);
}
Shell::Nu => {
self.init_nu();
}
Shell::Xonsh => {
xonsh::init_static(self.disable_up_arrow, self.disable_ctrl_r);
}
};
}

async fn dotfiles_init(&self, settings: &Settings) -> Result<()> {
let record_store_path = PathBuf::from(settings.record_store_path.as_str());
let sqlite_store = SqliteStore::new(record_store_path, settings.local_timeout).await?;

Expand Down Expand Up @@ -112,4 +132,14 @@ $env.config = (

Ok(())
}

pub async fn run(self, settings: &Settings) -> Result<()> {
if settings.dotfiles.enabled {
self.dotfiles_init(settings).await?;
} else {
self.static_init();
}

Ok(())
}
}
14 changes: 9 additions & 5 deletions atuin/src/command/client/init/bash.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use atuin_dotfiles::store::AliasStore;
use eyre::Result;

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
pub fn init_static(disable_up_arrow: bool, disable_ctrl_r: bool) {
let base = include_str!("../../../shell/atuin.bash");

let aliases = store.aliases().await?;

let aliases = atuin_dotfiles::shell::bash::build(&aliases[..]);

let (bind_ctrl_r, bind_up_arrow) = if std::env::var("ATUIN_NOBIND").is_ok() {
(false, false)
} else {
Expand All @@ -17,6 +13,14 @@ pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: boo
println!("__atuin_bind_ctrl_r={bind_ctrl_r}");
println!("__atuin_bind_up_arrow={bind_up_arrow}");
println!("{base}");
}

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = store.aliases().await?;
let aliases = atuin_dotfiles::shell::bash::build(&aliases[..]);

println!("{aliases}");

Ok(())
Expand Down
6 changes: 5 additions & 1 deletion atuin/src/command/client/init/fish.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use atuin_dotfiles::store::AliasStore;
use eyre::Result;

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
pub fn init_static(disable_up_arrow: bool, disable_ctrl_r: bool) {
let base = include_str!("../../../shell/atuin.fish");

println!("{base}");
Expand Down Expand Up @@ -32,6 +32,10 @@ bind -M insert \e\[A _atuin_bind_up";
}
println!("end");
}
}

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = store.aliases().await?;
let aliases = atuin_dotfiles::shell::fish::build(&aliases[..]);
Expand Down
6 changes: 5 additions & 1 deletion atuin/src/command/client/init/xonsh.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use atuin_dotfiles::store::AliasStore;
use eyre::Result;

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
pub fn init_static(disable_up_arrow: bool, disable_ctrl_r: bool) {
let base = include_str!("../../../shell/atuin.xsh");

let (bind_ctrl_r, bind_up_arrow) = if std::env::var("ATUIN_NOBIND").is_ok() {
Expand All @@ -18,6 +18,10 @@ pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: boo
if bind_up_arrow { "True" } else { "False" }
);
println!("{base}");
}

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = store.aliases().await?;
let aliases = atuin_dotfiles::shell::xonsh::build(&aliases[..]);
Expand Down
6 changes: 5 additions & 1 deletion atuin/src/command/client/init/zsh.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use atuin_dotfiles::store::AliasStore;
use eyre::Result;

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
pub fn init_static(disable_up_arrow: bool, disable_ctrl_r: bool) {
let base = include_str!("../../../shell/atuin.zsh");

println!("{base}");
Expand All @@ -26,6 +26,10 @@ bindkey -M vicmd 'k' atuin-up-search-vicmd";
println!("{BIND_UP_ARROW}");
}
}
}

pub async fn init(store: AliasStore, disable_up_arrow: bool, disable_ctrl_r: bool) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = store.aliases().await?;
let aliases = atuin_dotfiles::shell::zsh::build(&aliases[..]);
Expand Down

0 comments on commit 14be7e0

Please sign in to comment.