code formatting + minor refactorings

This commit is contained in:
Henrik Friedrichsen
2019-03-28 05:42:47 +01:00
parent 486bc7617e
commit d7848ec333
8 changed files with 85 additions and 60 deletions

View File

@@ -217,7 +217,12 @@ impl CommandManager {
}
}
fn handle_callbacks(&self, s: &mut Cursive, cmd: &String, args: &[String]) -> Result<Option<String>, String> {
fn handle_callbacks(
&self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<Option<String>, String> {
let local = {
let mut main: ViewRef<Layout> = s.find_id("main").unwrap();
main.on_command(s, cmd, args)?
@@ -226,9 +231,7 @@ impl CommandManager {
if let CommandResult::Consumed(output) = local {
Ok(output)
} else if let Some(callback) = self.callbacks.get(cmd) {
callback.as_ref()
.map(|cb| cb(s, args))
.unwrap_or(Ok(None))
callback.as_ref().map(|cb| cb(s, args)).unwrap_or(Ok(None))
} else {
Err("Unknown command.".to_string())
}

View File

@@ -154,7 +154,11 @@ fn main() {
cmd_manager.register_all(spotify.clone(), queue.clone(), playlists.clone());
let cmd_manager = Arc::new(cmd_manager);
CommandManager::register_keybindings(cmd_manager.clone(), &mut cursive, cfg.keybindings.clone());
CommandManager::register_keybindings(
cmd_manager.clone(),
&mut cursive,
cfg.keybindings.clone(),
);
let search = ui::search::SearchView::new(spotify.clone(), queue.clone());

View File

@@ -16,24 +16,25 @@ pub trait ListItem {
}
pub trait ViewExt: View {
fn on_command(&mut self,
fn on_command(
&mut self,
_s: &mut Cursive,
_cmd: &String,
_args: &[String]
_cmd: &str,
_args: &[String],
) -> Result<CommandResult, String> {
Ok(CommandResult::Ignored)
}
}
impl<V: ViewExt> ViewExt for IdView<V> {
fn on_command(&mut self,
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &String,
args: &[String]
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
self.with_view_mut(move |v| {
v.on_command(s, cmd, args)
}).unwrap()
self.with_view_mut(move |v| v.on_command(s, cmd, args))
.unwrap()
}
}

View File

@@ -12,9 +12,9 @@ use cursive::views::EditView;
use cursive::{Cursive, Printer};
use unicode_width::UnicodeWidthStr;
use events;
use traits::{ViewExt, IntoBoxedViewExt};
use commands::CommandResult;
use events;
use traits::{IntoBoxedViewExt, ViewExt};
struct Screen {
title: String,
@@ -77,7 +77,12 @@ impl Layout {
self.focus = Some(s);
}
pub fn view<S: Into<String>, T: IntoBoxedViewExt>(mut self, id: S, view: T, title: &str) -> Self {
pub fn view<S: Into<String>, T: IntoBoxedViewExt>(
mut self,
id: S,
view: T,
title: &str,
) -> Self {
(&mut self).add_view(id, view, title);
self
}
@@ -156,7 +161,10 @@ impl View for Layout {
printer.with_color(style, |printer| {
printer.print_hline((0, printer.size.y - cmdline_height), printer.size.x, " ");
printer.print((0, printer.size.y - cmdline_height), &format!("ERROR: {}", e));
printer.print(
(0, printer.size.y - cmdline_height),
&format!("ERROR: {}", e),
);
});
}
@@ -248,10 +256,11 @@ impl View for Layout {
}
impl ViewExt for Layout {
fn on_command(&mut self,
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &String,
args: &[String]
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "focus" {
if let Some(view) = args.get(0) {

View File

@@ -9,9 +9,9 @@ use cursive::view::ScrollBase;
use cursive::{Cursive, Printer, Rect, Vec2};
use unicode_width::UnicodeWidthStr;
use commands::CommandResult;
use queue::Queue;
use traits::{ListItem, ViewExt};
use commands::CommandResult;
pub struct ListView<I: 'static + ListItem> {
content: Arc<RwLock<Vec<I>>>,
@@ -174,10 +174,11 @@ impl<I: ListItem> View for ListView<I> {
}
impl<I: ListItem> ViewExt for ListView<I> {
fn on_command(&mut self,
fn on_command(
&mut self,
_s: &mut Cursive,
cmd: &String,
args: &[String]
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "play" {
let content = self.content.read().unwrap();
@@ -195,27 +196,25 @@ impl<I: ListItem> ViewExt for ListView<I> {
return Ok(CommandResult::Consumed(None));
}
if cmd != "move" {
return Ok(CommandResult::Ignored);
}
if cmd == "move" {
if let Some(dir) = args.get(0) {
let amount: i32 = args
.get(1)
.unwrap_or(&"1".to_string())
.parse()
.map_err(|e| format!("{:?}", e))?;
if let Some(dir) = args.get(0) {
let amount: i32 = args
.get(1)
.unwrap_or(&"1".to_string())
.parse()
.map_err(|e| format!("{:?}", e))?;
let len = self.content.read().unwrap().len();
let len = self.content.read().unwrap().len();
if dir == "up" && self.selected > 0 {
self.move_focus(-amount);
return Ok(CommandResult::Consumed(None));
}
if dir == "up" && self.selected > 0 {
self.move_focus(amount * -1);
return Ok(CommandResult::Consumed(None));
}
if dir == "down" && self.selected < len - 1 {
self.move_focus(amount);
return Ok(CommandResult::Consumed(None));
if dir == "down" && self.selected < len - 1 {
self.move_focus(amount);
return Ok(CommandResult::Consumed(None));
}
}
}

View File

@@ -56,10 +56,11 @@ impl ViewWrapper for PlaylistView {
}
impl ViewExt for PlaylistView {
fn on_command(&mut self,
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &String,
args: &[String]
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "delete" {
if let Some(dialog) = self.delete_dialog() {
@@ -69,6 +70,5 @@ impl ViewExt for PlaylistView {
}
self.list.on_command(s, cmd, args)
}
}

View File

@@ -24,10 +24,19 @@ impl QueueView {
pub fn new(queue: Arc<Queue>, playlists: Arc<Playlists>) -> QueueView {
let list = ListView::new(queue.queue.clone(), queue.clone());
QueueView { list, playlists, queue }
QueueView {
list,
playlists,
queue,
}
}
fn save_dialog_cb(s: &mut Cursive, queue: Arc<Queue>, playlists: Arc<Playlists>, id: Option<String>) {
fn save_dialog_cb(
s: &mut Cursive,
queue: Arc<Queue>,
playlists: Arc<Playlists>,
id: Option<String>,
) {
let tracks = queue.queue.read().unwrap().clone();
match id {
Some(id) => {
@@ -95,10 +104,11 @@ impl ViewWrapper for QueueView {
}
impl ViewExt for QueueView {
fn on_command(&mut self,
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &String,
args: &[String]
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "play" {
self.queue.play(self.list.get_selected_index(), true);
@@ -114,8 +124,7 @@ impl ViewExt for QueueView {
return Ok(CommandResult::Consumed(None));
}
self.with_view_mut(move |v| {
v.on_command(s, cmd, args)
}).unwrap()
self.with_view_mut(move |v| v.on_command(s, cmd, args))
.unwrap()
}
}

View File

@@ -47,7 +47,7 @@ impl SearchView {
edit: searchfield,
list,
edit_focused: true,
spotify
spotify,
}
}
@@ -118,12 +118,12 @@ impl View for SearchView {
}
}
impl ViewExt for SearchView {
fn on_command(&mut self,
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &String,
args: &[String]
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "search" && !args.is_empty() {
self.run_search(args.join(" "));