Allow setting a status message.

imgbot
Blaž Hrastnik 4 years ago
parent 742b3a709f
commit 8098e9bdcd

@ -35,6 +35,7 @@ pub struct Context<'a> {
pub callback: Option<crate::compositor::Callback>, pub callback: Option<crate::compositor::Callback>,
pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>, pub on_next_key_callback: Option<Box<dyn FnOnce(&mut Context, KeyEvent)>>,
pub callbacks: &'a mut LspCallbacks, pub callbacks: &'a mut LspCallbacks,
pub status_msg: Option<String>,
} }
use futures_util::FutureExt; use futures_util::FutureExt;
@ -87,6 +88,11 @@ impl<'a> Context<'a> {
}); });
self.callbacks.push(callback); 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 /// 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) { pub fn yank(cx: &mut Context) {
// TODO: should selections be made end inclusive? // TODO: should selections be made end inclusive?
let doc = cx.doc(); let doc = cx.doc();
let values = doc let values: Vec<String> = doc
.selection() .selection()
.fragments(doc.text().slice(..)) .fragments(doc.text().slice(..))
.map(|cow| cow.into_owned()) .map(|cow| cow.into_owned())
@ -1386,7 +1392,11 @@ pub fn yank(cx: &mut Context) {
// TODO: allow specifying reg // TODO: allow specifying reg
let reg = '"'; let reg = '"';
let msg = format!("yanked {} selection(s) to register {}", values.len(), reg);
register::set(reg, values); register::set(reg, values);
cx.set_status(msg)
} }
pub fn paste(cx: &mut Context) { pub fn paste(cx: &mut Context) {

@ -26,6 +26,7 @@ use tui::{
pub struct EditorView { pub struct EditorView {
keymap: Keymaps, keymap: Keymaps,
on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>, on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
status_msg: Option<String>,
} }
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@ -35,6 +36,7 @@ impl EditorView {
Self { Self {
keymap: keymap::default(), keymap: keymap::default(),
on_next_key: None, on_next_key: None,
status_msg: None,
} }
} }
@ -68,6 +70,12 @@ impl EditorView {
1, 1,
); );
self.render_statusline(&doc, area, surface, theme, is_focused); 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( pub fn render_buffer(
@ -441,8 +449,12 @@ impl Component for EditorView {
callback: None, callback: None,
callbacks: cx.callbacks, callbacks: cx.callbacks,
on_next_key_callback: None, 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 let Some(on_next_key) = self.on_next_key.take() {
// if there's a command waiting input, do that first // if there's a command waiting input, do that first
on_next_key(&mut cxt, event); 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.on_next_key = cxt.on_next_key_callback.take();
self.status_msg = cxt.status_msg.take();
// appease borrowck // appease borrowck
let callback = cxt.callback.take(); let callback = cxt.callback.take();

Loading…
Cancel
Save