Skip to content

Commit

Permalink
refactor: rustfmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan committed Nov 30, 2024
1 parent f32bc0b commit 4c118cf
Show file tree
Hide file tree
Showing 51 changed files with 774 additions and 879 deletions.
2 changes: 2 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allow-private-module-inception = true
type-complexity-threshold = 700
146 changes: 81 additions & 65 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{
error::Error,
env,
error::Error,
path::PathBuf,
rc::Rc,
sync::{
mpsc::Receiver,
Arc,
Mutex,
MutexGuard,
atomic::{AtomicBool, AtomicU64, Ordering},
mpsc::Receiver,
Arc, Mutex,
},
thread,
thread::JoinHandle,
Expand All @@ -24,16 +23,15 @@ use ratatui::{
use rodio::OutputStream;

use crate::{
components::{FileBrowser, FileBrowserSelection, Help, Library, Playlists, Queue},
config::Theme,
structs::{Song, Action, Actions, OnAction},
player::Player,
state::State,
structs::{Action, Actions, OnAction, OnActionMut, ScreenAction, Song},
term::set_terminal,
ui::{KeyboardHandlerRef, KeyboardHandlerMut, TopBar, Component},
ui::{Component, KeyboardHandlerMut, KeyboardHandlerRef, TopBar},
Command,
components::{FileBrowser, FileBrowserSelection, Library, Playlists, Queue, Help},
};
use crate::structs::{OnActionMut, ScreenAction};

pub struct App<'a> {
must_quit: bool,
Expand Down Expand Up @@ -76,7 +74,7 @@ impl<'a> App<'a> {

let focus_trap = Arc::new(AtomicBool::new(false));

let library = Arc::new(Library::new(theme));
let library = Rc::new(Library::new(theme));
library.on_enter({
let queue = queue.clone();

Expand All @@ -91,21 +89,23 @@ impl<'a> App<'a> {
player.play_song(song);
}
});
library.on_select_songs_fn({ // selected artist/album
library.on_select_songs_fn({
// selected artist/album
let queue = queue.clone();
let library = library.clone();

move |songs| {
log::trace!(target: "::app.library", "on_select_songs_fn -> adding songs to queue");
queue.append(&mut std::collections::VecDeque::from(songs));
library.on_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)); // hackish way to "select_next()"
// hackish way to "select_next()":
library.on_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
}
});

let playlist = Arc::new(Playlists::new(theme));
let playlist = Rc::new(Playlists::new(theme));
playlist.on_enter_song({
let queue = queue.clone();
move |song | {
move |song| {
queue.add_back(song);
}
});
Expand Down Expand Up @@ -133,14 +133,22 @@ impl<'a> App<'a> {
let player = player.clone();
let queue = queue.clone();
let playlists = playlist.clone();
let media_library = Arc::clone(&library);
let media_library = Rc::clone(&library);

move |(s, key_event)| {
Self::on_file_browser_select(player.as_ref(), queue.as_ref(), playlists.as_ref(), media_library.as_ref(), s, key_event);
Self::on_file_browser_select(
player.as_ref(),
queue.as_ref(),
playlists.as_ref(),
media_library.as_ref(),
s,
key_event,
);
}
});

let browser = Arc::new(Mutex::new(browser));
#[allow(clippy::arc_with_non_send_sync)]
let browser = Arc::new(Mutex::new(browser)); // arc_with_non_send_sync gives false positive for browser, but browser will be refactored to use List component anyway
let help = Arc::new(Mutex::new(Help::new(theme)));

Self {
Expand All @@ -153,8 +161,8 @@ impl<'a> App<'a> {
_music_output: output_stream,

screens: vec![
("Library".to_string(), Component::RefArc(library.clone())),
("Playlists".to_string(), Component::RefArc(playlist.clone())),
("Library".to_string(), Component::RefRc(library.clone())),
("Playlists".to_string(), Component::RefRc(playlist.clone())),
("Queue".to_string(), Component::RefArc(queue.clone())),
("File Browser".to_string(), Component::Mut(browser.clone())),
("Help".to_string(), Component::Mut(help.clone())),
Expand All @@ -171,15 +179,17 @@ impl<'a> App<'a> {
}
}

fn file_browser(&self) -> MutexGuard<FileBrowser<'a>> {
self.browser.lock().unwrap()
}

fn to_state(&self) -> State {
let queue_items = self.queue.songs().clone();

State {
last_visited_path: self.file_browser().current_directory().to_str().map(String::from),
last_visited_path: self
.browser
.lock()
.unwrap()
.current_directory()
.to_str()
.map(String::from),
queue_items: Vec::from(queue_items),
}
}
Expand Down Expand Up @@ -226,27 +236,30 @@ impl<'a> App<'a> {
let player_command_receiver = self.player_command_receiver.clone();
let player = self.player.clone();

let t = thread::Builder::new().name("media_key_rx".to_string()).spawn(move || {
loop {
match player_command_receiver.lock().unwrap().recv() {
Ok(Command::PlayPause) => {
player.toggle();
}
Ok(Command::Next) => {
player.stop();
}
Ok(Command::Quit) => {
log::debug!("Received Command::Quit");
break;
}
Err(err) => {
log::error!("Channel error: {}", err);
break;
let t = thread::Builder::new()
.name("media_key_rx".to_string())
.spawn(move || {
loop {
match player_command_receiver.lock().unwrap().recv() {
Ok(Command::PlayPause) => {
player.toggle();
}
Ok(Command::Next) => {
player.stop();
}
Ok(Command::Quit) => {
log::debug!("Received Command::Quit");
break;
}
Err(err) => {
log::error!("Channel error: {}", err);
break;
}
}
}
}
log::trace!("spawn_media_key_receiver_thread loop exit");
}).unwrap();
log::trace!("spawn_media_key_receiver_thread loop exit");
})
.unwrap();

self.player_command_receiver_thread = Some(t);
}
Expand Down Expand Up @@ -321,11 +334,11 @@ impl<'a> KeyboardHandlerMut<'a> for App<'a> {
}
if !self.focus_trap.load(Ordering::Acquire) {
match action {
Action::ScreenAction(_) => {
Action::Screen(_) => {
self.on_action(action);
return;
}
Action::PlayerAction(_) => {
Action::Player(_) => {
self.player.on_action(action);
return;
}
Expand All @@ -349,13 +362,13 @@ impl<'a> WidgetRef for &App<'a> {
.style(Style::default().bg(self.theme.background))
.render(area, buf);

let [area_top, _, area_center, area_bottom] =
Layout::vertical([
Constraint::Length(1),
Constraint::Length(1),
Constraint::Min(0),
Constraint::Length(3),
]).areas(area);
let [area_top, _, area_center, area_bottom] = Layout::vertical([
Constraint::Length(1),
Constraint::Length(1),
Constraint::Min(0),
Constraint::Length(3),
])
.areas(area);

let screen_titles: Vec<&str> = self.screens.iter().map(|screen| screen.0.as_str()).collect();

Expand All @@ -373,9 +386,15 @@ impl<'a> WidgetRef for &App<'a> {
};

match component {
Component::RefArc(ref s) => { s.render_ref(area_center, buf); }
Component::Mut(ref s) => { s.lock().unwrap().render_ref(area_center, buf); }
_ => {}
Component::RefArc(ref s) => {
s.render_ref(area_center, buf);
}
Component::RefRc(ref s) => {
s.render_ref(area_center, buf);
}
Component::Mut(ref s) => {
s.lock().unwrap().render_ref(area_center, buf);
}
}

self.player.render(area_bottom, buf);
Expand All @@ -384,17 +403,14 @@ impl<'a> WidgetRef for &App<'a> {

impl<'a> OnActionMut for App<'a> {
fn on_action(&mut self, action: Action) {
match action {
Action::ScreenAction(action) => {
match action {
ScreenAction::Library => { self.focused_screen = 0 }
ScreenAction::Playlists => { self.focused_screen = 1 }
ScreenAction::Queue => { self.focused_screen = 2 }
ScreenAction::FileBrowser => { self.focused_screen = 3 }
ScreenAction::Help => { self.focused_screen = 4 }
}
if let Action::Screen(action) = action {
match action {
ScreenAction::Library => self.focused_screen = 0,
ScreenAction::Playlists => self.focused_screen = 1,
ScreenAction::Queue => self.focused_screen = 2,
ScreenAction::FileBrowser => self.focused_screen = 3,
ScreenAction::Help => self.focused_screen = 4,
}
_ => {}
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/auto_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub enum ReleasesError {
impl std::fmt::Debug for ReleasesError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReleasesError::Reqwest(e) => { e.fmt(f) }
ReleasesError::TomlFileError(e) => { e.fmt(f) }
ReleasesError::Reqwest(e) => e.fmt(f),
ReleasesError::TomlFileError(e) => e.fmt(f),
}
}
}
Expand Down Expand Up @@ -73,9 +73,7 @@ pub fn get_releases() -> Result<(), ReleasesError> {

log::debug!(target: "::get_releases", "Got releases = {releases:#?}");

let releases = Releases {
releases,
};
let releases = Releases { releases };

write_toml_file("releases", &releases)?;

Expand All @@ -91,8 +89,7 @@ pub fn can_i_has_rls() -> Result<(), ReleasesError> {

log::trace!(target: target, "we has rls file = {releases:#?}");

log::trace!(target: target, "we has rls published_at = {:?}", releases.releases.get(0).map(|r| r.published_at.as_str()));

log::trace!(target: target, "we has rls published_at = {:?}", releases.releases.first().map(|r| r.published_at.as_str()));

Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions src/bye.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::{UNIX_EPOCH, Duration, SystemTime};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

const BYE_N: usize = 9;
const BYE: [&str; BYE_N] = [
Expand All @@ -15,7 +15,9 @@ const BYE: [&str; BYE_N] = [

/// The important things in life
pub fn bye() -> &'static str {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or(Duration::from_secs(0));
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::from_secs(0));
let now = now.as_micros() as usize;
let i = now % BYE_N;
BYE[i]
Expand Down
10 changes: 5 additions & 5 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
mod file_browser;
mod library;
mod queue;
mod help;
mod playlists;
mod library;
mod list;
mod playlists;
mod queue;
mod rendering_error;

pub use file_browser::{directory_to_songs_and_folders, FileBrowser, FileBrowserSelection};
pub use help::Help;
pub use library::Library;
pub use list::List;
pub use playlists::Playlists;
pub use queue::Queue;
pub use help::Help;
pub use list::List;
6 changes: 3 additions & 3 deletions src/components/file_browser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod file_browser;
pub mod widget;
pub mod keyboard_handler;
mod file_browser_selection;
pub mod keyboard_handler;
pub mod widget;

pub use file_browser::*;
pub use file_browser_selection::{FileBrowserSelection, directory_to_songs_and_folders};
pub use file_browser_selection::{directory_to_songs_and_folders, FileBrowserSelection};
Loading

0 comments on commit 4c118cf

Please sign in to comment.