diff options
author | Matan Kushner <hello@matchai.me> | 2019-04-23 14:51:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-23 14:51:08 -0400 |
commit | bb2bcd604b13d15f7dcb7f7fc1d1cc2ae12dda8d (patch) | |
tree | cd5d7f5099c334bad18f77b092881536c20f33b0 /src/context.rs | |
parent | 33d8beda2d8856ab2fc6673fb54c4b0963b7cc5a (diff) |
Share dir_files between segments through Context (#16)
Diffstat (limited to 'src/context.rs')
-rw-r--r-- | src/context.rs | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/context.rs b/src/context.rs index 32e9fab11..71662fe06 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,30 +1,52 @@ use clap::ArgMatches; use std::env; +use std::fs; use std::path::PathBuf; pub struct Context<'a> { pub current_dir: PathBuf, + pub dir_files: Vec<PathBuf>, pub arguments: ArgMatches<'a>, } impl<'a> Context<'a> { pub fn new(arguments: ArgMatches) -> Context { - // TODO: Currently gets the physical directory. Get the logical directory. let current_dir = env::current_dir().expect("Unable to identify current directory."); - - Context { - current_dir, - arguments, - } + Context::new_with_dir(arguments, current_dir) } + #[allow(dead_code)] pub fn new_with_dir<T>(arguments: ArgMatches, dir: T) -> Context where T: Into<PathBuf>, { + // TODO: Currently gets the physical directory. Get the logical directory. + let current_dir = Context::expand_tilde(dir.into()); + + let dir_files = fs::read_dir(¤t_dir) + .unwrap_or_else(|_| { + panic!( + "Unable to read current directory: {}", + current_dir.to_string_lossy() + ) + }) + .filter_map(Result::ok) + .map(|entry| entry.path()) + .collect::<Vec<PathBuf>>(); + Context { - current_dir: dir.into(), + current_dir, arguments, + dir_files, + } + } + + /// Convert a `~` in a path to the home directory + fn expand_tilde(dir: PathBuf) -> PathBuf { + if dir.starts_with("~") { + let without_home = dir.strip_prefix("~").unwrap(); + return dirs::home_dir().unwrap().join(without_home); } + dir } } |