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",
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| {
let login_view = Dialog::new()
.title("Spotify login")

View File

@@ -116,7 +116,7 @@ impl fmt::Display for Command {
format!("repeat {}", param)
}
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(""))
}
Command::Share(mode) => format!("share {}", mode),
@@ -190,7 +190,9 @@ pub fn parse(input: &str) -> Option<Command> {
_ => None,
})
.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" => {
let amount = args.get(1).and_then(|amount| amount.parse().ok());
@@ -268,7 +270,9 @@ pub fn parse(input: &str) -> Option<Command> {
.ok()
.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 {
"queue" => Command::SaveQueue,
_ => Command::Save,

View File

@@ -766,7 +766,7 @@ impl Library {
{
let mut store = self.playlists.write().unwrap();
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 status = ui::statusbar::StatusBar::new(
queue.clone(),
library.clone(),
cfg.use_nerdfont.unwrap_or(false),
);
let status =
ui::statusbar::StatusBar::new(queue.clone(), library, cfg.use_nerdfont.unwrap_or(false));
let mut layout = ui::layout::Layout::new(status, &event_manager, theme)
.view("search", search.with_name("search"), "Search")
@@ -246,7 +243,6 @@ fn main() {
{
let ev = event_manager.clone();
let cmd_manager = cmd_manager.clone();
layout.cmdline.set_on_submit(move |s, cmd| {
{
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 queue = queue.clone();
let spotify = spotify.clone();
f.method("Previous", (), move |m| {
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 spotify = spotify.clone();
f.method("Rewind", (), move |m| {
spotify.seek_relative(-5000);
Ok(vec![m.msg.method_return()])

View File

@@ -229,7 +229,11 @@ impl Queue {
self.play(index, false, false);
} else if repeat == RepeatSetting::RepeatPlaylist && q.len() > 0 {
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 {
self.spotify.stop();
}

View File

@@ -222,7 +222,6 @@ impl Spotify {
let (tx, rx) = mpsc::unbounded();
{
let events = events.clone();
thread::spawn(move || {
Self::worker(events, rx, player_config, credentials, user_tx, volume)
});
@@ -731,7 +730,7 @@ impl Spotify {
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
// a * exp(b * x)
const A: f64 = 1.0 / 1000.0;
@@ -752,7 +751,7 @@ impl Spotify {
info!("setting volume to {}", volume);
self.volume.store(volume, Ordering::Relaxed);
self.channel
.unbounded_send(WorkerCommand::SetVolume(Self::to_log_scale(volume)))
.unbounded_send(WorkerCommand::SetVolume(Self::log_scale(volume)))
.unwrap();
}
}

View File

@@ -50,11 +50,7 @@ impl AlbumView {
.tab(
"artists",
"Artists",
ListView::new(
Arc::new(RwLock::new(artists)),
queue.clone(),
library.clone(),
),
ListView::new(Arc::new(RwLock::new(artists)), queue, library),
);
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 = related.clone();
let spotify = spotify.clone();
let id = artist.id.clone();
let library = library.clone();
thread::spawn(move || {
@@ -83,7 +82,7 @@ impl ArtistView {
tabs.add_tab(
"top_tracks",
"Top 10",
ListView::new(top_tracks.clone(), queue.clone(), library.clone()),
ListView::new(top_tracks, queue.clone(), library.clone()),
);
tabs.add_tab(
@@ -99,7 +98,7 @@ impl ArtistView {
tabs.add_tab(
"related",
"Related Artists",
ListView::new(related.clone(), queue.clone(), library.clone()),
ListView::new(related, queue, library),
);
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 let Some(ref id) = self.focus {
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 {
self.statusbar.on_event(

View File

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

View File

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

View File

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

View File

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

View File

@@ -125,7 +125,7 @@ impl View for StatusBar {
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| {