Skip to content

Commit

Permalink
Format keys identically in statusline and command palette (helix-edit…
Browse files Browse the repository at this point in the history
…or#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
  • Loading branch information
sudormrfbin authored Jun 21, 2022
1 parent 8ad0b83 commit 8b67acf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
5 changes: 2 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,9 +2237,8 @@ pub fn command_palette(cx: &mut Context) {
.iter()
.map(|bind| {
bind.iter()
.map(|key| key.to_string())
.collect::<Vec<String>>()
.join("+")
.map(|key| key.key_sequence_format())
.collect::<String>()
})
.collect::<Vec<String>>()
.join(", ")
Expand Down
8 changes: 1 addition & 7 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use helix_core::{
},
movement::Direction,
syntax::{self, HighlightEvent},
unicode::segmentation::UnicodeSegmentation,
unicode::width::UnicodeWidthStr,
LineEnding, Position, Range, Selection, Transaction,
};
Expand Down Expand Up @@ -1391,12 +1390,7 @@ impl Component for EditorView {
disp.push_str(&count.to_string())
}
for key in self.keymaps.pending() {
let s = key.to_string();
if s.graphemes(true).count() > 1 {
disp.push_str(&format!("<{}>", s));
} else {
disp.push_str(&s);
}
disp.push_str(&key.key_sequence_format());
}
if let Some(pseudo_pending) = &cx.editor.pseudo_pending {
disp.push_str(pseudo_pending.as_str())
Expand Down
27 changes: 26 additions & 1 deletion helix-view/src/input.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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, "<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 {
Expand Down

0 comments on commit 8b67acf

Please sign in to comment.