Skip to content

Commit

Permalink
Refactor friendly_path
Browse files Browse the repository at this point in the history
Reason:
        There is an `if let` statement in Rust
        that can help simplify code in such cases.
        For more have a look at Rust by Example book:
        https://rustbyexample.com/flow_control/if_let.html
  • Loading branch information
Bogdan Arabadzhi authored and xcambar committed Oct 18, 2017
1 parent 830d0b6 commit 2195b2c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/precmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ fn first_char(s: &str) -> String {
}

fn fmt_current_path(cwd: &str) -> String {
let home: Regex = match env::home_dir() {
Some(path) => Regex::new(path.to_str().unwrap()).unwrap(),
None => Regex::new("").unwrap(),
let friendly_path = if let Some(path) = env::home_dir() {
Regex::new(path.to_str().unwrap()).unwrap().replace(cwd, "~").to_string()
} else {
String::from("")
};
let friendly_path = home.replace(cwd, "~").to_string();

let mut friendly_path_split: Vec<&str> = friendly_path.split("/").collect();
let current_dir = friendly_path_split.pop().unwrap().to_string();
let mut short_path: Vec<String> = friendly_path_split.iter().map(|s| first_char(s)).collect();
Expand Down

0 comments on commit 2195b2c

Please sign in to comment.