From 54230bd85b7a65f1e939c0d795bb645c92e23ac6 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Wed, 5 Feb 2020 22:48:35 +0100 Subject: [PATCH] show commands in help screen in the same syntax they are parsed --- Cargo.lock | 20 ++++++++++++++ Cargo.toml | 2 ++ src/command.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 3 +++ src/queue.rs | 3 ++- src/ui/help.rs | 2 +- 6 files changed, 95 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6a28ea..138d871 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1672,6 +1672,8 @@ dependencies = [ "rspotify 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)", + "strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strum_macros 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2720,6 +2722,22 @@ name = "strsim" version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "strum" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "strum_macros" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "subtle" version = "1.0.0" @@ -3772,6 +3790,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" +"checksum strum 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "530efb820d53b712f4e347916c5e7ed20deb76a4f0457943b3182fb889b06d2c" +"checksum strum_macros 0.17.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e6e163a520367c465f59e0a61a23cfae3b10b6546d78b6f672a382be79f7110" "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5" diff --git a/Cargo.toml b/Cargo.toml index b092f03..46222c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,8 @@ rand = "0.7" webbrowser = "0.5" clipboard = { version = "0.5", optional = true } url = "1.7" +strum = "0.17.1" +strum_macros = "0.17.1" [dependencies.cursive] version = "0.14" diff --git a/src/command.rs b/src/command.rs index 5f43886..63cdfdd 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,7 +1,10 @@ use queue::RepeatSetting; use std::collections::HashMap; +use std::fmt; use std::iter::FromIterator; +use strum_macros::Display; + #[derive(Clone, Serialize, Deserialize, Debug)] pub enum SeekInterval { Forward, @@ -9,13 +12,15 @@ pub enum SeekInterval { Custom(usize), } -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Display, Clone, Serialize, Deserialize, Debug)] +#[strum(serialize_all = "lowercase")] pub enum TargetMode { Current, Selected, } -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Display, Clone, Serialize, Deserialize, Debug)] +#[strum(serialize_all = "lowercase")] pub enum MoveMode { Up, Down, @@ -23,13 +28,15 @@ pub enum MoveMode { Right, } -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Display, Clone, Serialize, Deserialize, Debug)] +#[strum(serialize_all = "lowercase")] pub enum ShiftMode { Up, Down, } -#[derive(Clone, Serialize, Deserialize, Debug)] +#[derive(Display, Clone, Serialize, Deserialize, Debug)] +#[strum(serialize_all = "lowercase")] pub enum GotoMode { Album, Artist, @@ -41,6 +48,18 @@ pub enum SeekDirection { Absolute(u32), } +impl fmt::Display for SeekDirection { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let repr = match self { + SeekDirection::Absolute(pos) => format!("{}", pos), + SeekDirection::Relative(delta) => { + format!("{}{}", if delta > &0 { "+" } else { "" }, delta) + } + }; + write!(f, "{}", repr) + } +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub enum Command { Quit, @@ -70,6 +89,48 @@ pub enum Command { Search(String), } +impl fmt::Display for Command { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let repr = match self { + Command::Quit => "quit".to_string(), + Command::TogglePlay => "playpause".to_string(), + Command::Stop => "stop".to_string(), + Command::Previous => "previous".to_string(), + Command::Next => "next".to_string(), + Command::Clear => "clear".to_string(), + Command::Queue => "queue".to_string(), + Command::Play => "play".to_string(), + Command::UpdateLibrary => "update".to_string(), + Command::Save => "save".to_string(), + Command::SaveQueue => "save queue".to_string(), + Command::Delete => "delete".to_string(), + Command::Focus(tab) => format!("focus {}", tab), + Command::Seek(direction) => format!("seek {}", direction), + Command::VolumeUp => "volup".to_string(), + Command::VolumeDown => "voldown".to_string(), + Command::Repeat(mode) => { + let param = match mode { + Some(mode) => format!("{}", mode), + None => "".to_string(), + }; + format!("repeat {}", param) + } + Command::Shuffle(on) => { + let param = on.map(|x| if x == true { "on" } else { "off" }); + format!("shuffle {}", param.unwrap_or("")) + } + Command::Share(mode) => format!("share {}", mode), + Command::Back => "back".to_string(), + Command::Open(mode) => format!("open {}", mode), + Command::Goto(mode) => format!("goto {}", mode), + Command::Move(mode, amount) => format!("move {} {}", mode, amount.unwrap_or(1)), + Command::Shift(mode, amount) => format!("shift {} {}", mode, amount.unwrap_or(1)), + Command::Search(term) => format!("search {}", term), + }; + write!(f, "{}", repr) + } +} + fn register_aliases(map: &mut HashMap<&str, &str>, cmd: &'static str, names: Vec<&'static str>) { for a in names { map.insert(a, cmd); @@ -212,6 +273,8 @@ pub fn parse(input: &str) -> Option { "queue" => Command::SaveQueue, _ => Command::Save, }), + "volup" => Some(Command::VolumeUp), + "voldown" => Some(Command::VolumeDown), _ => None, } } diff --git a/src/main.rs b/src/main.rs index 38f066e..c0f5f87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,9 @@ extern crate fern; extern crate rand; extern crate url; +extern crate strum; +extern crate strum_macros; + use std::fs; use std::path::PathBuf; use std::process; diff --git a/src/queue.rs b/src/queue.rs index d641530..b0a0352 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -1,11 +1,12 @@ use std::sync::{Arc, RwLock}; use rand::prelude::*; +use strum_macros::Display; use spotify::Spotify; use track::Track; -#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] +#[derive(Display, Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] pub enum RepeatSetting { None, RepeatPlaylist, diff --git a/src/ui/help.rs b/src/ui/help.rs index f64a568..7a606d0 100644 --- a/src/ui/help.rs +++ b/src/ui/help.rs @@ -27,7 +27,7 @@ impl HelpView { keys.sort(); for key in keys { - let command = serde_json::to_string(&bindings[key]).unwrap_or_default(); + let command = &bindings[key]; let binding = format!("{} -> {}\n", key, command); text.append(binding); }