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
This commit is contained in:
Conrad Ludgate 2021-02-14 18:40:51 +00:00 committed by GitHub
parent ea020f1b82
commit 6636f5878a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 38 deletions

13
Cargo.lock generated
View file

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "aead" name = "aead"
version = "0.2.0" version = "0.2.0"
@ -109,7 +111,6 @@ dependencies = [
"chrono", "chrono",
"directories", "directories",
"eyre", "eyre",
"home",
"hostname", "hostname",
"indicatif", "indicatif",
"log 0.4.14", "log 0.4.14",
@ -543,15 +544,6 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "home"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "hostname" name = "hostname"
version = "0.3.1" version = "0.3.1"
@ -1341,7 +1333,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [ dependencies = [
"getrandom 0.2.2", "getrandom 0.2.2",
"serde",
] ]
[[package]] [[package]]

View file

@ -7,19 +7,18 @@ license = "MIT"
description = "atuin - sync your shell history" description = "atuin - sync your shell history"
[dependencies] [dependencies]
log = "0.4.*" log = "0.4"
pretty_env_logger = "0.4.*" pretty_env_logger = "0.4"
chrono = "0.4.*" chrono = "0.4"
eyre = "0.6.*" eyre = "0.6"
shellexpand = "2.*" shellexpand = "2"
structopt = "0.3.*" structopt = "0.3"
directories = "3.*" directories = "3"
uuid = { version = "0.8", features = ["serde", "v4"] } uuid = { version = "0.8", features = ["v4"] }
home = "0.5.3"
indicatif = "0.15.0" indicatif = "0.15.0"
hostname = "0.3.1" hostname = "0.3.1"
rocket = "0.4.7" rocket = "0.4.7"
[dependencies.rusqlite] [dependencies.rusqlite]
version = "0.24.*" version = "0.24"
features = ["bundled"] features = ["bundled"]

View file

@ -1,8 +1,8 @@
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use directories::UserDirs;
use eyre::{eyre, Result}; use eyre::{eyre, Result};
use home::home_dir;
use structopt::StructOpt; use structopt::StructOpt;
use crate::local::database::{Database, Sqlite}; use crate::local::database::{Database, Sqlite};
@ -39,7 +39,7 @@ impl Cmd {
Self::Auto => { Self::Auto => {
let shell = env::var("SHELL").unwrap_or_else(|_| String::from("NO_SHELL")); 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"); println!("Detected ZSH");
import_zsh(db) import_zsh(db)
} else { } else {
@ -61,21 +61,34 @@ fn import_zsh(db: &mut Sqlite) -> Result<()> {
let histpath = env::var("HISTFILE"); let histpath = env::var("HISTFILE");
let histpath = if let Ok(p) = histpath { let histpath = if let Ok(p) = histpath {
PathBuf::from(p) let histpath = PathBuf::from(p);
} else {
let mut home = home_dir().unwrap();
home.push(".zhistory");
home
};
if !histpath.exists() { if !histpath.exists() {
return Err(eyre!( return Err(eyre!(
"Could not find history file at {}, try setting $HISTFILE", "Could not find history file at {}",
histpath.to_str().unwrap() 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())?; let zsh = Zsh::new(histpath.to_str().unwrap())?;
let progress = ProgressBar::new(zsh.loc); let progress = ProgressBar::new(zsh.loc);

View file

@ -26,6 +26,10 @@ pub enum AtuinCmd {
Uuid, Uuid,
} }
pub fn uuid_v4() -> String {
Uuid::new_v4().to_simple().to_string()
}
impl AtuinCmd { impl AtuinCmd {
pub fn run(self, db: &mut Sqlite) -> Result<()> { pub fn run(self, db: &mut Sqlite) -> Result<()> {
match self { match self {
@ -34,7 +38,7 @@ impl AtuinCmd {
Self::Server(server) => server.run(), Self::Server(server) => server.run(),
Self::Uuid => { Self::Uuid => {
println!("{}", Uuid::new_v4().to_simple().to_string()); println!("{}", uuid_v4());
Ok(()) Ok(())
} }
} }

View file

@ -1,6 +1,6 @@
use std::env; use std::env;
use uuid::Uuid; use crate::command::uuid_v4;
#[derive(Debug)] #[derive(Debug)]
pub struct History { pub struct History {
@ -26,12 +26,12 @@ impl History {
) -> Self { ) -> Self {
let session = session let session = session
.or_else(|| env::var("ATUIN_SESSION").ok()) .or_else(|| env::var("ATUIN_SESSION").ok())
.unwrap_or_else(|| Uuid::new_v4().to_simple().to_string()); .unwrap_or_else(uuid_v4);
let hostname = let hostname =
hostname.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string()); hostname.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
Self { Self {
id: Uuid::new_v4().to_simple().to_string(), id: uuid_v4(),
timestamp, timestamp,
command, command,
cwd, cwd,