From 6636f5878ac11d6461b9958af025021486a7d58f Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sun, 14 Feb 2021 18:40:51 +0000 Subject: [PATCH] zsh bin is sometimes /usr/bin/zsh or might be elsewhere too (#8) zsh also uses ~/.zsh_history get better errors for not found history file --- Cargo.lock | 13 ++----------- Cargo.toml | 19 +++++++++---------- src/command/import.rs | 39 ++++++++++++++++++++++++++------------- src/command/mod.rs | 6 +++++- src/local/history.rs | 6 +++--- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9d9fec5..588231d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "aead" version = "0.2.0" @@ -109,7 +111,6 @@ dependencies = [ "chrono", "directories", "eyre", - "home", "hostname", "indicatif", "log 0.4.14", @@ -543,15 +544,6 @@ dependencies = [ "digest", ] -[[package]] -name = "home" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654" -dependencies = [ - "winapi", -] - [[package]] name = "hostname" version = "0.3.1" @@ -1341,7 +1333,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" dependencies = [ "getrandom 0.2.2", - "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c718423..29d7df0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,19 +7,18 @@ license = "MIT" description = "atuin - sync your shell history" [dependencies] -log = "0.4.*" -pretty_env_logger = "0.4.*" -chrono = "0.4.*" -eyre = "0.6.*" -shellexpand = "2.*" -structopt = "0.3.*" -directories = "3.*" -uuid = { version = "0.8", features = ["serde", "v4"] } -home = "0.5.3" +log = "0.4" +pretty_env_logger = "0.4" +chrono = "0.4" +eyre = "0.6" +shellexpand = "2" +structopt = "0.3" +directories = "3" +uuid = { version = "0.8", features = ["v4"] } indicatif = "0.15.0" hostname = "0.3.1" rocket = "0.4.7" [dependencies.rusqlite] -version = "0.24.*" +version = "0.24" features = ["bundled"] diff --git a/src/command/import.rs b/src/command/import.rs index 77db1c8..5a91b6b 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,21 +61,34 @@ fn import_zsh(db: &mut Sqlite) -> Result<()> { let histpath = env::var("HISTFILE"); let histpath = if let Ok(p) = histpath { - PathBuf::from(p) + let histpath = PathBuf::from(p); + + if !histpath.exists() { + return Err(eyre!( + "Could not find history file at {}", + histpath.to_str().unwrap() + )); + } + + histpath } else { - let mut home = home_dir().unwrap(); - home.push(".zhistory"); + let user_dirs = UserDirs::new().unwrap(); + let home_dir = user_dirs.home_dir(); - home + 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")), + } + } }; - if !histpath.exists() { - return Err(eyre!( - "Could not find history file at {}, try setting $HISTFILE", - histpath.to_str().unwrap() - )); - } - let zsh = Zsh::new(histpath.to_str().unwrap())?; let progress = ProgressBar::new(zsh.loc); diff --git a/src/command/mod.rs b/src/command/mod.rs index 4ac6238..2e8d477 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(()) } } diff --git a/src/local/history.rs b/src/local/history.rs index d88353f..05600b8 100644 --- a/src/local/history.rs +++ b/src/local/history.rs @@ -1,6 +1,6 @@ use std::env; -use uuid::Uuid; +use crate::command::uuid_v4; #[derive(Debug)] pub struct History { @@ -26,12 +26,12 @@ impl History { ) -> Self { let session = session .or_else(|| env::var("ATUIN_SESSION").ok()) - .unwrap_or_else(|| Uuid::new_v4().to_simple().to_string()); + .unwrap_or_else(uuid_v4); let hostname = hostname.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string()); Self { - id: Uuid::new_v4().to_simple().to_string(), + id: uuid_v4(), timestamp, command, cwd,