/
remove.rs
124 lines (109 loc) · 4.4 KB
/
remove.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::args::RemoveArgs;
use crate::config::UserConfig;
use std::io::{stdout, Write, stdin};
use mprs::utils::{print_table, list_dir};
use std::fs::remove_file;
use std::path::Path;
pub fn mprs_remove(args: &RemoveArgs, config: &UserConfig)
{
// println!("\nDEBUG INFO");
// println!("{:?}", config);
// println!("{:?}", args);
// println!(
// "Query: {:?}\nPlaylist: {:?}",
// args.track, args.playlist
// );
// Get all playlists in base directory.
let mut playlists = list_dir(&config.base_dir);
let mut remove_options = vec![];
match (&args.track, &args.playlist) {
(q, Some(playlist)) => {
let mut playlist_path = config.base_dir.clone();
playlist_path.push(&playlist.clone());
// Catch invalid user input
if !playlist_path.is_dir() {
println!("Playlist {:?} does not exist.\nThere is thus nothing to remove.", playlist_path);
return;
}
for x in list_dir(&playlist_path) {
match q {
Some(query) => {
if x.as_path()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_lowercase()
.contains(&query.to_lowercase()[..])
{
remove_options.push(x);
}
}
None => {
remove_options.push(x);
}
}
}
}
(q, None) => {
for playlist in playlists.iter() {
for track in list_dir(&playlist) {
match q {
Some(query) => {
if track.as_path()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_lowercase()
.contains(&query.to_lowercase()[..])
{
remove_options.push(track);
}
}
None => {
remove_options.push(track);
}
}
}
}
}
}
if remove_options.len() == 0 {
println!("No matching tracks found.");
return;
}
if remove_options.len() == 1 {
// Do not print table of options if there is only one option.
let removed_song = &remove_options[0];
let removed_song_title = Path::new(removed_song.file_stem().unwrap()).file_name().unwrap();
let removed_from_playlist = Path::new(removed_song.parent().unwrap()).file_name().unwrap();
println!("Removed {:?} from playlist {:?}.", removed_song_title, removed_from_playlist);
remove_file(removed_song.to_owned()).unwrap();
return;
}
// Declare index representing which song to delete (assigned via user input if no query term is given).
let id_idx: i32;
let mut table_content = vec![vec!["Song Title".to_string(), "Playlist".to_string()]];
for path in remove_options.iter() {
let song_title = Path::new(path.file_stem().unwrap()).file_name().unwrap().to_os_string();
table_content.push(vec![
song_title.to_str().unwrap().to_string(),
path.parent().unwrap().file_name().unwrap().to_str().unwrap().to_string()
]
);
}
print_table(&table_content);
// Get user input for which song to remove.
let mut input_string = String::new();
print!("Select song to remove by number : ");
let _ = stdout().flush();
stdin().read_line(&mut input_string).unwrap();
// Convert user input to i32 and store in id_idx.
id_idx = input_string.trim().parse().unwrap();
let removed_song = &remove_options[(id_idx - 1) as usize];
let removed_song_title = Path::new(removed_song.file_stem().unwrap()).file_name().unwrap();
let removed_from_playlist = Path::new(removed_song.parent().unwrap()).file_name().unwrap();
println!("Removed {:?} from playlist {:?}.", removed_song_title, removed_from_playlist);
remove_file(removed_song.to_owned()).unwrap();
}