Move to enum-based commands

This commit is contained in:
Rasmus Larsen
2019-05-20 22:15:12 +02:00
parent 4b4a027c3c
commit 1671db14c1
15 changed files with 343 additions and 317 deletions

View File

@@ -5,6 +5,7 @@ use cursive::Cursive;
use album::Album;
use artist::Artist;
use command::Command;
use commands::CommandResult;
use library::Library;
use queue::Queue;
@@ -69,12 +70,7 @@ impl ViewExt for AlbumView {
format!("{} ({})", self.album.title, self.album.year)
}
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
self.tabs.on_command(s, cmd, args)
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
self.tabs.on_command(s, cmd)
}
}

View File

@@ -5,6 +5,7 @@ use cursive::view::ViewWrapper;
use cursive::Cursive;
use artist::Artist;
use command::Command;
use commands::CommandResult;
use library::Library;
use queue::Queue;
@@ -110,12 +111,7 @@ impl ViewExt for ArtistView {
self.artist.name.clone()
}
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
self.tabs.on_command(s, cmd, args)
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
self.tabs.on_command(s, cmd)
}
}

View File

@@ -12,6 +12,8 @@ use cursive::views::EditView;
use cursive::{Cursive, Printer};
use unicode_width::UnicodeWidthStr;
use command::Command;
use command::Command::Focus;
use commands::CommandResult;
use events;
use traits::{IntoBoxedViewExt, ViewExt};
@@ -283,29 +285,28 @@ impl View for Layout {
}
impl ViewExt for Layout {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "focus" {
if let Some(view) = args.get(0) {
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Focus(view) => {
if self.views.keys().any(|k| k == view) {
self.set_view(view.clone());
let screen = self.views.get_mut(view).unwrap();
screen.view.on_command(s, cmd, args)?;
screen.view.on_command(s, cmd)?;
}
Ok(CommandResult::Consumed(None))
}
Command::Back => {
self.pop_view();
Ok(CommandResult::Consumed(None))
}
_ => {
if let Some(screen) = self.get_current_screen_mut() {
screen.view.on_command(s, cmd)
} else {
Ok(CommandResult::Ignored)
}
}
Ok(CommandResult::Consumed(None))
} else if cmd == "back" {
self.pop_view();
Ok(CommandResult::Consumed(None))
} else if let Some(screen) = self.get_current_screen_mut() {
screen.view.on_command(s, cmd, args)
} else {
Ok(CommandResult::Ignored)
}
}
}

View File

@@ -3,6 +3,7 @@ use std::sync::Arc;
use cursive::view::ViewWrapper;
use cursive::Cursive;
use command::Command;
use commands::CommandResult;
use library::Library;
use queue::Queue;
@@ -48,12 +49,7 @@ impl ViewWrapper for LibraryView {
}
impl ViewExt for LibraryView {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
self.tabs.on_command(s, cmd, args)
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
self.tabs.on_command(s, cmd)
}
}

View File

@@ -10,6 +10,7 @@ use cursive::{Cursive, Printer, Rect, Vec2};
use unicode_width::UnicodeWidthStr;
use clipboard::{ClipboardContext, ClipboardProvider};
use command::{Command, GotoMode, MoveMode, TargetMode};
use commands::CommandResult;
use library::Library;
use queue::Queue;
@@ -277,123 +278,116 @@ impl<I: ListItem> View for ListView<I> {
}
impl<I: ListItem + Clone> ViewExt for ListView<I> {
fn on_command(
&mut self,
_s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "play" {
self.queue.clear();
fn on_command(&mut self, _s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Play => {
self.queue.clear();
if !self.attempt_play_all_tracks() {
if !self.attempt_play_all_tracks() {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
item.play(self.queue.clone());
}
}
return Ok(CommandResult::Consumed(None));
}
Command::Queue => {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
item.play(self.queue.clone());
item.queue(self.queue.clone());
}
return Ok(CommandResult::Consumed(None));
}
Command::Save => {
let mut item = {
let content = self.content.read().unwrap();
content.get(self.selected).cloned()
};
if let Some(item) = item.as_mut() {
item.toggle_saved(self.library.clone());
}
}
return Ok(CommandResult::Consumed(None));
}
if cmd == "queue" {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
item.queue(self.queue.clone());
}
return Ok(CommandResult::Consumed(None));
}
if cmd == "save" {
let mut item = {
let content = self.content.read().unwrap();
content.get(self.selected).cloned()
};
if let Some(item) = item.as_mut() {
item.toggle_saved(self.library.clone());
}
}
if cmd == "share" {
let source = args.get(0);
let url =
source.and_then(|source| match source.as_str() {
"selected" => self.content.read().ok().and_then(|content| {
Command::Share(mode) => {
let url = match mode {
TargetMode::Selected => self.content.read().ok().and_then(|content| {
content.get(self.selected).and_then(ListItem::share_url)
}),
"current" => self.queue.get_current().and_then(|t| t.share_url()),
_ => None,
});
TargetMode::Current => self.queue.get_current().and_then(|t| t.share_url()),
};
if let Some(url) = url {
ClipboardProvider::new()
.and_then(|mut ctx: ClipboardContext| ctx.set_contents(url))
.ok();
};
if let Some(url) = url {
ClipboardProvider::new()
.and_then(|mut ctx: ClipboardContext| ctx.set_contents(url))
.ok();
}
return Ok(CommandResult::Consumed(None));
}
if cmd == "move" {
if let Some(dir) = args.get(0) {
let amount: usize = args
.get(1)
.unwrap_or(&"1".to_string())
.parse()
.map_err(|e| format!("{:?}", e))?;
return Ok(CommandResult::Consumed(None));
}
Command::Move(mode, amount) => {
let amount = match amount {
Some(amount) => *amount,
_ => 1,
};
let len = self.content.read().unwrap().len();
if dir == "up" && self.selected > 0 {
self.move_focus(-(amount as i32));
return Ok(CommandResult::Consumed(None));
}
if dir == "down" {
if self.selected < len.saturating_sub(1) {
match mode {
MoveMode::Up if self.selected > 0 => {
self.move_focus(-(amount as i32));
return Ok(CommandResult::Consumed(None));
}
MoveMode::Down if self.selected < len.saturating_sub(1) => {
self.move_focus(amount as i32);
return Ok(CommandResult::Consumed(None));
} else if self.selected == len.saturating_sub(1) && self.can_paginate() {
}
MoveMode::Down
if self.selected == len.saturating_sub(1) && self.can_paginate() =>
{
self.pagination.call(&self.content);
}
_ => {}
}
}
}
if cmd == "open" {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
let queue = self.queue.clone();
let library = self.library.clone();
if let Some(view) = item.open(queue, library) {
return Ok(CommandResult::View(view));
}
}
}
if cmd == "goto" {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
let queue = self.queue.clone();
let library = self.library.clone();
let arg = args.get(0).cloned().unwrap_or_default();
if arg == "album" {
if let Some(album) = item.album(queue.clone()) {
let view = AlbumView::new(queue, library, &album).as_boxed_view_ext();
return Ok(CommandResult::View(view));
}
}
if arg == "artist" {
if let Some(artist) = item.artist() {
let view = ArtistView::new(queue, library, &artist).as_boxed_view_ext();
Command::Open => {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
let queue = self.queue.clone();
let library = self.library.clone();
if let Some(view) = item.open(queue, library) {
return Ok(CommandResult::View(view));
}
}
}
}
Command::Goto(mode) => {
let mut content = self.content.write().unwrap();
if let Some(item) = content.get_mut(self.selected) {
let queue = self.queue.clone();
let library = self.library.clone();
match mode {
GotoMode::Album => {
if let Some(album) = item.album(queue.clone()) {
let view =
AlbumView::new(queue, library, &album).as_boxed_view_ext();
return Ok(CommandResult::View(view));
}
}
GotoMode::Artist => {
if let Some(artist) = item.artist() {
let view =
ArtistView::new(queue, library, &artist).as_boxed_view_ext();
return Ok(CommandResult::View(view));
}
}
}
}
}
_ => {}
};
Ok(CommandResult::Ignored)
}

View File

@@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
use cursive::view::ViewWrapper;
use cursive::Cursive;
use command::Command;
use commands::CommandResult;
use library::Library;
use playlist::Playlist;
@@ -43,12 +44,7 @@ impl ViewExt for PlaylistView {
self.playlist.name.clone()
}
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
self.list.on_command(s, cmd, args)
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
self.list.on_command(s, cmd)
}
}

View File

@@ -4,6 +4,7 @@ use cursive::view::ViewWrapper;
use cursive::views::Dialog;
use cursive::Cursive;
use command::Command;
use commands::CommandResult;
use library::Library;
use playlist::Playlist;
@@ -52,19 +53,17 @@ impl ViewWrapper for PlaylistsView {
}
impl ViewExt for PlaylistsView {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "delete" {
if let Some(dialog) = self.delete_dialog() {
s.add_layer(dialog);
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Delete => {
if let Some(dialog) = self.delete_dialog() {
s.add_layer(dialog);
}
return Ok(CommandResult::Consumed(None));
}
return Ok(CommandResult::Consumed(None));
_ => {}
}
self.list.on_command(s, cmd, args)
self.list.on_command(s, cmd)
}
}

View File

@@ -6,6 +6,7 @@ use cursive::Cursive;
use std::cmp::min;
use std::sync::Arc;
use command::{Command, ShiftMode};
use commands::CommandResult;
use library::Library;
use queue::Queue;
@@ -88,55 +89,51 @@ impl ViewWrapper for QueueView {
}
impl ViewExt for QueueView {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "play" {
self.queue.play(self.list.get_selected_index(), true);
return Ok(CommandResult::Consumed(None));
}
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Play => {
self.queue.play(self.list.get_selected_index(), true);
return Ok(CommandResult::Consumed(None));
}
Command::Queue => {
return Ok(CommandResult::Ignored);
}
Command::Delete => {
self.queue.remove(self.list.get_selected_index());
return Ok(CommandResult::Consumed(None));
}
Command::Shift(mode, amount) => {
let amount = match amount {
Some(amount) => *amount,
_ => 1,
};
if cmd == "queue" {
return Ok(CommandResult::Ignored);
}
if cmd == "delete" {
self.queue.remove(self.list.get_selected_index());
return Ok(CommandResult::Consumed(None));
}
if cmd == "shift" {
if let Some(dir) = args.get(0) {
let amount: usize = args
.get(1)
.unwrap_or(&"1".to_string())
.parse()
.map_err(|e| format!("{:?}", e))?;
let selected = self.list.get_selected_index();
let len = self.queue.len();
if dir == "up" && selected > 0 {
self.queue.shift(selected, selected.saturating_sub(amount));
self.list.move_focus(-(amount as i32));
return Ok(CommandResult::Consumed(None));
} else if dir == "down" && selected < len.saturating_sub(1) {
self.queue
.shift(selected, min(selected + amount as usize, len - 1));
self.list.move_focus(amount as i32);
return Ok(CommandResult::Consumed(None));
match mode {
ShiftMode::Up if selected > 0 => {
self.queue.shift(selected, selected.saturating_sub(amount));
self.list.move_focus(-(amount as i32));
return Ok(CommandResult::Consumed(None));
}
ShiftMode::Down if selected < len.saturating_sub(1) => {
self.queue
.shift(selected, min(selected + amount as usize, len - 1));
self.list.move_focus(amount as i32);
return Ok(CommandResult::Consumed(None));
}
_ => {}
}
}
Command::SaveQueue => {
let dialog = Self::save_dialog(self.queue.clone(), self.library.clone());
s.add_layer(dialog);
return Ok(CommandResult::Consumed(None));
}
_ => {}
}
if cmd == "save" && args.get(0).unwrap_or(&"".to_string()) == "queue" {
let dialog = Self::save_dialog(self.queue.clone(), self.library.clone());
s.add_layer(dialog);
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)).unwrap()
}
}

View File

@@ -11,6 +11,7 @@ use std::sync::{Arc, Mutex, RwLock};
use album::Album;
use artist::Artist;
use command::{Command, MoveMode};
use commands::CommandResult;
use events::EventManager;
use library::Library;
@@ -435,42 +436,37 @@ impl View for SearchView {
}
impl ViewExt for SearchView {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
if cmd == "search" && !args.is_empty() {
self.run_search(args.join(" "));
return Ok(CommandResult::Consumed(None));
}
if cmd == "focus" {
self.edit_focused = true;
self.clear();
return Ok(CommandResult::Consumed(None));
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Search(query) => self.run_search(query.to_string()),
Command::Focus(view) => {
self.edit_focused = true;
self.clear();
return Ok(CommandResult::Consumed(None));
}
_ => {}
}
let result = if !self.edit_focused {
self.tabs.on_command(s, cmd, args)?
self.tabs.on_command(s, cmd)?
} else {
CommandResult::Ignored
};
if let CommandResult::Ignored = result {
if cmd == "move" {
if let Some(dir) = args.get(0) {
if dir == "up" && !self.edit_focused {
match cmd {
Command::Move(mode, amount) => match mode {
MoveMode::Up if !self.edit_focused => {
self.edit_focused = true;
return Ok(CommandResult::Consumed(None));
}
if dir == "down" && self.edit_focused {
MoveMode::Down if self.edit_focused => {
self.edit_focused = false;
return Ok(CommandResult::Consumed(None));
}
}
_ => {}
},
_ => {}
}
}

View File

@@ -7,6 +7,7 @@ use cursive::traits::View;
use cursive::{Cursive, Printer, Vec2};
use unicode_width::UnicodeWidthStr;
use command::{Command, MoveMode};
use commands::CommandResult;
use traits::{IntoBoxedViewExt, ViewExt};
@@ -102,36 +103,33 @@ impl View for TabView {
}
impl ViewExt for TabView {
fn on_command(
&mut self,
s: &mut Cursive,
cmd: &str,
args: &[String],
) -> Result<CommandResult, String> {
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))?;
fn on_command(&mut self, s: &mut Cursive, cmd: &Command) -> Result<CommandResult, String> {
match cmd {
Command::Move(mode, amount) => {
let amount = match amount {
Some(amount) => *amount,
_ => 1,
};
let len = self.tabs.len();
if dir == "left" && self.selected > 0 {
self.move_focus(-amount);
return Ok(CommandResult::Consumed(None));
}
if dir == "right" && self.selected < len - 1 {
self.move_focus(amount);
return Ok(CommandResult::Consumed(None));
match mode {
MoveMode::Left if self.selected > 0 => {
self.move_focus(-(amount as i32));
return Ok(CommandResult::Consumed(None));
}
MoveMode::Right if self.selected < len - 1 => {
self.move_focus(amount as i32);
return Ok(CommandResult::Consumed(None));
}
_ => {}
}
}
_ => {}
}
if let Some(tab) = self.tabs.get_mut(self.selected) {
tab.view.on_command(s, cmd, args)
tab.view.on_command(s, cmd)
} else {
Ok(CommandResult::Ignored)
}