Krolyxon
Commit 50918aa

rmptuisrcmpdcommands.rs

168 lines · 4.1 KB · Rust

1//! Constructors for the commands rmptui sends.
2
3use super::protocol::Command;
4
5fn bool_arg(value: bool) -> &'static str {
6 if value { "1" } else { "0" }
7}
8
9pub fn play() -> Command {
10 Command::new("play")
11}
12
13pub fn play_pos(pos: u32) -> Command {
14 Command::new("play").arg(pos.to_string())
15}
16
17pub fn play_id(id: u32) -> Command {
18 Command::new("playid").arg(id.to_string())
19}
20
21pub fn pause(paused: bool) -> Command {
22 Command::new("pause").arg(bool_arg(paused))
23}
24
25pub fn stop() -> Command {
26 Command::new("stop")
27}
28
29pub fn next() -> Command {
30 Command::new("next")
31}
32
33pub fn previous() -> Command {
34 Command::new("previous")
35}
36
37/// Seeks relative to the current position.
38pub fn seek_relative(seconds: i64) -> Command {
39 Command::new("seekcur").arg(format!("{seconds:+}"))
40}
41
42pub fn seek_to(seconds: f64) -> Command {
43 Command::new("seekcur").arg(format!("{seconds:.3}"))
44}
45
46/// Changes the volume relative to the current level. The server applies the
47/// change atomically, so fast repeated key presses are never lost.
48pub fn change_volume(delta: i32) -> Command {
49 Command::new("volume").arg(format!("{delta:+}"))
50}
51
52pub fn set_volume(volume: u8) -> Command {
53 Command::new("setvol").arg(volume.min(100).to_string())
54}
55
56pub fn repeat(on: bool) -> Command {
57 Command::new("repeat").arg(bool_arg(on))
58}
59
60pub fn random(on: bool) -> Command {
61 Command::new("random").arg(bool_arg(on))
62}
63
64pub fn single(on: bool) -> Command {
65 Command::new("single").arg(bool_arg(on))
66}
67
68pub fn consume(on: bool) -> Command {
69 Command::new("consume").arg(bool_arg(on))
70}
71
72pub fn update(path: Option<&str>) -> Command {
73 let command = Command::new("update");
74 match path {
75 Some(path) if !path.is_empty() => command.arg(path),
76 _ => command,
77 }
78}
79
80pub fn clear() -> Command {
81 Command::new("clear")
82}
83
84pub fn shuffle() -> Command {
85 Command::new("shuffle")
86}
87
88/// Adds a song or (recursively) a directory to the queue.
89pub fn add(uri: &str) -> Command {
90 Command::new("add").arg(uri)
91}
92
93pub fn add_id(uri: &str) -> Command {
94 Command::new("addid").arg(uri)
95}
96
97pub fn delete_pos(pos: u32) -> Command {
98 Command::new("delete").arg(pos.to_string())
99}
100
101pub fn delete_id(id: u32) -> Command {
102 Command::new("deleteid").arg(id.to_string())
103}
104
105/// Moves the song at queue position `from` to `to`. Positions (unlike ids)
106/// stay correct when several moves are sent before the queue is refreshed.
107pub fn move_pos(from: u32, to: u32) -> Command {
108 Command::new("move")
109 .arg(from.to_string())
110 .arg(to.to_string())
111}
112
113pub fn find_add(filter: &str) -> Command {
114 Command::new("findadd").arg(filter)
115}
116
117pub fn load(playlist: &str) -> Command {
118 Command::new("load").arg(playlist)
119}
120
121pub fn save(playlist: &str) -> Command {
122 Command::new("save").arg(playlist)
123}
124
125pub fn rename(from: &str, to: &str) -> Command {
126 Command::new("rename").arg(from).arg(to)
127}
128
129pub fn remove_playlist(playlist: &str) -> Command {
130 Command::new("rm").arg(playlist)
131}
132
133/// Adds a song or directory to a stored playlist, creating it if needed.
134pub fn playlist_add(playlist: &str, uri: &str) -> Command {
135 Command::new("playlistadd").arg(playlist).arg(uri)
136}
137
138/// Adds every song matching `filter` to a stored playlist.
139pub fn playlist_add_filter(playlist: &str, filter: &str) -> Command {
140 Command::new("searchaddpl").arg(playlist).arg(filter)
141}
142
143pub fn playlist_delete(playlist: &str, pos: u32) -> Command {
144 Command::new("playlistdelete")
145 .arg(playlist)
146 .arg(pos.to_string())
147}
148
149pub fn playlist_move(playlist: &str, from: u32, to: u32) -> Command {
150 Command::new("playlistmove")
151 .arg(playlist)
152 .arg(from.to_string())
153 .arg(to.to_string())
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[test]
161 fn formats_relative_arguments() {
162 assert_eq!(seek_relative(5).as_str(), r#"seekcur "+5""#);
163 assert_eq!(seek_relative(-5).as_str(), r#"seekcur "-5""#);
164 assert_eq!(change_volume(-2).as_str(), r#"volume "-2""#);
165 assert_eq!(update(None).as_str(), "update");
166 assert_eq!(set_volume(140).as_str(), r#"setvol "100""#);
167 }
168}