@@ -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 {