From 8b67acf130e12cf8aaa439fe19ea7b8917db300b Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Tue, 21 Jun 2022 22:16:50 +0530 Subject: Format keys identically in statusline and command palette (#2790) The command palette previously used + as a delimiter for denoting a single key in a key sequence, (like C+w). This was at odds with how the statusline displayed them with pending keys (like ). This patch changes the palette formatting to the statusline formatting--- helix-view/src/input.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'helix-view/src') diff --git a/helix-view/src/input.rs b/helix-view/src/input.rs index 5b867930..093006c4 100644 --- a/helix-view/src/input.rs +++ b/helix-view/src/input.rs @@ -1,6 +1,6 @@ //! Input event handling, currently backed by crossterm. use anyhow::{anyhow, Error}; -use helix_core::unicode::width::UnicodeWidthStr; +use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidthStr}; use serde::de::{self, Deserialize, Deserializer}; use std::fmt; @@ -22,6 +22,31 @@ impl KeyEvent { _ => None, } } + + /// Format the key in such a way that a concatenated sequence + /// of keys can be read easily. + /// + /// ``` + /// # use std::str::FromStr; + /// # use helix_view::input::KeyEvent; + /// + /// let k = KeyEvent::from_str("w").unwrap().key_sequence_format(); + /// assert_eq!(k, "w"); + /// + /// let k = KeyEvent::from_str("C-w").unwrap().key_sequence_format(); + /// assert_eq!(k, ""); + /// + /// let k = KeyEvent::from_str(" ").unwrap().key_sequence_format(); + /// assert_eq!(k, ""); + /// ``` + pub fn key_sequence_format(&self) -> String { + let s = self.to_string(); + if s.graphemes(true).count() > 1 { + format!("<{}>", s) + } else { + s + } + } } pub(crate) mod keys { -- cgit v1.2.3-70-g09d2