various non-functional code cleanups

This commit is contained in:
Henrik Friedrichsen
2020-02-21 23:33:08 +01:00
parent 7d9a81fa0a
commit d2915316da
15 changed files with 41 additions and 49 deletions

View File

@@ -15,7 +15,7 @@ pub fn create_credentials(path: &Path) -> Result<RespotCredentials, String> {
"Cannot read config file from {}\n", "Cannot read config file from {}\n",
path.to_str().unwrap() path.to_str().unwrap()
)); ));
let info_view = Dialog::around(TextView::new_with_content(info_buf.clone())) let info_view = Dialog::around(TextView::new_with_content(info_buf))
.button("Login", move |s| { .button("Login", move |s| {
let login_view = Dialog::new() let login_view = Dialog::new()
.title("Spotify login") .title("Spotify login")

View File

@@ -116,7 +116,7 @@ impl fmt::Display for Command {
format!("repeat {}", param) format!("repeat {}", param)
} }
Command::Shuffle(on) => { Command::Shuffle(on) => {
let param = on.map(|x| if x == true { "on" } else { "off" }); let param = on.map(|x| if x { "on" } else { "off" });
format!("shuffle {}", param.unwrap_or("")) format!("shuffle {}", param.unwrap_or(""))
} }
Command::Share(mode) => format!("share {}", mode), Command::Share(mode) => format!("share {}", mode),
@@ -190,7 +190,9 @@ pub fn parse(input: &str) -> Option<Command> {
_ => None, _ => None,
}) })
.map(Command::Open), .map(Command::Open),
"search" => args.get(0).map(|query| Command::Search(query.to_string())), "search" => args
.get(0)
.map(|query| Command::Search((*query).to_string())),
"shift" => { "shift" => {
let amount = args.get(1).and_then(|amount| amount.parse().ok()); let amount = args.get(1).and_then(|amount| amount.parse().ok());
@@ -268,7 +270,9 @@ pub fn parse(input: &str) -> Option<Command> {
.ok() .ok()
.map(|amount| Command::Seek(SeekDirection::Absolute(amount))), .map(|amount| Command::Seek(SeekDirection::Absolute(amount))),
}), }),
"focus" => args.get(0).map(|target| Command::Focus(target.to_string())), "focus" => args
.get(0)
.map(|target| Command::Focus((*target).to_string())),
"save" => args.get(0).map(|target| match *target { "save" => args.get(0).map(|target| match *target {
"queue" => Command::SaveQueue, "queue" => Command::SaveQueue,
_ => Command::Save, _ => Command::Save,

View File

@@ -766,7 +766,7 @@ impl Library {
{ {
let mut store = self.playlists.write().unwrap(); let mut store = self.playlists.write().unwrap();
if !store.iter().any(|p| p.id == playlist.id) { if !store.iter().any(|p| p.id == playlist.id) {
store.insert(0, playlist.clone()); store.insert(0, playlist);
} }
} }

View File

@@ -215,11 +215,8 @@ fn main() {
let helpview = ui::help::HelpView::new(cmd_manager.keybindings().clone()); let helpview = ui::help::HelpView::new(cmd_manager.keybindings().clone());
let status = ui::statusbar::StatusBar::new( let status =
queue.clone(), ui::statusbar::StatusBar::new(queue.clone(), library, cfg.use_nerdfont.unwrap_or(false));
library.clone(),
cfg.use_nerdfont.unwrap_or(false),
);
let mut layout = ui::layout::Layout::new(status, &event_manager, theme) let mut layout = ui::layout::Layout::new(status, &event_manager, theme)
.view("search", search.with_name("search"), "Search") .view("search", search.with_name("search"), "Search")
@@ -246,7 +243,6 @@ fn main() {
{ {
let ev = event_manager.clone(); let ev = event_manager.clone();
let cmd_manager = cmd_manager.clone();
layout.cmdline.set_on_submit(move |s, cmd| { layout.cmdline.set_on_submit(move |s, cmd| {
{ {
let mut main = s.find_name::<ui::layout::Layout>("main").unwrap(); let mut main = s.find_name::<ui::layout::Layout>("main").unwrap();

View File

@@ -367,7 +367,6 @@ fn run_dbus_server(spotify: Arc<Spotify>, queue: Arc<Queue>, rx: mpsc::Receiver<
}; };
let method_previous = { let method_previous = {
let queue = queue.clone();
let spotify = spotify.clone(); let spotify = spotify.clone();
f.method("Previous", (), move |m| { f.method("Previous", (), move |m| {
if spotify.get_current_progress() < Duration::from_secs(5) { if spotify.get_current_progress() < Duration::from_secs(5) {
@@ -388,7 +387,6 @@ fn run_dbus_server(spotify: Arc<Spotify>, queue: Arc<Queue>, rx: mpsc::Receiver<
}; };
let method_rewind = { let method_rewind = {
let spotify = spotify.clone();
f.method("Rewind", (), move |m| { f.method("Rewind", (), move |m| {
spotify.seek_relative(-5000); spotify.seek_relative(-5000);
Ok(vec![m.msg.method_return()]) Ok(vec![m.msg.method_return()])

View File

@@ -229,7 +229,11 @@ impl Queue {
self.play(index, false, false); self.play(index, false, false);
} else if repeat == RepeatSetting::RepeatPlaylist && q.len() > 0 { } else if repeat == RepeatSetting::RepeatPlaylist && q.len() > 0 {
let random_order = self.random_order.read().unwrap(); let random_order = self.random_order.read().unwrap();
self.play(random_order.as_ref().map(|o| o[0]).unwrap_or(0), false, false); self.play(
random_order.as_ref().map(|o| o[0]).unwrap_or(0),
false,
false,
);
} else { } else {
self.spotify.stop(); self.spotify.stop();
} }

View File

@@ -222,7 +222,6 @@ impl Spotify {
let (tx, rx) = mpsc::unbounded(); let (tx, rx) = mpsc::unbounded();
{ {
let events = events.clone();
thread::spawn(move || { thread::spawn(move || {
Self::worker(events, rx, player_config, credentials, user_tx, volume) Self::worker(events, rx, player_config, credentials, user_tx, volume)
}); });
@@ -731,7 +730,7 @@ impl Spotify {
self.volume.load(Ordering::Relaxed) as u16 self.volume.load(Ordering::Relaxed) as u16
} }
fn to_log_scale(volume: u16) -> u16 { fn log_scale(volume: u16) -> u16 {
// https://www.dr-lex.be/info-stuff/volumecontrols.html#ideal2 // https://www.dr-lex.be/info-stuff/volumecontrols.html#ideal2
// a * exp(b * x) // a * exp(b * x)
const A: f64 = 1.0 / 1000.0; const A: f64 = 1.0 / 1000.0;
@@ -752,7 +751,7 @@ impl Spotify {
info!("setting volume to {}", volume); info!("setting volume to {}", volume);
self.volume.store(volume, Ordering::Relaxed); self.volume.store(volume, Ordering::Relaxed);
self.channel self.channel
.unbounded_send(WorkerCommand::SetVolume(Self::to_log_scale(volume))) .unbounded_send(WorkerCommand::SetVolume(Self::log_scale(volume)))
.unwrap(); .unwrap();
} }
} }

View File

@@ -50,11 +50,7 @@ impl AlbumView {
.tab( .tab(
"artists", "artists",
"Artists", "Artists",
ListView::new( ListView::new(Arc::new(RwLock::new(artists)), queue, library),
Arc::new(RwLock::new(artists)),
queue.clone(),
library.clone(),
),
); );
Self { album, tabs } Self { album, tabs }

View File

@@ -51,7 +51,6 @@ impl ArtistView {
let related: Arc<RwLock<Vec<Artist>>> = Arc::new(RwLock::new(Vec::new())); let related: Arc<RwLock<Vec<Artist>>> = Arc::new(RwLock::new(Vec::new()));
{ {
let related = related.clone(); let related = related.clone();
let spotify = spotify.clone();
let id = artist.id.clone(); let id = artist.id.clone();
let library = library.clone(); let library = library.clone();
thread::spawn(move || { thread::spawn(move || {
@@ -83,7 +82,7 @@ impl ArtistView {
tabs.add_tab( tabs.add_tab(
"top_tracks", "top_tracks",
"Top 10", "Top 10",
ListView::new(top_tracks.clone(), queue.clone(), library.clone()), ListView::new(top_tracks, queue.clone(), library.clone()),
); );
tabs.add_tab( tabs.add_tab(
@@ -99,7 +98,7 @@ impl ArtistView {
tabs.add_tab( tabs.add_tab(
"related", "related",
"Related Artists", "Related Artists",
ListView::new(related.clone(), queue.clone(), library.clone()), ListView::new(related, queue, library),
); );
Self { artist, tabs } Self { artist, tabs }

View File

@@ -242,7 +242,7 @@ impl View for Layout {
if position.y < self.last_size.y.saturating_sub(2 + cmdline_height) { if position.y < self.last_size.y.saturating_sub(2 + cmdline_height) {
if let Some(ref id) = self.focus { if let Some(ref id) = self.focus {
let screen = self.views.get_mut(id).unwrap(); let screen = self.views.get_mut(id).unwrap();
screen.view.on_event(event.clone()); screen.view.on_event(event);
} }
} else if position.y < self.last_size.y - cmdline_height { } else if position.y < self.last_size.y - cmdline_height {
self.statusbar.on_event( self.statusbar.on_event(

View File

@@ -37,7 +37,7 @@ impl LibraryView {
.tab( .tab(
"playlists", "playlists",
"Playlists", "Playlists",
PlaylistsView::new(queue.clone(), library.clone()), PlaylistsView::new(queue, library.clone()),
); );
Self { tabs } Self { tabs }

View File

@@ -387,14 +387,10 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
let queue = self.queue.clone(); let queue = self.queue.clone();
let library = self.library.clone(); let library = self.library.clone();
let target: Option<Box<dyn ListItem>> = match mode { let target: Option<Box<dyn ListItem>> = match mode {
TargetMode::Current => { TargetMode::Current => self.queue.get_current().map(|t| t.as_listitem()),
self.queue.get_current().and_then(|t| Some(t.as_listitem()))
}
TargetMode::Selected => { TargetMode::Selected => {
let content = self.content.read().unwrap(); let content = self.content.read().unwrap();
content content.get(self.selected).map(|t| t.as_listitem())
.get(self.selected)
.and_then(|t| Some(t.as_listitem()))
} }
}; };

View File

@@ -21,7 +21,7 @@ pub struct PlaylistsView {
impl PlaylistsView { impl PlaylistsView {
pub fn new(queue: Arc<Queue>, library: Arc<Library>) -> Self { pub fn new(queue: Arc<Queue>, library: Arc<Library>) -> Self {
Self { Self {
list: ListView::new(library.playlists.clone(), queue.clone(), library.clone()), list: ListView::new(library.playlists.clone(), queue, library.clone()),
library, library,
} }
} }

View File

@@ -73,8 +73,7 @@ impl SearchView {
let pagination_albums = list_albums.get_pagination().clone(); let pagination_albums = list_albums.get_pagination().clone();
let list_artists = ListView::new(results_artists.clone(), queue.clone(), library.clone()); let list_artists = ListView::new(results_artists.clone(), queue.clone(), library.clone());
let pagination_artists = list_artists.get_pagination().clone(); let pagination_artists = list_artists.get_pagination().clone();
let list_playlists = let list_playlists = ListView::new(results_playlists.clone(), queue, library);
ListView::new(results_playlists.clone(), queue.clone(), library.clone());
let pagination_playlists = list_playlists.get_pagination().clone(); let pagination_playlists = list_playlists.get_pagination().clone();
let tabs = TabView::new() let tabs = TabView::new()
@@ -272,19 +271,20 @@ impl SearchView {
let total_items = handler(&spotify, &results, &query, 0, false) as usize; let total_items = handler(&spotify, &results, &query, 0, false) as usize;
// register paginator if the API has more than one page of results // register paginator if the API has more than one page of results
if paginator.is_some() && total_items > results.read().unwrap().len() { if let Some(mut paginator) = paginator {
let mut paginator = paginator.unwrap(); if total_items > results.read().unwrap().len() {
let ev = ev.clone(); let ev = ev.clone();
// paginator callback // paginator callback
let cb = move |items: Arc<RwLock<Vec<I>>>| { let cb = move |items: Arc<RwLock<Vec<I>>>| {
let offset = items.read().unwrap().len(); let offset = items.read().unwrap().len();
handler(&spotify, &results, &query, offset, true); handler(&spotify, &results, &query, offset, true);
ev.trigger(); ev.trigger();
}; };
paginator.set(total_items, Box::new(cb)); paginator.set(total_items, Box::new(cb));
} else if let Some(mut p) = paginator { } else {
p.clear() paginator.clear()
}
} }
ev.trigger(); ev.trigger();
}); });

View File

@@ -125,7 +125,7 @@ impl View for StatusBar {
let volume = format!( let volume = format!(
" [{}%]", " [{}%]",
(self.spotify.volume() as f64 / 0xffff as f64 * 100.0) as u16 (self.spotify.volume() as f64 / 65535_f64 * 100.0) as u16
); );
printer.with_color(style_bar_bg, |printer| { printer.with_color(style_bar_bg, |printer| {