Skip to content

Commit

Permalink
Implement context name for modes
Browse files Browse the repository at this point in the history
  • Loading branch information
nick42d committed Dec 5, 2024
1 parent 27f404a commit 01c321b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
1 change: 0 additions & 1 deletion youtui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use ui::YoutuiWindow;

#[macro_use]
pub mod component;
pub mod keycommand;
mod server;
mod structures;
pub mod ui;
Expand Down
35 changes: 21 additions & 14 deletions youtui/src/config/keymap.rs
Original file line number Diff line number Diff line change
@@ -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<A> = BTreeMap<Keybind, KeyActionTree<A>>;
Expand Down Expand Up @@ -243,7 +246,7 @@ impl YoutuiKeymap {
}
}

impl<A> KeyActionTree<A> {
impl<A: Action> KeyActionTree<A> {
pub fn new_key_defaulted(action: A) -> Self {
Self::Key(KeyAction {
action,
Expand Down Expand Up @@ -317,10 +320,14 @@ impl<A> KeyActionTree<A> {
/// 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<str> {
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(),
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions youtui/src/keyaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 01c321b

Please sign in to comment.