fix some bugs (#90)

* fix some bugs

* format
This commit is contained in:
Conrad Ludgate 2021-05-09 19:12:41 +01:00 committed by GitHub
parent bd4db1fa03
commit 4b9ff801a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 49 deletions

View file

@ -98,13 +98,11 @@ impl Settings {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
let config_dir = atuin_common::utils::config_dir(); let config_dir = atuin_common::utils::config_dir();
let config_dir = config_dir.as_path();
let data_dir = atuin_common::utils::data_dir(); let data_dir = atuin_common::utils::data_dir();
let data_dir = data_dir.as_path();
create_dir_all(config_dir)?; create_dir_all(&config_dir)?;
create_dir_all(data_dir)?; create_dir_all(&data_dir)?;
let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") { let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
PathBuf::from(p) PathBuf::from(p)

View file

@ -30,39 +30,60 @@ pub fn uuid_v4() -> String {
Uuid::new_v4().to_simple().to_string() Uuid::new_v4().to_simple().to_string()
} }
pub fn config_dir() -> PathBuf { // TODO: more reliable, more tested
// TODO: more reliable, more tested // I don't want to use ProjectDirs, it puts config in awkward places on
// I don't want to use ProjectDirs, it puts config in awkward places on // mac. Data too. Seems to be more intended for GUI apps.
// mac. Data too. Seems to be more intended for GUI apps. pub fn home_dir() -> PathBuf {
let home = std::env::var("HOME").expect("$HOME not found"); let home = std::env::var("HOME").expect("$HOME not found");
let home = PathBuf::from(home); PathBuf::from(home)
}
std::env::var("XDG_CONFIG_HOME").map_or_else( pub fn config_dir() -> PathBuf {
|_| { let config_dir =
let mut config = home.clone(); std::env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from);
config.push(".config"); config_dir.join("atuin")
config.push("atuin");
config
},
PathBuf::from,
)
} }
pub fn data_dir() -> PathBuf { pub fn data_dir() -> PathBuf {
// TODO: more reliable, more tested let data_dir = std::env::var("XDG_DATA_HOME")
// I don't want to use ProjectDirs, it puts config in awkward places on .map_or_else(|_| home_dir().join(".local").join("share"), PathBuf::from);
// mac. Data too. Seems to be more intended for GUI apps.
let home = std::env::var("HOME").expect("$HOME not found");
let home = PathBuf::from(home);
std::env::var("XDG_DATA_HOME").map_or_else( data_dir.join("atuin")
|_| { }
let mut data = home.clone();
data.push(".local"); #[cfg(test)]
data.push("share"); mod tests {
data.push("atuin"); use super::*;
data use std::env;
},
PathBuf::from, #[test]
) fn test_config_dir_xdg() {
env::remove_var("HOME");
env::set_var("XDG_CONFIG_HOME", "/home/user/custom_config");
assert_eq!(
config_dir(),
PathBuf::from("/home/user/custom_config/atuin")
);
}
#[test]
fn test_config_dir() {
env::set_var("HOME", "/home/user");
env::remove_var("XDG_CONFIG_HOME");
assert_eq!(config_dir(), PathBuf::from("/home/user/.config/atuin"));
}
#[test]
fn test_data_dir_xdg() {
env::remove_var("HOME");
env::set_var("XDG_DATA_HOME", "/home/user/custom_data");
assert_eq!(data_dir(), PathBuf::from("/home/user/custom_data/atuin"));
}
#[test]
fn test_data_dir() {
env::set_var("HOME", "/home/user");
env::remove_var("XDG_DATA_HOME");
assert_eq!(data_dir(), PathBuf::from("/home/user/.local/share/atuin"));
}
} }

View file

@ -155,23 +155,34 @@ impl Cmd {
human, human,
cmd_only, cmd_only,
} => { } => {
let params = (session, cwd); let session = if *session {
let cwd = env::current_dir()?.display().to_string(); Some(env::var("ATUIN_SESSION")?)
let session = env::var("ATUIN_SESSION")?; } else {
None
};
let cwd = if *cwd {
Some(env::current_dir()?.display().to_string())
} else {
None
};
let query_session = format!("select * from history where session = {};", session); let history = match (session, cwd) {
(None, None) => db.list(None, false).await?,
let query_dir = format!("select * from history where cwd = {};", cwd); (None, Some(cwd)) => {
let query_session_dir = format!( let query = format!("select * from history where cwd = {};", cwd);
"select * from history where cwd = {} and session = {};", db.query_history(&query).await?
cwd, session }
); (Some(session), None) => {
let query = format!("select * from history where session = {};", session);
let history = match params { db.query_history(&query).await?
(false, false) => db.list(None, false).await?, }
(true, false) => db.query_history(query_session.as_str()).await?, (Some(session), Some(cwd)) => {
(false, true) => db.query_history(query_dir.as_str()).await?, let query = format!(
(true, true) => db.query_history(query_session_dir.as_str()).await?, "select * from history where cwd = {} and session = {};",
cwd, session
);
db.query_history(&query).await?
}
}; };
print_list(&history, *human, *cmd_only); print_list(&history, *human, *cmd_only);