Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct TrackFormat {
pub left: Option<String>,
pub center: Option<String>,
pub right: Option<String>,
pub number_format: Option<String>,
}

impl TrackFormat {
Expand All @@ -52,6 +53,7 @@ impl TrackFormat {
left: Some(String::from("%artists - %title")),
center: Some(String::from("%album")),
right: Some(String::from("%saved %duration")),
number_format: Some(String::from("{:>2}")),
}
}
}
Expand All @@ -61,13 +63,15 @@ impl TrackFormat {
pub struct NotificationFormat {
pub title: Option<String>,
pub body: Option<String>,
pub number_format: Option<String>,
}

impl NotificationFormat {
pub fn default() -> Self {
Self {
title: Some(String::from("%title")),
body: Some(String::from("%artists")),
number_format: Some(String::from("{0:2}")),
}
}
}
Expand Down Expand Up @@ -98,6 +102,7 @@ pub struct ConfigValues {
pub track_format: Option<TrackFormat>,
pub notification_format: Option<NotificationFormat>,
pub statusbar_format: Option<String>,
pub number_format: Option<String>,
pub library_tabs: Option<Vec<LibraryTab>>,
pub hide_display_names: Option<bool>,
pub ap_port: Option<u16>,
Expand Down
37 changes: 36 additions & 1 deletion src/model/playable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::queue::Queue;
use crate::traits::{ListItem, ViewExt};
use crate::utils::ms_to_hms;
use std::fmt;
use std::fmt::Display;
use std::sync::Arc;

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand All @@ -19,8 +20,26 @@ pub enum Playable {
Episode(Episode),
}

fn fmt_number<T: Display>(num_fmt: &str, num: T) -> String {
match num_fmt {
"{:01}" => format!("{:01}", num),
"{:02}" => format!("{:02}", num),
"{:03}" => format!("{:03}", num),
"{:04}" => format!("{:04}", num),
"{:05}" => format!("{:05}", num),
"{:>1}" => format!("{:>1}", num),
"{:>2}" => format!("{:>2}", num),
"{:>3}" => format!("{:>3}", num),
"{:>4}" => format!("{:>4}", num),
"{:>5}" => format!("{:>5}", num),
_ => {
num.to_string()
}
}
}

impl Playable {
pub fn format(playable: &Self, formatting: &str, library: &Library) -> String {
pub fn format(playable: &Self, formatting: &str, number_format: &str, library: &Library) -> String {
formatting
.replace(
"%artists",
Expand Down Expand Up @@ -52,6 +71,22 @@ impl Playable {
}
.as_str(),
)
.replace(
"%track_number",
match playable.clone() {
Self::Episode(episode) => episode.list_index.to_string(),
Self::Track(track) => fmt_number(&number_format, &track.track_number),
}
.as_str(),
)
.replace(
"%disc_number",
match playable.clone() {
Self::Track(track) => track.disc_number.to_string(),
Self::Episode(_episode) => String::new(),
}
.as_str(),
)
.replace(
"%album",
match playable.clone() {
Expand Down
12 changes: 9 additions & 3 deletions src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ impl ListItem for Track {
.clone()
.unwrap_or_default();
let default = config::TrackFormat::default().left.unwrap();
let default_tno_fmt = config::TrackFormat::default().number_format.unwrap();
let left = formatting.left.unwrap_or_else(|| default.clone());
let tno_fmt = formatting.number_format.unwrap_or_else(|| default_tno_fmt.clone());
if left != default {
Playable::format(&Playable::Track(self.clone()), &left, library)
Playable::format(&Playable::Track(self.clone()), &left, &tno_fmt, library)
} else {
format!("{self}")
}
Expand All @@ -201,9 +203,11 @@ impl ListItem for Track {
.clone()
.unwrap_or_default();
let default = config::TrackFormat::default().center.unwrap();
let default_tno_fmt = config::TrackFormat::default().number_format.unwrap();
let center = formatting.center.unwrap_or_else(|| default.clone());
let tno_fmt = formatting.number_format.unwrap_or_else(|| default_tno_fmt.clone());
if center != default {
Playable::format(&Playable::Track(self.clone()), &center, library)
Playable::format(&Playable::Track(self.clone()), &center, &tno_fmt, library)
} else {
self.album.clone().unwrap_or_default()
}
Expand All @@ -217,9 +221,11 @@ impl ListItem for Track {
.clone()
.unwrap_or_default();
let default = config::TrackFormat::default().right.unwrap();
let default_tno_fmt = config::TrackFormat::default().number_format.unwrap();
let right = formatting.right.unwrap_or_else(|| default.clone());
let tno_fmt = formatting.number_format.unwrap_or_else(|| default_tno_fmt.clone());
if right != default {
Playable::format(&Playable::Track(self.clone()), &right, library)
Playable::format(&Playable::Track(self.clone()), &right, &tno_fmt, library)
} else {
let saved = if library.is_saved_track(&Playable::Track(self.clone())) {
if library.cfg.values().use_nerdfont.unwrap_or(false) {
Expand Down
4 changes: 2 additions & 2 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ impl Queue {
let default_body = crate::config::NotificationFormat::default().body.unwrap();
let body = format.body.unwrap_or_else(|| default_body.clone());

let summary_txt = Playable::format(track, &title, &self.library);
let body_txt = Playable::format(track, &body, &self.library);
let summary_txt = Playable::format(track, &title, "", &self.library);
let body_txt = Playable::format(track, &body, "", &self.library);
let cover_url = track.cover_url();
move || send_notification(&summary_txt, &body_txt, cover_url)
});
Expand Down
9 changes: 8 additions & 1 deletion src/ui/statusbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ impl StatusBar {
.statusbar_format
.clone()
.unwrap_or_else(|| "%artists - %title".to_string());
Playable::format(t, &format, &self.library)
let number_format = self
.library
.cfg
.values()
.number_format
.clone()
.unwrap_or_else(|| "{:>2}".to_string());
Playable::format(t, &format, &number_format, &self.library)
}
}

Expand Down
Loading