Skip to content

Commit

Permalink
refactor: rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan committed Nov 29, 2024
1 parent f32bc0b commit 1d747a6
Show file tree
Hide file tree
Showing 45 changed files with 613 additions and 676 deletions.
113 changes: 62 additions & 51 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{
error::Error,
env,
error::Error,
path::PathBuf,
sync::{
mpsc::Receiver,
Arc,
Mutex,
MutexGuard,
atomic::{AtomicBool, AtomicU64, Ordering},
mpsc::Receiver,
Arc, Mutex, MutexGuard,
},
thread,
thread::JoinHandle,
Expand All @@ -24,16 +22,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 @@ -91,21 +88,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));
playlist.on_enter_song({
let queue = queue.clone();
move |song | {
move |song| {
queue.add_back(song);
}
});
Expand Down Expand Up @@ -136,7 +135,14 @@ impl<'a> App<'a> {
let media_library = Arc::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,
);
}
});

Expand Down Expand Up @@ -171,7 +177,7 @@ impl<'a> App<'a> {
}
}

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

Expand Down Expand Up @@ -226,27 +232,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 @@ -349,13 +358,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,8 +382,12 @@ 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::Mut(ref s) => {
s.lock().unwrap().render_ref(area_center, buf);
}
_ => {}
}

Expand All @@ -385,15 +398,13 @@ 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 }
}
}
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,
},
_ => {}
}
}
Expand Down
9 changes: 3 additions & 6 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 @@ -93,7 +91,6 @@ pub fn can_i_has_rls() -> Result<(), ReleasesError> {

log::trace!(target: target, "we has rls published_at = {:?}", releases.releases.get(0).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};
27 changes: 13 additions & 14 deletions src/components/file_browser/file_browser.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use std::{
path::PathBuf,
sync::Mutex,
};
use std::{path::PathBuf, sync::Mutex};

use crossterm::event::{KeyCode, KeyEvent};

use crate::config::Theme;

use super::file_browser_selection::{
FileBrowserSelection,
directory_to_songs_and_folders,
};
use super::file_browser_selection::{directory_to_songs_and_folders, FileBrowserSelection};

pub struct FileBrowser<'a> {
on_select_fn: Box<dyn FnMut((FileBrowserSelection, KeyEvent)) + 'a>,
Expand Down Expand Up @@ -100,7 +94,9 @@ impl<'a> FileBrowser<'a> {
}

pub fn navigate_up(&mut self) {
let Some(parent) = self.current_directory.as_path().parent().map(|p| p.to_path_buf()) else { return };
let Some(parent) = self.current_directory.as_path().parent().map(|p| p.to_path_buf()) else {
return;
};
self.items = directory_to_songs_and_folders(&parent);
self.select_current_directory();
self.current_directory = parent;
Expand Down Expand Up @@ -183,7 +179,12 @@ impl<'a> FileBrowser<'a> {
return None;
}

if self.items[i].to_path().to_string_lossy().to_lowercase().contains(&s.to_lowercase()) {
if self.items[i]
.to_path()
.to_string_lossy()
.to_lowercase()
.contains(&s.to_lowercase())
{
return Some(i);
}
}
Expand Down Expand Up @@ -236,8 +237,6 @@ impl<'a> FileBrowser<'a> {
};
}



fn padding_top(&self) -> usize {
6
}
Expand All @@ -248,12 +247,12 @@ impl<'a> FileBrowser<'a> {

pub fn set_offset(&mut self, i: usize, padding: usize) {
self.offset = if i > padding {
i.saturating_sub(padding).min(self.items.len().saturating_sub(*self.height.lock().unwrap()))
i.saturating_sub(padding)
.min(self.items.len().saturating_sub(*self.height.lock().unwrap()))
} else {
0
};
}

}

impl Drop for FileBrowser<'_> {
Expand Down
Loading

0 comments on commit 1d747a6

Please sign in to comment.