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
pull/2851/head
Gokul Soumya 2 years ago committed by GitHub
parent 8ad0b83e30
commit 8b67acf130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2237,9 +2237,8 @@ pub fn command_palette(cx: &mut Context) {
.iter() .iter()
.map(|bind| { .map(|bind| {
bind.iter() bind.iter()
.map(|key| key.to_string()) .map(|key| key.key_sequence_format())
.collect::<Vec<String>>() .collect::<String>()
.join("+")
}) })
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", ") .join(", ")

@ -13,7 +13,6 @@ use helix_core::{
}, },
movement::Direction, movement::Direction,
syntax::{self, HighlightEvent}, syntax::{self, HighlightEvent},
unicode::segmentation::UnicodeSegmentation,
unicode::width::UnicodeWidthStr, unicode::width::UnicodeWidthStr,
LineEnding, Position, Range, Selection, Transaction, LineEnding, Position, Range, Selection, Transaction,
}; };
@ -1391,12 +1390,7 @@ impl Component for EditorView {
disp.push_str(&count.to_string()) disp.push_str(&count.to_string())
} }
for key in self.keymaps.pending() { for key in self.keymaps.pending() {
let s = key.to_string(); disp.push_str(&key.key_sequence_format());
if s.graphemes(true).count() > 1 {
disp.push_str(&format!("<{}>", s));
} else {
disp.push_str(&s);
}
} }
if let Some(pseudo_pending) = &cx.editor.pseudo_pending { if let Some(pseudo_pending) = &cx.editor.pseudo_pending {
disp.push_str(pseudo_pending.as_str()) disp.push_str(pseudo_pending.as_str())

@ -1,6 +1,6 @@
//! Input event handling, currently backed by crossterm. //! Input event handling, currently backed by crossterm.
use anyhow::{anyhow, Error}; 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 serde::de::{self, Deserialize, Deserializer};
use std::fmt; use std::fmt;
@ -22,6 +22,31 @@ impl KeyEvent {
_ => None, _ => 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, "<C-w>");
///
/// let k = KeyEvent::from_str(" ").unwrap().key_sequence_format();
/// assert_eq!(k, "<space>");
/// ```
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 { pub(crate) mod keys {

Loading…
Cancel
Save