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> {
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 = data_dir.as_path();
create_dir_all(config_dir)?;
create_dir_all(data_dir)?;
create_dir_all(&config_dir)?;
create_dir_all(&data_dir)?;
let mut config_file = if let Ok(p) = std::env::var("ATUIN_CONFIG_DIR") {
PathBuf::from(p)

View File

@ -30,39 +30,60 @@ pub fn uuid_v4() -> String {
Uuid::new_v4().to_simple().to_string()
}
pub fn config_dir() -> PathBuf {
// TODO: more reliable, more tested
// 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.
// TODO: more reliable, more tested
// 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.
pub fn home_dir() -> PathBuf {
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(
|_| {
let mut config = home.clone();
config.push(".config");
config.push("atuin");
config
},
PathBuf::from,
)
pub fn config_dir() -> PathBuf {
let config_dir =
std::env::var("XDG_CONFIG_HOME").map_or_else(|_| home_dir().join(".config"), PathBuf::from);
config_dir.join("atuin")
}
pub fn data_dir() -> PathBuf {
// TODO: more reliable, more tested
// 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.
let home = std::env::var("HOME").expect("$HOME not found");
let home = PathBuf::from(home);
let data_dir = std::env::var("XDG_DATA_HOME")
.map_or_else(|_| home_dir().join(".local").join("share"), PathBuf::from);
std::env::var("XDG_DATA_HOME").map_or_else(
|_| {
let mut data = home.clone();
data.push(".local");
data.push("share");
data.push("atuin");
data
},
PathBuf::from,
)
data_dir.join("atuin")
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
#[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,
cmd_only,
} => {
let params = (session, cwd);
let cwd = env::current_dir()?.display().to_string();
let session = env::var("ATUIN_SESSION")?;
let session = if *session {
Some(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 query_dir = format!("select * from history where cwd = {};", cwd);
let query_session_dir = format!(
"select * from history where cwd = {} and session = {};",
cwd, session
);
let history = match params {
(false, false) => db.list(None, false).await?,
(true, false) => db.query_history(query_session.as_str()).await?,
(false, true) => db.query_history(query_dir.as_str()).await?,
(true, true) => db.query_history(query_session_dir.as_str()).await?,
let history = match (session, cwd) {
(None, None) => db.list(None, false).await?,
(None, Some(cwd)) => {
let query = format!("select * from history where cwd = {};", cwd);
db.query_history(&query).await?
}
(Some(session), None) => {
let query = format!("select * from history where session = {};", session);
db.query_history(&query).await?
}
(Some(session), Some(cwd)) => {
let query = format!(
"select * from history where cwd = {} and session = {};",
cwd, session
);
db.query_history(&query).await?
}
};
print_list(&history, *human, *cmd_only);