diff options
Diffstat (limited to 'src/command')
-rw-r--r-- | src/command/import.rs | 41 | ||||
-rw-r--r-- | src/command/mod.rs | 6 |
2 files changed, 32 insertions, 15 deletions
diff --git a/src/command/import.rs b/src/command/import.rs index 77db1c84..5a91b6b7 100644 --- a/src/command/import.rs +++ b/src/command/import.rs @@ -1,8 +1,8 @@ use std::env; use std::path::PathBuf; +use directories::UserDirs; use eyre::{eyre, Result}; -use home::home_dir; use structopt::StructOpt; use crate::local::database::{Database, Sqlite}; @@ -39,7 +39,7 @@ impl Cmd { Self::Auto => { let shell = env::var("SHELL").unwrap_or_else(|_| String::from("NO_SHELL")); - if shell.as_str() == "/bin/zsh" { + if shell.ends_with("/zsh") { println!("Detected ZSH"); import_zsh(db) } else { @@ -61,20 +61,33 @@ fn import_zsh(db: &mut Sqlite) -> Result<()> { let histpath = env::var("HISTFILE"); let histpath = if let Ok(p) = histpath { - PathBuf::from(p) - } else { - let mut home = home_dir().unwrap(); - home.push(".zhistory"); + let histpath = PathBuf::from(p); - home - }; + if !histpath.exists() { + return Err(eyre!( + "Could not find history file at {}", + histpath.to_str().unwrap() + )); + } - if !histpath.exists() { - return Err(eyre!( - "Could not find history file at {}, try setting $HISTFILE", - histpath.to_str().unwrap() - )); - } + histpath + } else { + let user_dirs = UserDirs::new().unwrap(); + let home_dir = user_dirs.home_dir(); + + let mut candidates = [".zhistory", ".zsh_history"].iter(); + loop { + match candidates.next() { + Some(candidate) => { + let histpath = home_dir.join(candidate); + if histpath.exists() { + break histpath; + } + } + None => return Err(eyre!("Could not find history file. try setting $HISTFILE")), + } + } + }; let zsh = Zsh::new(histpath.to_str().unwrap())?; diff --git a/src/command/mod.rs b/src/command/mod.rs index 4ac62385..2e8d4778 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -26,6 +26,10 @@ pub enum AtuinCmd { Uuid, } +pub fn uuid_v4() -> String { + Uuid::new_v4().to_simple().to_string() +} + impl AtuinCmd { pub fn run(self, db: &mut Sqlite) -> Result<()> { match self { @@ -34,7 +38,7 @@ impl AtuinCmd { Self::Server(server) => server.run(), Self::Uuid => { - println!("{}", Uuid::new_v4().to_simple().to_string()); + println!("{}", uuid_v4()); Ok(()) } } |