From 8098e9bdcd85890d86b45be4e889604f651d7f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 29 Mar 2021 16:47:02 +0900 Subject: [PATCH] Allow setting a status message. --- helix-term/src/commands.rs | 12 +++++++++++- helix-term/src/ui/editor.rs | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index d2aa481af..fa7a41625 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -35,6 +35,7 @@ pub struct Context<'a> { pub callback: Option, pub on_next_key_callback: Option>, pub callbacks: &'a mut LspCallbacks, + pub status_msg: Option, } use futures_util::FutureExt; @@ -87,6 +88,11 @@ impl<'a> Context<'a> { }); self.callbacks.push(callback); } + + // TODO: allow &'static str? + pub fn set_status(&mut self, msg: String) { + self.status_msg = Some(msg); + } } /// A command is a function that takes the current state and a count, and does a side-effect on the @@ -1378,7 +1384,7 @@ pub fn redo(cx: &mut Context) { pub fn yank(cx: &mut Context) { // TODO: should selections be made end inclusive? let doc = cx.doc(); - let values = doc + let values: Vec = doc .selection() .fragments(doc.text().slice(..)) .map(|cow| cow.into_owned()) @@ -1386,7 +1392,11 @@ pub fn yank(cx: &mut Context) { // TODO: allow specifying reg let reg = '"'; + let msg = format!("yanked {} selection(s) to register {}", values.len(), reg); + register::set(reg, values); + + cx.set_status(msg) } pub fn paste(cx: &mut Context) { diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index bd0398a22..b02bd9811 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -26,6 +26,7 @@ use tui::{ pub struct EditorView { keymap: Keymaps, on_next_key: Option>, + status_msg: Option, } const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter @@ -35,6 +36,7 @@ impl EditorView { Self { keymap: keymap::default(), on_next_key: None, + status_msg: None, } } @@ -68,6 +70,12 @@ impl EditorView { 1, ); self.render_statusline(&doc, area, surface, theme, is_focused); + + // render status + if let Some(status_msg) = &self.status_msg { + let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender + surface.set_string(viewport.x, viewport.y + viewport.height, status_msg, style); + } } pub fn render_buffer( @@ -441,8 +449,12 @@ impl Component for EditorView { callback: None, callbacks: cx.callbacks, on_next_key_callback: None, + status_msg: None, }; + // clear status + self.status_msg = None; + if let Some(on_next_key) = self.on_next_key.take() { // if there's a command waiting input, do that first on_next_key(&mut cxt, event); @@ -486,6 +498,7 @@ impl Component for EditorView { } self.on_next_key = cxt.on_next_key_callback.take(); + self.status_msg = cxt.status_msg.take(); // appease borrowck let callback = cxt.callback.take();