Lines
100 %
Functions
83.33 %
Branches
//! The bindings the focused chain offers, shadowing resolved: what the status bar,
//! Help and the footers show.
use tvui::keymap::Chord;
use super::scopes;
use crate::app::App;
use crate::event::{Intent, acts, resolve_overlay};
use crate::form::Form;
use crate::modal::Modal;
use crate::widgets::WidgetKind;
use crate::window::WindowId;
/// A binding some key press reaches from the focused chain.
pub(super) struct Shown<'a> {
pub scope: &'static str,
pub chords: &'a [Chord],
pub intent: Intent,
}
/// The chords as typed, `C-x C-s`.
pub(super) fn chord_text(chords: &[Chord]) -> String {
let names: Vec<String> = chords.iter().map(Chord::to_string).collect();
names.join(" ")
/// The keys of one scope chain that do something now: innermost scope first; a chord an
/// inner scope binds hides the outer binding of it, and an intent that does nothing in
/// the focus of `app` is left out.
pub struct KeyView<'a> {
pub(super) shown: Vec<Shown<'a>>,
impl<'a> KeyView<'a> {
pub(super) fn of(app: &'a App, chain: &[&'static str]) -> Self {
Self::collect(app, chain, |intent| acts(app, intent))
/// The keys of `chain` whose intent satisfies `keep`.
fn collect(app: &'a App, chain: &[&'static str], keep: impl Fn(Intent) -> bool) -> Self {
let map = &app.keys.map;
let mut shown: Vec<Shown<'a>> = Vec::new();
for (depth, scope) in chain.iter().enumerate() {
let inner = &chain[..depth];
for (chords, intent) in map.bindings(scope) {
let hidden = inner
.iter()
.any(|outer| map.bindings(outer).any(|(other, _)| other == chords));
if !hidden && keep(*intent) {
shown.push(Shown {
scope,
chords,
intent: *intent,
});
KeyView { shown }
/// The first key of the chain that does `intent`, as typed.
#[must_use]
pub fn key(&self, intent: Intent) -> Option<String> {
let found = self.shown.iter().find(|shown| shown.intent == intent);
found.map(|shown| chord_text(shown.chords))
/// The key of the outermost scope of the chain that does `intent`, as typed: the
/// primary chord, where inner scopes only alias it.
pub fn outermost_key(&self, intent: Intent) -> Option<String> {
let outer = self
.shown
.rev()
.find(|shown| shown.intent == intent)?;
let first = self
.find(|shown| shown.intent == intent && shown.scope == outer.scope);
first.map(|shown| chord_text(shown.chords))
/// The intent the chain binds to `key`, as typed.
pub fn bound(&self, key: &str) -> Option<Intent> {
let found = self
.find(|shown| chord_text(shown.chords) == key);
found.map(|shown| shown.intent)
/// Every key of the chain that does `intent`, as `Esc / q`.
pub fn all(&self, intent: Intent) -> String {
let keys: Vec<String> = self
.filter(|shown| shown.intent == intent)
.map(|shown| chord_text(shown.chords))
.collect();
keys.join(" / ")
/// The keys of the chain the focused layer uses.
pub fn here(app: &App) -> KeyView<'_> {
KeyView::of(app, &scopes::chain(app).names)
/// The keys of the view scopes of the active tab, whatever has the focus.
pub fn in_view(app: &App) -> KeyView<'_> {
KeyView::of(app, &scopes::front_view(app).names)
/// The keys of the view scopes of window `id`, whatever has the focus.
pub fn in_window(app: &App, id: WindowId) -> KeyView<'_> {
KeyView::of(app, &scopes::window_view(app, id).names)
/// The keys of the view scopes of the active tab, those that do nothing now included.
pub fn in_view_all(app: &App) -> KeyView<'_> {
KeyView::collect(app, &scopes::front_view(app).names, |_| true)
/// The keys of `modal` when it is the topmost overlay.
pub fn in_modal<'a>(app: &'a App, modal: &Modal) -> KeyView<'a> {
KeyView::of(app, &scopes::overlay(modal).names)
/// The keys of `form` as its focused field has them.
pub fn in_form<'a>(app: &'a App, form: &Form) -> KeyView<'a> {
KeyView::of(
app,
&scopes::form_scopes(form.focused_widget_kind(), form.focused_select()).names,
)
/// The keys of a rows field, whether or not it has the focus.
pub fn in_rows(app: &App) -> KeyView<'_> {
&scopes::form_scopes(Some(WidgetKind::Rows), None).names,
/// A status bar item: the intent a click on it applies, the hotkey and the label.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hint {
pub key: String,
pub label: String,
impl Hint {
pub fn pair(&self) -> (String, String) {
(self.key.clone(), self.label.clone())
/// The intent, hotkey and label of each action the chain's scopes list as hints: `global` first, a
/// fixed place on the bar, then the inner scopes innermost first. The key is the innermost
/// binding of the intent from the hinting scope outwards, if it does something now.
pub fn hinted(app: &App) -> Vec<Hint> {
let chain = scopes::chain(app).names;
let view = KeyView::of(app, &chain);
let (global, inner) = chain
.split_last()
.map_or((None, &[][..]), |(g, i)| (Some(g), i));
let mut done: Vec<Intent> = Vec::new();
let mut bar = Vec::new();
for scope in global.into_iter().chain(inner) {
for intent in app.keys.map.hinted(scope) {
let from = chain.iter().position(|name| name == scope).unwrap_or(0);
let own = view
.find(|shown| shown.intent == *intent && chain[from..].contains(&shown.scope));
if let (Some(own), Some(label), false) =
(own, hint_label(app, *intent), done.contains(intent))
{
bar.push(Hint {
key: chord_text(own.chords),
label: button_label(app, *intent, label),
done.push(*intent);
bar
/// The label of `intent` as what it does now: a key that tests a template form says so.
fn hint_label(app: &App, intent: Intent) -> Option<&'static str> {
match resolve_overlay(app, intent) {
resolved @ Intent::Template(_) => resolved.label(),
_ => intent.label(),
/// The label of the top modal's button that presses `intent`, else `label`.
fn button_label(app: &App, intent: Intent, label: &str) -> String {
let specs = app.overlays.top().map_or(&[][..], Modal::button_specs);
specs
.find(|&&(_, pressed)| pressed == intent)
.map_or_else(|| label.to_string(), |&(text, _)| tvui::plain(text))