Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 568e8c48 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

LKT-RS: Can now use a range, a count or let the argument empty when listing karas in the queue

parent d4a62e63
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -17,6 +17,14 @@ struct Args { ...@@ -17,6 +17,14 @@ struct Args {
pub verbose: u8, pub verbose: u8,
} }
/// Specify which to display karas from the queue.
#[derive(Debug, Clone)]
pub enum LktQueuePositions {
Range(Range<usize>),
NextItems(usize),
Default,
}
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
#[command(long_about = None, about = None)] #[command(long_about = None, about = None)]
enum SubCommand { enum SubCommand {
...@@ -40,14 +48,16 @@ enum SubCommand { ...@@ -40,14 +48,16 @@ enum SubCommand {
status: bool, status: bool,
#[arg( action = clap::ArgAction::Set #[arg( action = clap::ArgAction::Set
, value_parser = crate::parsers::RangeParser::new() , value_parser = crate::parsers::QueuePositionParser::new()
, exclusive = true , exclusive = true
, short = 'l' , short = 'l'
, long = "list" , long = "list"
, help = "Prints the names and ids of karas in the queue. Karas are designated by a range of positions." , id = "LOWER:UPPER|COUNT"
, id = "LOWER:UPPER" , help = concat!( "Prints the names and ids of karas in the queue. Karas are designated by a range of positions, "
)] , "or the count to display from the playing kara. If no option is passed, the default number of "
pos: Option<Option<Range<usize>>>, , "karas is displayed from the currently played one."
))]
pos: Option<Option<LktQueuePositions>>,
#[arg( action = clap::ArgAction::SetTrue #[arg( action = clap::ArgAction::SetTrue
, exclusive = true , exclusive = true
...@@ -331,6 +341,9 @@ pub(crate) enum LktQueueCommand { ...@@ -331,6 +341,9 @@ pub(crate) enum LktQueueCommand {
List { List {
range: Range<usize>, range: Range<usize>,
}, },
ListNext {
count: Option<usize>,
},
Next, Next,
Previous, Previous,
...@@ -433,8 +446,6 @@ impl LktCommand { ...@@ -433,8 +446,6 @@ impl LktCommand {
SubCommand::Queue { shuffle: Some(lvl), .. } => Queue(LktQueueCommand::Shuffle { up_to_lvl: lvl.unwrap_or(LektorPriorityLevel::Enforce) }), SubCommand::Queue { shuffle: Some(lvl), .. } => Queue(LktQueueCommand::Shuffle { up_to_lvl: lvl.unwrap_or(LektorPriorityLevel::Enforce) }),
SubCommand::Queue { clear: Some(lvl), .. } => Queue(LktQueueCommand::Clear { up_to_lvl: lvl.unwrap_or(LektorPriorityLevel::Enforce) }), SubCommand::Queue { clear: Some(lvl), .. } => Queue(LktQueueCommand::Clear { up_to_lvl: lvl.unwrap_or(LektorPriorityLevel::Enforce) }),
SubCommand::Queue { seek: Some(id), .. } => { Queue(LktQueueCommand::SeekIdInQueue { id }) } SubCommand::Queue { seek: Some(id), .. } => { Queue(LktQueueCommand::SeekIdInQueue { id }) }
SubCommand::Queue { pos: Some(None), .. } => Queue(LktQueueCommand::List { range: 0..config.search.default_queue_count }),
SubCommand::Queue { pos: Some(Some(range)), .. } => Queue(LktQueueCommand::List { range }),
SubCommand::Queue { play: Some(index), .. } => Queue(LktQueueCommand::Play { index }), SubCommand::Queue { play: Some(index), .. } => Queue(LktQueueCommand::Play { index }),
SubCommand::Queue { swap: Some(args), .. } => match &args[..] { SubCommand::Queue { swap: Some(args), .. } => match &args[..] {
[p1, p2] => Queue(LktQueueCommand::SwapPositions { [p1, p2] => Queue(LktQueueCommand::SwapPositions {
...@@ -457,6 +468,10 @@ impl LktCommand { ...@@ -457,6 +468,10 @@ impl LktCommand {
.map_err(|e| format!("invalid query in queue add command: {e}"))? .map_err(|e| format!("invalid query in queue add command: {e}"))?
}), }),
} }
SubCommand::Queue { pos: Some(Some(LktQueuePositions::Default)), .. }
| SubCommand::Queue { pos: Some(None), .. } => Queue(LktQueueCommand::ListNext { count: None }),
SubCommand::Queue { pos: Some(Some(LktQueuePositions::NextItems(count))), .. } => Queue(LktQueueCommand::ListNext { count: Some(count) }),
SubCommand::Queue { pos: Some(Some(LktQueuePositions::Range(range))), .. } => Queue(LktQueueCommand::List { range }),
// SEARCH COMMANDS // // SEARCH COMMANDS //
......
...@@ -63,6 +63,8 @@ async fn handle_cmd_queue(_: LktConfig, mut conn: LektorConnexion, cmd: LktQueue ...@@ -63,6 +63,8 @@ async fn handle_cmd_queue(_: LktConfig, mut conn: LektorConnexion, cmd: LktQueue
}) })
} }
LktQueueCommand::List { range } => todo!(), LktQueueCommand::List { range } => todo!(),
LktQueueCommand::ListNext { count } => todo!(),
LktQueueCommand::Next => send!(conn => LektorQuery::PlayNext; ok), LktQueueCommand::Next => send!(conn => LektorQuery::PlayNext; ok),
LktQueueCommand::Previous => send!(conn => LektorQuery::PlayPrevious; ok), LktQueueCommand::Previous => send!(conn => LektorQuery::PlayPrevious; ok),
LktQueueCommand::Pause => { LktQueueCommand::Pause => {
......
...@@ -5,16 +5,16 @@ use std::ops::Range; ...@@ -5,16 +5,16 @@ use std::ops::Range;
/// A parser for the [std::ops::Range] structure /// A parser for the [std::ops::Range] structure
#[derive(Copy, Clone, Debug, Default)] #[derive(Copy, Clone, Debug, Default)]
#[non_exhaustive] #[non_exhaustive]
pub struct RangeParser {} pub struct QueuePositionParser {}
impl RangeParser { impl QueuePositionParser {
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
} }
} }
impl TypedValueParser for RangeParser { impl TypedValueParser for QueuePositionParser {
type Value = Range<usize>; type Value = crate::args::LktQueuePositions;
fn parse_ref( fn parse_ref(
&self, &self,
...@@ -22,6 +22,7 @@ impl TypedValueParser for RangeParser { ...@@ -22,6 +22,7 @@ impl TypedValueParser for RangeParser {
arg: Option<&clap::Arg>, arg: Option<&clap::Arg>,
value: &std::ffi::OsStr, value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> { ) -> Result<Self::Value, clap::Error> {
use crate::args::LktQueuePositions as QPV;
let name = cmd.get_name(); let name = cmd.get_name();
let arg = arg let arg = arg
.map(ToString::to_string) .map(ToString::to_string)
...@@ -32,8 +33,16 @@ impl TypedValueParser for RangeParser { ...@@ -32,8 +33,16 @@ impl TypedValueParser for RangeParser {
format!("not a valid utf8 string: {}", value.to_string_lossy()), format!("not a valid utf8 string: {}", value.to_string_lossy()),
) )
})?; })?;
match value.split(':').collect::<Vec<_>>()[..] { let value = value.trim();
[lower, upper] => {
if value.is_empty() {
// Default value
Ok(QPV::Default)
} else if let Ok(value) = value.parse::<usize>() {
// A count from the playing kara
Ok(QPV::NextItems(value))
} else if let [lower, upper] = value.split(':').collect::<Vec<_>>()[..] {
// The range
let lower = lower.parse::<usize>().map_err(|e| { let lower = lower.parse::<usize>().map_err(|e| {
clap::Error::raw( clap::Error::raw(
ErrorKind::InvalidValue, ErrorKind::InvalidValue,
...@@ -46,15 +55,16 @@ impl TypedValueParser for RangeParser { ...@@ -46,15 +55,16 @@ impl TypedValueParser for RangeParser {
format!("in `{name} {arg}`, the value is not an integer: {e}"), format!("in `{name} {arg}`, the value is not an integer: {e}"),
) )
})?; })?;
either!(lower <= upper => Ok(lower..upper); Err(clap::Error::raw( either!(lower <= upper => Ok(QPV::Range(lower..upper)); Err(clap::Error::raw(
ErrorKind::InvalidValue, ErrorKind::InvalidValue,
format!("in `{name} {arg}`, the range must be valid, unlike `{lower}:{upper}`, did you meant to write `{upper}:{lower}` instead?"), format!("in `{name} {arg}`, the range must be valid, unlike `{lower}:{upper}`, did you meant to write `{upper}:{lower}` instead?"),
))) )))
} } else {
_ => Err(clap::Error::raw( // Error
Err(clap::Error::raw(
ErrorKind::InvalidValue, ErrorKind::InvalidValue,
format!("in `{name} {arg}`, the value should be of the form: `[lower]:[upper]`"), format!("in `{name} {arg}`, the value should be of the form: `[lower]:[upper]` or `[count]` or empty"),
)), ))
} }
} }
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter