Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

music: remove seek_step, add seek_step_secs #1718

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/blocks/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! `player` | Name(s) of the music player(s) MPRIS interface. This can be either a music player name or an array of music player names. Run <code>busctl --user list &vert; grep "org.mpris.MediaPlayer2." &vert; cut -d' ' -f1</code> and the name is the part after "org.mpris.MediaPlayer2.". | `None`
//! `interface_name_exclude` | A list of regex patterns for player MPRIS interface names to ignore. | `[]`
//! `separator` | String to insert between artist and title. | `" - "`
//! `seek_step` | Number of microseconds to seek forward/backward when scrolling on the bar. | `1000`
//! `seek_step_secs` | Positive number of seconds to seek forward/backward when scrolling on the bar. Does not need to be an integer. | `1`
//!
//! Note: All placeholders exctpt `icon` can be absent. See the examples below to learn how to handle this.
//!
Expand Down Expand Up @@ -119,8 +119,8 @@ pub struct Config {
interface_name_exclude: Vec<String>,
#[default(" - ".into())]
separator: String,
#[default(1_000)]
seek_step: i64,
#[default(1.into())]
seek_step_secs: Seconds<false>,
}

#[derive(Deserialize, Debug, Clone, SmartDefault)]
Expand Down Expand Up @@ -346,10 +346,10 @@ pub async fn run(config: Config, mut api: CommonApi) -> Result<()> {
break;
}
"seek_forward" => {
player.seek(config.seek_step).await?;
player.seek(config.seek_step_secs.0.as_micros() as i64).await?;
}
"seek_backward" => {
player.seek(-config.seek_step).await?;
player.seek(-(config.seek_step_secs.0.as_micros() as i64)).await?;
}
_ => (),
}
Expand Down
16 changes: 8 additions & 8 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::borrow::Cow;
use std::time::Duration;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Seconds(pub Duration);
pub struct Seconds<const ALLOW_ONCE: bool = true>(pub Duration);

impl From<u64> for Seconds {
impl<const ALLOW_ONCE: bool> From<u64> for Seconds<ALLOW_ONCE> {
fn from(v: u64) -> Self {
Self::new(v)
}
}

impl Seconds {
impl<const ALLOW_ONCE: bool> Seconds<ALLOW_ONCE> {
pub fn new(value: u64) -> Self {
Self(Duration::from_secs(value))
}
Expand All @@ -28,15 +28,15 @@ impl Seconds {
}
}

impl<'de> Deserialize<'de> for Seconds {
impl<'de, const ALLOW_ONCE: bool> Deserialize<'de> for Seconds<ALLOW_ONCE> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SecondsVisitor;
struct SecondsVisitor<const ALLOW_ONCE: bool>;

impl<'de> de::Visitor<'de> for SecondsVisitor {
type Value = Seconds;
impl<'de, const ALLOW_ONCE: bool> de::Visitor<'de> for SecondsVisitor<ALLOW_ONCE> {
type Value = Seconds<ALLOW_ONCE>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("\"once\", i64 or f64")
Expand All @@ -46,7 +46,7 @@ impl<'de> Deserialize<'de> for Seconds {
where
E: de::Error,
{
if v == "once" {
if ALLOW_ONCE && v == "once" {
Ok(Seconds(Duration::from_secs(60 * 60 * 24 * 365)))
} else {
Err(E::custom(format!("'{v}' is not a valid duration")))
Expand Down