From 8ff0f477ac4431bdcaac73d5e143671493c522d9 Mon Sep 17 00:00:00 2001 From: talwat <83217276+talwat@users.noreply.github.com> Date: Wed, 17 Dec 2025 22:44:34 +0100 Subject: [PATCH] style: improve a few names --- src/player.rs | 6 +++--- src/tests/ui.rs | 2 +- src/ui.rs | 22 +++++++++++----------- src/ui/interface.rs | 4 ++-- src/ui/interface/titlebar.rs | 2 +- src/ui/mpris.rs | 4 ++-- src/volume.rs | 23 ++++++++++------------- 7 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/player.rs b/src/player.rs index 7d58690..9c892c9 100644 --- a/src/player.rs +++ b/src/player.rs @@ -61,7 +61,7 @@ pub struct Player { rx: Receiver, /// Broadcast channel used to send UI updates. - broadcast: broadcast::Sender, + updater: broadcast::Sender, /// Current playback state (loading or track). current: Current, @@ -106,7 +106,7 @@ impl Player { /// Sends a `ui::Update` to the broadcast channel. pub fn update(&mut self, update: ui::Update) -> crate::Result<()> { - self.broadcast.send(update)?; + self.updater.send(update)?; Ok(()) } @@ -147,7 +147,7 @@ impl Player { waiter: waiter::Handle::new(Arc::clone(&sink), tx), bookmarks: Bookmarks::load().await?, current: Current::default(), - broadcast: utx, + updater: utx, rx, sink, }) diff --git a/src/tests/ui.rs b/src/tests/ui.rs index 445b17d..bb34284 100644 --- a/src/tests/ui.rs +++ b/src/tests/ui.rs @@ -156,7 +156,7 @@ mod interface { sink.set_volume(0.5); let mut state = State::initial(sink, String::from("test")); - state.timer = Some(Instant::now()); + state.volume_timer = Some(Instant::now()); let menu = Interface::default().menu(&mut state); assert_eq!(menu[0], "loading "); diff --git a/src/ui.rs b/src/ui.rs index 74689e0..df1bcf6 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -29,11 +29,11 @@ pub enum Error { #[error("unable to write output: {0}")] Write(#[from] std::io::Error), - #[error("sending message to backend from ui failed: {0}")] - CrateSend(#[from] tokio::sync::mpsc::error::SendError), + #[error("sending signal message to backend from ui failed: {0}")] + SignalSend(#[from] tokio::sync::mpsc::error::SendError), #[error("sharing state between backend and frontend failed: {0}")] - Send(#[from] tokio::sync::broadcast::error::SendError), + StateSend(#[from] tokio::sync::broadcast::error::SendError), #[error("you can't disable the UI without MPRIS!")] RejectedDisable, @@ -64,11 +64,11 @@ pub struct State { pub bookmarked: bool, /// The timer, which is used when the user changes volume to briefly display it. - pub(crate) timer: Option, + pub(crate) volume_timer: Option, - /// The name of the playing tracklist, for MPRIS. + /// The name of the playing tracklist, mainly for MPRIS. #[allow(dead_code)] - list: String, + tracklist: String, } impl State { @@ -76,10 +76,10 @@ impl State { pub fn initial(sink: Arc, list: String) -> Self { Self { sink, - list, + tracklist: list, current: Current::default(), bookmarked: false, - timer: None, + volume_timer: None, } } } @@ -155,18 +155,18 @@ impl Handle { /// `rx` is the receiver for state updates, `state` the initial state, /// and `params` specifies aesthetic options that are specified by the user. async fn ui( - mut rx: broadcast::Receiver, + mut updater: broadcast::Receiver, mut state: State, params: interface::Params, ) -> Result<()> { let mut interface = Interface::new(params); loop { - if let Ok(message) = rx.try_recv() { + if let Ok(message) = updater.try_recv() { match message { Update::Track(track) => state.current = track, Update::Bookmarked(bookmarked) => state.bookmarked = bookmarked, - Update::Volume => state.timer = Some(Instant::now()), + Update::Volume => state.volume_timer = Some(Instant::now()), Update::Quit => break, } } diff --git a/src/ui/interface.rs b/src/ui/interface.rs index f9d828c..36da665 100644 --- a/src/ui/interface.rs +++ b/src/ui/interface.rs @@ -121,12 +121,12 @@ impl Interface { pub(crate) fn menu(&self, state: &mut State) -> Vec { let action = components::action(state, self.params.width); - let middle = match state.timer { + let middle = match state.volume_timer { Some(timer) => { let volume = state.sink.volume(); let percentage = format!("{}%", (volume * 100.0).round().abs()); if timer.elapsed() > Duration::from_secs(1) { - state.timer = None; + state.volume_timer = None; } components::audio_bar(self.params.width - 17, volume, &percentage) diff --git a/src/ui/interface/titlebar.rs b/src/ui/interface/titlebar.rs index 43f4169..c221386 100644 --- a/src/ui/interface/titlebar.rs +++ b/src/ui/interface/titlebar.rs @@ -1,7 +1,7 @@ use std::fmt::Display; use unicode_segmentation::UnicodeSegmentation; -/// The titlebar, which is essentially the entire top of the window. +/// The titlebar, which is the entire top row of the window. /// /// The struct offers a basic API for displaying messages to it. pub struct TitleBar { diff --git a/src/ui/mpris.rs b/src/ui/mpris.rs index 5228ec4..a127c97 100644 --- a/src/ui/mpris.rs +++ b/src/ui/mpris.rs @@ -331,7 +331,7 @@ impl Server { let suffix = if env::var("LOWFI_FIXED_MPRIS_NAME").is_ok_and(|x| x == "1") { String::from("lowfi") } else { - format!("lowfi.{}.instance{}", state.list, process::id()) + format!("lowfi.{}.instance{}", state.tracklist, process::id()) }; let server = mpris_server::Server::new( @@ -340,7 +340,7 @@ impl Server { sender: Sender::new(sender), sink: state.sink, current: ArcSwap::new(Arc::new(state.current)), - list: state.list, + list: state.tracklist, }, ) .await?; diff --git a/src/volume.rs b/src/volume.rs index dc7956a..4164b99 100644 --- a/src/volume.rs +++ b/src/volume.rs @@ -12,7 +12,7 @@ type Result = std::result::Result; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("couldn't find config directory")] - Directory, + DirectoryNotFound, #[error("io error: {0}")] Io(#[from] std::io::Error), @@ -33,17 +33,17 @@ pub struct PersistentVolume { } impl PersistentVolume { - /// Retrieves the config directory, creating it if necessary. - async fn config() -> Result { + /// Retrieves the config file path, creating it if necessary. + async fn path() -> Result { let config = dirs::config_dir() - .ok_or(Error::Directory)? + .ok_or(Error::DirectoryNotFound)? .join(PathBuf::from("lowfi")); if !config.exists() { fs::create_dir_all(&config).await?; } - Ok(config) + Ok(config.join(PathBuf::from("volume.txt"))) } /// Returns the volume as a normalized float in the range 0.0..=1.0. @@ -56,17 +56,16 @@ impl PersistentVolume { /// If the file does not exist a default of `100` is written and /// returned. pub async fn load() -> Result { - let config = Self::config().await?; - let volume = config.join(PathBuf::from("volume.txt")); + let path = Self::path().await?; // Basically just read from the volume file if it exists, otherwise return 100. - let volume = if volume.exists() { - let contents = fs::read_to_string(volume).await?; + let volume = if path.exists() { + let contents = fs::read_to_string(path).await?; let trimmed = contents.trim(); let stripped = trimmed.strip_suffix("%").unwrap_or(trimmed); stripped.parse()? } else { - fs::write(&volume, "100").await?; + fs::write(&path, "100").await?; 100u16 }; @@ -75,10 +74,8 @@ impl PersistentVolume { /// Saves `volume` (0.0..=1.0) to `volume.txt` as an integer percent. pub async fn save(volume: f32) -> Result<()> { - let config = Self::config().await?; - let path = config.join(PathBuf::from("volume.txt")); let percentage = (volume * 100.0).abs().round() as u16; - fs::write(path, percentage.to_string()).await?; + fs::write(Self::path().await?, percentage.to_string()).await?; Ok(()) }