From 349d30a0e0c8c1a841d905e1db4b2f53dd91745a Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Tue, 27 Jul 2021 10:26:42 +0530 Subject: [PATCH] Show pending keys and counts in status line --- helix-term/src/keymap.rs | 16 ++++++++++++++++ helix-term/src/ui/editor.rs | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index 93cc53289d710..416f4152b4521 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -234,6 +234,7 @@ pub struct Keymap { /// Always a Node #[serde(flatten)] root: KeyTrie, + /// Stores pending keys waiting for the next key #[serde(skip)] state: Vec, } @@ -250,6 +251,14 @@ impl Keymap { &self.root } + /// Returns list of keys waiting to be disambiguated. + pub fn pending(&self) -> Option<&Vec> { + match self.state.is_empty() { + true => None, + false => Some(&self.state), + } + } + /// Lookup `key` in the keymap to try and find a command to execute pub fn get(&mut self, key: KeyEvent) -> KeymapResult { let &first = self.state.get(0).unwrap_or(&key); @@ -292,6 +301,13 @@ impl Default for Keymap { #[serde(transparent)] pub struct Keymaps(pub HashMap); +impl Keymaps { + /// Returns list of keys waiting to be disambiguated in current mode. + pub fn pending(&self) -> Option<&Vec> { + self.0.values().find_map(|keymap| keymap.pending()) + } +} + impl Deref for Keymaps { type Target = HashMap; diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 78a54079d7f58..9813db47cef3c 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -10,6 +10,7 @@ use helix_core::{ coords_at_pos, graphemes::{ensure_grapheme_boundary, next_grapheme_boundary}, syntax::{self, HighlightEvent}, + unicode::width::UnicodeWidthStr, LineEnding, Position, Range, }; use helix_view::{ @@ -810,6 +811,29 @@ impl Component for EditorView { status_msg, style, ); + } else { + let mut disp = String::new(); + if let Some(count) = cx.editor.count { + disp.push_str(&count.get().to_string()) + } + if let Some(keys) = self.keymaps.pending() { + for key in keys { + let s = key.to_string(); + if s.width() > 1 { + disp.push_str(&format!("<{}>", s)); + } else { + disp.push_str(&s); + } + } + } + let width = 10usize; + surface.set_string( + area.x + area.width.saturating_sub(width as u16), + area.y + area.height.saturating_sub(1), + disp.get(disp.len().saturating_sub(width)..) + .unwrap_or(&disp), + cx.editor.theme.get("ui.text"), + ); } if let Some(completion) = &self.completion {