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