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

LKT: Separate the queue and playback in lkt

parent 1c58abbb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!197Draft: Refactor the whole code.
...@@ -106,8 +106,6 @@ Please, refer to the [CONTRIBUTING](CONTRIBUTING.md) file for any contributions. ...@@ -106,8 +106,6 @@ Please, refer to the [CONTRIBUTING](CONTRIBUTING.md) file for any contributions.
# TODO List # TODO List
- Finish lkt.
- Separate teh playback and the queue commands...
- Finish Amadeus. - Finish Amadeus.
- Finish popups. - Finish popups.
- Query state from lektord in a sensible way. - Query state from lektord in a sensible way.
......
...@@ -27,9 +27,9 @@ pub struct Args { ...@@ -27,9 +27,9 @@ pub struct Args {
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
#[command(long_about = None, about = None)] #[command(long_about = None, about = None)]
pub enum SubCommand { pub enum SubCommand {
/// Queue commands. Manipulate the play queue and the playback status. /// Playback commands. Manipulate the playback status of lektord.
#[command(short_flag = 'Q', arg_required_else_help = true)] #[command(short_flag = 'P', arg_required_else_help = true)]
Queue { Playback {
/// Prints informations about the currently playing kara. Can be used to display the /// Prints informations about the currently playing kara. Can be used to display the
/// current kara in a status bar like xmobar or in the i3 panel. /// current kara in a status bar like xmobar or in the i3 panel.
#[arg( action = clap::ArgAction::SetTrue #[arg( action = clap::ArgAction::SetTrue
...@@ -47,17 +47,6 @@ pub enum SubCommand { ...@@ -47,17 +47,6 @@ pub enum SubCommand {
)] )]
status: bool, status: bool,
/// Prints the names and ids of karas in the queue. Karas are designated by a range of
/// positions.
#[arg( action = clap::ArgAction::Set
, value_parser = parsers::RangeParser::new()
, exclusive = true
, short = 'l'
, long = "list"
, value_name = "RANGE"
)]
pos: Option<Option<lektor_payloads::Range>>,
/// Play the next kara in the queue. /// Play the next kara in the queue.
#[arg( action = clap::ArgAction::SetTrue #[arg( action = clap::ArgAction::SetTrue
, exclusive = true , exclusive = true
...@@ -100,6 +89,21 @@ pub enum SubCommand { ...@@ -100,6 +89,21 @@ pub enum SubCommand {
, long = "stop" , long = "stop"
)] )]
stop: bool, stop: bool,
},
/// Queue commands. Manipulate the play queue of lektord.
#[command(short_flag = 'Q', arg_required_else_help = true)]
Queue {
/// Prints the names and ids of karas in the queue. Karas are designated by a range of
/// positions.
#[arg( action = clap::ArgAction::Set
, value_parser = parsers::RangeParser::new()
, exclusive = true
, short = 'l'
, long = "list"
, value_name = "RANGE"
)]
pos: Option<Option<lektor_payloads::Range>>,
/// Add karas to the queue when they are matching the query. The priority can be passed as /// Add karas to the queue when they are matching the query. The priority can be passed as
/// a numeric or as its string representation. /// a numeric or as its string representation.
...@@ -227,7 +231,7 @@ pub enum SubCommand { ...@@ -227,7 +231,7 @@ pub enum SubCommand {
}, },
/// Playlist commands. Create, delete and populate playlists. /// Playlist commands. Create, delete and populate playlists.
#[command(short_flag = 'P', arg_required_else_help = true)] #[command(short_flag = 'L', arg_required_else_help = true)]
Playlist { Playlist {
/// Create a new playlist with a specific name. /// Create a new playlist with a specific name.
#[arg( value_parser = clap::builder::NonEmptyStringValueParser::new() #[arg( value_parser = clap::builder::NonEmptyStringValueParser::new()
......
mod range_parser;
pub use range_parser::*; use clap::builder::TypedValueParser;
use lektor_payloads::Range;
macro_rules! clap_error { macro_rules! clap_error {
($($err: ident)? ( $kind: ident, $fmt: literal $(,$str: expr)* )) => { ($($err: ident)? ( $kind: ident, $fmt: literal $(,$str: expr)* )) => {
...@@ -27,3 +27,30 @@ fn prepare_args( ...@@ -27,3 +27,30 @@ fn prepare_args(
let value = value.trim().to_string(); let value = value.trim().to_string();
Ok((name, arg, value)) Ok((name, arg, value))
} }
/// A parser for the [std::ops::Range] structure
#[derive(Copy, Clone, Debug, Default)]
#[non_exhaustive]
pub struct RangeParser {}
impl RangeParser {
pub fn new() -> Self {
Default::default()
}
}
impl TypedValueParser for RangeParser {
type Value = Range;
fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let (name, arg, value) = prepare_args(cmd, arg, value)?;
value.parse().map_err(|err| {
clap_error! { (InvalidValue, "in `{name} {arg}`, {err}") }
})
}
}
use crate::args::parsers::{clap_error, prepare_args};
use clap::builder::TypedValueParser;
use lektor_payloads::Range;
/// A parser for the [std::ops::Range] structure
#[derive(Copy, Clone, Debug, Default)]
#[non_exhaustive]
pub struct RangeParser {}
impl RangeParser {
pub fn new() -> Self {
Default::default()
}
}
impl TypedValueParser for RangeParser {
type Value = Range;
fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let (name, arg, value) = prepare_args(cmd, arg, value)?;
value.parse().map_err(|err| {
clap_error! { (InvalidValue, "in `{name} {arg}`, {err}") }
})
}
}
...@@ -75,6 +75,79 @@ async fn exec_lkt(config: LktConfig, cmd: SubCommand) -> Result<()> { ...@@ -75,6 +75,79 @@ async fn exec_lkt(config: LktConfig, cmd: SubCommand) -> Result<()> {
// Queue options // // Queue options //
// ============= // // ============= //
// Display current kara in one line (for status lines...)
Playback { current: true, .. } => {
let PlayStateWithCurrent { state, current } =
requests::get_status(config.clone()).await?;
match current {
None => println!("[{state:?}]"),
Some((kid, elapsed, duration)) => {
let current = requests::get_kara_by_kid(config, kid).await?.to_string();
println!("[{state:?}] {elapsed}/{duration} {current}");
}
}
Ok(())
}
// Display the status of lektord
Playback { status: true, .. } => {
let Infos { version } = requests::get_infos(config.clone()).await?;
let PlayStateWithCurrent { state, current } =
requests::get_status(config.clone()).await?;
let current = match current {
None => None,
Some((kid, elapsed, duration)) => Some((
requests::get_kara_by_kid(config.clone(), kid).await?,
elapsed,
duration,
)),
};
let queue_counts = requests::get_queue_count(config.clone()).await?;
let history_count = requests::get_history_count(config).await?;
println!("Version: {version}");
println!("Playback State: {state:?}");
if let Some((current, elapsed, duration)) = current {
println!("Current Kara: {current}");
println!("Kara elapsed: {elapsed}s");
println!("Kara duration: {duration}s");
}
println!("Karas in Queue: {queue_counts:?}");
println!("Karas in Hustory: {history_count}");
Ok(())
}
// Play from a position in the queue
Playback {
play: Some(play), ..
} => requests::play_from_position(config, play.unwrap_or_default()).await,
// Toggle the play state
Playback {
pause: Some(None), ..
} => requests::toggle_playback_state(config).await,
// Pause the playback
Playback {
pause: Some(Some(true)),
..
} => requests::set_playback_state(config, PlayState::Pause).await,
// Force to resume the playback
Playback {
pause: Some(Some(false)),
..
} => requests::set_playback_state(config, PlayState::Play).await,
Playback { next: true, .. } => requests::play_next(config).await,
Playback { prev: true, .. } => requests::play_previous(config).await,
Playback { stop: true, .. } => requests::set_playback_state(config, PlayState::Stop).await,
// ============= //
// Queue options //
// ============= //
// List kara in the queue // List kara in the queue
Queue { pos: Some(pos), .. } => { Queue { pos: Some(pos), .. } => {
let pos = pos.unwrap_or_default(); let pos = pos.unwrap_or_default();
...@@ -120,75 +193,6 @@ async fn exec_lkt(config: LktConfig, cmd: SubCommand) -> Result<()> { ...@@ -120,75 +193,6 @@ async fn exec_lkt(config: LktConfig, cmd: SubCommand) -> Result<()> {
remove: Some(r), .. remove: Some(r), ..
} => requests::remove_range_from_queue(config, r.unwrap_or_default()).await, } => requests::remove_range_from_queue(config, r.unwrap_or_default()).await,
// Play from a position in the queue
Queue {
play: Some(play), ..
} => requests::play_from_position(config, play.unwrap_or_default()).await,
// Toggle the play state
Queue {
pause: Some(None), ..
} => requests::toggle_playback_state(config).await,
// Pause the playback
Queue {
pause: Some(Some(true)),
..
} => requests::set_playback_state(config, PlayState::Pause).await,
// Force to resume the playback
Queue {
pause: Some(Some(false)),
..
} => requests::set_playback_state(config, PlayState::Play).await,
// Display current kara in one line (for status lines...)
Queue { current: true, .. } => {
let PlayStateWithCurrent { state, current } =
requests::get_status(config.clone()).await?;
match current {
None => println!("[{state:?}]"),
Some((kid, elapsed, duration)) => {
let current = requests::get_kara_by_kid(config, kid).await?.to_string();
println!("[{state:?}] {elapsed}/{duration} {current}");
}
}
Ok(())
}
// Display the status of lektord
Queue { status: true, .. } => {
let Infos { version } = requests::get_infos(config.clone()).await?;
let PlayStateWithCurrent { state, current } =
requests::get_status(config.clone()).await?;
let current = match current {
None => None,
Some((kid, elapsed, duration)) => Some((
requests::get_kara_by_kid(config.clone(), kid).await?,
elapsed,
duration,
)),
};
let queue_counts = requests::get_queue_count(config.clone()).await?;
let history_count = requests::get_history_count(config).await?;
println!("Version: {version}");
println!("Playback State: {state:?}");
if let Some((current, elapsed, duration)) = current {
println!("Current Kara: {current}");
println!("Kara elapsed: {elapsed}s");
println!("Kara duration: {duration}s");
}
println!("Karas in Queue: {queue_counts:?}");
println!("Karas in Hustory: {history_count}");
Ok(())
}
Queue { next: true, .. } => requests::play_next(config).await,
Queue { prev: true, .. } => requests::play_previous(config).await,
Queue { stop: true, .. } => requests::set_playback_state(config, PlayState::Stop).await,
// Shuffle the queue. // Shuffle the queue.
Queue { Queue {
shuffle: Some(shuffle), shuffle: Some(shuffle),
......
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