Add CursiveExt trait
For easier usage of shared logic involving the `Cursive` instance.
This commit is contained in:
@@ -7,6 +7,7 @@ use crate::command::{
|
|||||||
};
|
};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::events::EventManager;
|
use crate::events::EventManager;
|
||||||
|
use crate::ext_traits::CursiveExt;
|
||||||
use crate::library::Library;
|
use crate::library::Library;
|
||||||
use crate::queue::{Queue, RepeatSetting};
|
use crate::queue::{Queue, RepeatSetting};
|
||||||
use crate::spotify::{Spotify, VOLUME_PERCENT};
|
use crate::spotify::{Spotify, VOLUME_PERCENT};
|
||||||
@@ -310,10 +311,7 @@ impl CommandManager {
|
|||||||
} else if let Some(mut play_track) = s.find_name::<PlayTrackMenu>("playtrackmenu") {
|
} else if let Some(mut play_track) = s.find_name::<PlayTrackMenu>("playtrackmenu") {
|
||||||
play_track.on_command(s, cmd)?
|
play_track.on_command(s, cmd)?
|
||||||
} else {
|
} else {
|
||||||
let mut main = s
|
s.on_layout(|siv, mut l| l.on_command(siv, cmd))?
|
||||||
.find_name::<Layout>("main")
|
|
||||||
.expect("could not find layout");
|
|
||||||
main.on_command(s, cmd)?
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if let CommandResult::Consumed(output) = local {
|
if let CommandResult::Consumed(output) = local {
|
||||||
|
|||||||
21
src/ext_traits.rs
Normal file
21
src/ext_traits.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use cursive::views::ViewRef;
|
||||||
|
|
||||||
|
use crate::ui::layout::Layout;
|
||||||
|
|
||||||
|
pub trait CursiveExt {
|
||||||
|
fn on_layout<F, R>(&mut self, cb: F) -> R
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut cursive::Cursive, ViewRef<Layout>) -> R;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CursiveExt for cursive::Cursive {
|
||||||
|
fn on_layout<F, R>(&mut self, cb: F) -> R
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut cursive::Cursive, ViewRef<Layout>) -> R,
|
||||||
|
{
|
||||||
|
let layout = self
|
||||||
|
.find_name::<Layout>("main")
|
||||||
|
.expect("Could not find Layout");
|
||||||
|
cb(self, layout)
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/main.rs
10
src/main.rs
@@ -23,6 +23,7 @@ mod command;
|
|||||||
mod commands;
|
mod commands;
|
||||||
mod config;
|
mod config;
|
||||||
mod events;
|
mod events;
|
||||||
|
mod ext_traits;
|
||||||
mod library;
|
mod library;
|
||||||
mod model;
|
mod model;
|
||||||
mod queue;
|
mod queue;
|
||||||
@@ -44,6 +45,7 @@ use crate::command::{Command, JumpMode};
|
|||||||
use crate::commands::CommandManager;
|
use crate::commands::CommandManager;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::events::{Event, EventManager};
|
use crate::events::{Event, EventManager};
|
||||||
|
use crate::ext_traits::CursiveExt;
|
||||||
use crate::library::Library;
|
use crate::library::Library;
|
||||||
use crate::spotify::PlayerEvent;
|
use crate::spotify::PlayerEvent;
|
||||||
use crate::ui::contextmenu::ContextMenu;
|
use crate::ui::contextmenu::ContextMenu;
|
||||||
@@ -282,10 +284,7 @@ async fn main() -> Result<(), String> {
|
|||||||
{
|
{
|
||||||
let ev = event_manager.clone();
|
let ev = event_manager.clone();
|
||||||
layout.cmdline.set_on_submit(move |s, cmd| {
|
layout.cmdline.set_on_submit(move |s, cmd| {
|
||||||
{
|
s.on_layout(|_, mut layout| layout.clear_cmdline());
|
||||||
let mut main = s.find_name::<ui::layout::Layout>("main").unwrap();
|
|
||||||
main.clear_cmdline();
|
|
||||||
}
|
|
||||||
let cmd_without_prefix = &cmd[1..];
|
let cmd_without_prefix = &cmd[1..];
|
||||||
if cmd.strip_prefix('/').is_some() {
|
if cmd.strip_prefix('/').is_some() {
|
||||||
let command = Command::Jump(JumpMode::Query(cmd_without_prefix.to_string()));
|
let command = Command::Jump(JumpMode::Query(cmd_without_prefix.to_string()));
|
||||||
@@ -302,8 +301,7 @@ async fn main() -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let mut main = s.find_name::<ui::layout::Layout>("main").unwrap();
|
s.on_layout(|_, mut layout| layout.set_result(Err(err.to_string())));
|
||||||
main.set_result(Err(err.to_string()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user