show commands in help screen in the same syntax they are parsed

This commit is contained in:
Henrik Friedrichsen
2020-02-05 22:48:35 +01:00
parent cea2cbb33e
commit 54230bd85b
6 changed files with 95 additions and 6 deletions

View File

@@ -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<Command> {
"queue" => Command::SaveQueue,
_ => Command::Save,
}),
"volup" => Some(Command::VolumeUp),
"voldown" => Some(Command::VolumeDown),
_ => None,
}
}

View File

@@ -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;

View File

@@ -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,

View File

@@ -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);
}