on master help sign in
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 <C-w>). This patch changes the palette formatting to the statusline formatting

commit8b67acf130e12cf8aaa439fe19ea7b8917db300b authorGokul Soumya <gokulps15@gmail.com> dateTue 21 Jun 2022, 22:16 (4 years ago) parent8ad0b83
3 files changed+29−11
helix-term/src/commands.rs+2−3 helix-term/src/ui/editor.rs+1−7 helix-view/src/input.rs+26−1
helix-term/src/commands.rs+2−3
@@ -2237,9 +2237,8 @@ pub fn command_palette(cx: &mut Context) {
22372237 .iter()
22382238 .map(|bind| {
22392239 bind.iter()
2240− .map(|key| key.to_string())
2241− .collect::<Vec<String>>()
2242− .join("+")
2240+ .map(|key| key.key_sequence_format())
2241+ .collect::<String>()
22432242 })
22442243 .collect::<Vec<String>>()
22452244 .join(", ")
helix-term/src/ui/editor.rs+1−7
@@ -13,7 +13,6 @@ use helix_core::{
1313 },
1414 movement::Direction,
1515 syntax::{self, HighlightEvent},
16− unicode::segmentation::UnicodeSegmentation,
1716 unicode::width::UnicodeWidthStr,
1817 LineEnding, Position, Range, Selection, Transaction,
1918};
@@ -1391,12 +1390,7 @@ impl Component for EditorView {
13911390 disp.push_str(&count.to_string())
13921391 }
13931392 for key in self.keymaps.pending() {
1394− let s = key.to_string();
1395− if s.graphemes(true).count() > 1 {
1396− disp.push_str(&format!("<{}>", s));
1397− } else {
1398− disp.push_str(&s);
1399− }
1393+ disp.push_str(&key.key_sequence_format());
14001394 }
14011395 if let Some(pseudo_pending) = &cx.editor.pseudo_pending {
14021396 disp.push_str(pseudo_pending.as_str())
helix-view/src/input.rs+26−1
@@ -1,6 +1,6 @@
11//! Input event handling, currently backed by crossterm.
22use anyhow::{anyhow, Error};
3−use helix_core::unicode::width::UnicodeWidthStr;
3+use helix_core::unicode::{segmentation::UnicodeSegmentation, width::UnicodeWidthStr};
44use serde::de::{self, Deserialize, Deserializer};
55use std::fmt;
66
@@ -22,6 +22,31 @@ impl KeyEvent {
2222 _ => None,
2323 }
2424 }
25+
26+ /// Format the key in such a way that a concatenated sequence
27+ /// of keys can be read easily.
28+ ///
29+ /// ```
30+ /// # use std::str::FromStr;
31+ /// # use helix_view::input::KeyEvent;
32+ ///
33+ /// let k = KeyEvent::from_str("w").unwrap().key_sequence_format();
34+ /// assert_eq!(k, "w");
35+ ///
36+ /// let k = KeyEvent::from_str("C-w").unwrap().key_sequence_format();
37+ /// assert_eq!(k, "<C-w>");
38+ ///
39+ /// let k = KeyEvent::from_str(" ").unwrap().key_sequence_format();
40+ /// assert_eq!(k, "<space>");
41+ /// ```
42+ pub fn key_sequence_format(&self) -> String {
43+ let s = self.to_string();
44+ if s.graphemes(true).count() > 1 {
45+ format!("<{}>", s)
46+ } else {
47+ s
48+ }
49+ }
2550}
2651
2752pub(crate) mod keys {