Skip to content

Commit

Permalink
Merge pull request #78 from Julien-cpsn/ckaznable/main
Browse files Browse the repository at this point in the history
Add support for home directory symbol (~) in file paths
  • Loading branch information
Julien-cpsn authored May 17, 2024
2 parents 75fb892 + c5d4dc6 commit f03da1b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
49 changes: 49 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ boa_engine = { version = "0.18.0", default-features = false }
parse_postman_collection = "0.2.3"
curl-parser = { version = "0.3.1", default-features = false }
clap = { version = "4.5.4", features = ["derive", "color", "suggestions"] }
dirs = "5.0.1"
arboard = "3.3.2"
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread", "macros"] }
parking_lot = { version = "0.12.2", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ https://github.com/NachoNievaG/atac.nvim
| [My fork](https://github.com/Julien-cpsn/postman-collection-rs) of [postman_collection](https://github.com/mandrean/postman-collection-rs) | 0.2.1 | Deserialize Postman collection files |
| [curl-parser](https://github.com/tyrchen/curl-parser) | 0.3.1 | Parse cURL request files |
| [clap](https://github.com/clap-rs/clap) | 4.5.0 | Command Line Argument Parser |
| [dirs](https://github.com/dirs-dev/dirs-rs) | 5.0.1 | Use system files |
| [arboard](https://github.com/1Password/arboard) | 3.3.2 | Copy response body to clipboard |
| [tokio](https://github.com/tokio-rs/tokio) | 1.0.0 | Handle asynchronous requests |
| [parking_lot](https://github.com/Amanieu/parking_lot) | 0.12.2 | Smaller, faster and more flexible implementation of RwLock and Mutex. Used everywhere. |
Expand Down
5 changes: 3 additions & 2 deletions src/app/files/key_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ratatui::text::Span;
use serde::Deserialize;

use crate::app::app::App;
use crate::app::files::utils::expand_tilde;
use crate::panic_error;

#[derive(Default, Copy, Clone, Deserialize)]
Expand Down Expand Up @@ -272,7 +273,7 @@ impl App<'_> {
pub fn parse_key_bindings_file(&mut self) {
let path = match env::var("ATAC_KEY_BINDINGS") {
// If the ATAC_KEY_BINDINGS environment variable exists
Ok(env_key_bindings) => PathBuf::from(env_key_bindings),
Ok(env_key_bindings) => expand_tilde(PathBuf::from(env_key_bindings)),
Err(_) => {
println!("No key bindings file found\n");
return;
Expand Down Expand Up @@ -307,4 +308,4 @@ pub fn unique_key_and_help(help: Span<'static>, key: Span<'static>) -> Vec<Span<
else {
vec![help, Span::raw(" "), key]
}
}
}
3 changes: 2 additions & 1 deletion src/app/files/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod environment;
pub mod log;
pub mod config;
pub mod key_bindings;
pub mod import;
pub mod import;
pub mod utils;
15 changes: 15 additions & 0 deletions src/app/files/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::path::PathBuf;

pub(crate) fn expand_tilde(path_buf: PathBuf) -> PathBuf {
if !path_buf.starts_with("~/") {
return path_buf;
}

match dirs::home_dir() {
Some(mut home_dir) => {
home_dir.push(path_buf.strip_prefix("~/").unwrap());
return home_dir;
},
None => panic!("No home directory found when trying to expand ~")
}
}
7 changes: 4 additions & 3 deletions src/app/startup/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use clap::{Parser, Subcommand};
use clap::builder::Styles;
use lazy_static::lazy_static;
use crate::app::files::utils::expand_tilde;
use crate::panic_error;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -77,11 +78,11 @@ lazy_static! {

let (directory, is_directory_from_env) = match args.directory {
// If a directory was provided with a CLI argument
Some(arg_directory) => (arg_directory, false),
Some(arg_directory) => (expand_tilde(arg_directory), false),
// If no directory was provided with the CLI
None => match env::var("ATAC_MAIN_DIR") {
// If the ATAC_MAIN_DIR environment variable exists
Ok(env_directory) => (PathBuf::from(env_directory), true),
Ok(env_directory) => (expand_tilde(PathBuf::from(env_directory)), true),
Err(_) => panic_error("No directory provided, provide one either with `--directory <dir>` or via the environment variable `ATAC_MAIN_DIR`")
}
};
Expand All @@ -93,4 +94,4 @@ lazy_static! {
should_save: !args.dry_run
}
};
}
}

0 comments on commit f03da1b

Please sign in to comment.