Skip to content

Commit 1502a6d

Browse files
committed
feat: expand ~ to home dir in command line args
1 parent bf0d553 commit 1502a6d

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,33 @@ impl AtomicF32 {
6969
}
7070

7171
pub fn to_vec<R: std::io::BufRead>(reader: R) -> Result<Vec<String>, Box<dyn std::error::Error>> {
72-
use std::env;
73-
74-
// i swear theres a better way to do this lmao
75-
let mut v: Vec<String> = Vec::new();
76-
let home = if cfg!(unix) { env::var("HOME") } else { env::var("USERPROFILE") }
77-
.expect("can't find home dir");
72+
let mut v = Vec::new();
7873

7974
for line in reader.lines() {
8075
let line = line?;
81-
if line.is_empty() {
82-
continue;
83-
}
84-
let line = line.replacen('~', &home, 1);
85-
// dbg!(&line);
76+
let line = if let Some(l) = normalize_line(&line) { l } else { continue };
8677
v.push(line); // fast code
8778
}
8879

8980
Ok(v)
9081
}
9182

83+
pub fn normalize<R: Iterator<Item = String>>(i: R) -> Vec<String> {
84+
let mut vec = Vec::new();
85+
for s in i {
86+
if let Some(s) = normalize_line(&s) { vec.push(s) } else { continue }
87+
}
88+
89+
vec
90+
}
91+
92+
pub fn normalize_line(s: &str) -> Option<String> {
93+
use std::env;
94+
95+
let home = if cfg!(unix) { env::var("HOME") } else { env::var("USERPROFILE") }
96+
.expect("can't find home dir");
97+
98+
if s.is_empty() { return None };
99+
Some(s.replacen('~', &home, 1))
100+
}
101+

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6565
let mut render_requested_mode = RenderMode::default();
6666

6767
if args.len() == 2 {
68-
let mut first_arg = BufReader::new(File::open(&args[1])?);
68+
let mut first_arg = if let Some(s) = encore::normalize_line(&args[1]) { BufReader::new(File::open(s)?) }
69+
else {
70+
quit_with("No such file or directory", "argv[1] not found.")?;
71+
unreachable!("quit_with must quit")
72+
};
6973
match file_format::check_file(&mut first_arg).unwrap() {
7074
FileFormat::Audio => {
71-
parse_playlist(&args, 1).unwrap();
75+
let playlist = encore::normalize(&mut args.into_iter());
76+
parse_playlist(&playlist, 1).unwrap();
7277
}
7378
FileFormat::Other => {
7479
let possible_playlist = encore::to_vec(&mut first_arg).expect("valid utf8");
7580
parse_playlist(&possible_playlist, 0).unwrap();
7681
}
7782
}
7883
} else {
79-
parse_playlist(&args, 1).unwrap();
84+
let file = encore::normalize(&mut args.into_iter());
85+
parse_playlist(&file, 1).unwrap();
8086
}
8187

8288
let playlist_len = PLAYLIST.read().unwrap().len();

0 commit comments

Comments
 (0)