From 01c321bf5b54c651c3864983fa688759f0d65bda Mon Sep 17 00:00:00 2001 From: Nick Dowsett Date: Thu, 5 Dec 2024 18:18:38 +0800 Subject: [PATCH] Implement context name for modes --- youtui/src/app.rs | 1 - youtui/src/config/keymap.rs | 35 +++++++++++++++++++++-------------- youtui/src/keyaction.rs | 8 ++++++-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/youtui/src/app.rs b/youtui/src/app.rs index 69b3b73..c427f8f 100644 --- a/youtui/src/app.rs +++ b/youtui/src/app.rs @@ -21,7 +21,6 @@ use ui::YoutuiWindow; #[macro_use] pub mod component; -pub mod keycommand; mod server; mod structures; pub mod ui; diff --git a/youtui/src/config/keymap.rs b/youtui/src/config/keymap.rs index d5e5ab9..f84df05 100644 --- a/youtui/src/config/keymap.rs +++ b/youtui/src/config/keymap.rs @@ -1,22 +1,25 @@ use crate::{ - app::ui::{ - action::{AppAction, HelpAction, ListAction, TextEntryAction}, - browser::{ - artistalbums::{ - albumsongs::{BrowserSongsAction, FilterAction, SortAction}, - artistsearch::{BrowserArtistsAction, BrowserSearchAction}, + app::{ + component::actionhandler::Action, + ui::{ + action::{AppAction, HelpAction, ListAction, TextEntryAction}, + browser::{ + artistalbums::{ + albumsongs::{BrowserSongsAction, FilterAction, SortAction}, + artistsearch::{BrowserArtistsAction, BrowserSearchAction}, + }, + BrowserAction, }, - BrowserAction, + logger::LoggerAction, + playlist::PlaylistAction::{self, ViewBrowser}, }, - logger::LoggerAction, - playlist::PlaylistAction::{self, ViewBrowser}, }, keyaction::{KeyAction, KeyActionVisibility}, keybind::Keybind, }; use crossterm::event::KeyModifiers; use serde::{Deserialize, Serialize}; -use std::{collections::BTreeMap, convert::Infallible, str::FromStr}; +use std::{borrow::Cow, collections::BTreeMap, convert::Infallible, str::FromStr}; /// Convenience type alias pub type Keymap = BTreeMap>; @@ -243,7 +246,7 @@ impl YoutuiKeymap { } } -impl KeyActionTree { +impl KeyActionTree { pub fn new_key_defaulted(action: A) -> Self { Self::Key(KeyAction { action, @@ -317,10 +320,14 @@ impl KeyActionTree { /// If a key, get the context of the key's action. /// If a mode, recursively get the context of the first key's keyactiontree. /// Returns String::default() if no keys in the mode. - pub fn get_context(&self) -> String { + pub fn get_context(&self) -> Cow { match self { - KeyActionTree::Key(k) => k.action.context() - KeyActionTree::Mode { keys, .. } => keys.iter().next().unwrap_or_default().1.get_context(), + KeyActionTree::Key(k) => k.action.context(), + KeyActionTree::Mode { keys, .. } => keys + .iter() + .next() + .map(|(_, kt)| kt.get_context()) + .unwrap_or_default(), } } } diff --git a/youtui/src/keyaction.rs b/youtui/src/keyaction.rs index 7018bb8..817d894 100644 --- a/youtui/src/keyaction.rs +++ b/youtui/src/keyaction.rs @@ -55,9 +55,13 @@ impl<'a> DisplayableKeyAction<'a> { context: k.action.context(), description: k.action.describe(), }, - KeyActionTree::Mode { ref mut name, keys } => DisplayableKeyAction { + KeyActionTree::Mode { name, keys } => DisplayableKeyAction { keybinds: key.to_string().into(), - context: keys.iter().next().unwrap_or_default().1.get_context(), + context: keys + .iter() + .next() + .map(|(_, kt)| kt.get_context()) + .unwrap_or_default(), description: name .as_ref() .map(ToOwned::to_owned)