Set the cursor position appropriately.

This makes international input popups get placed correctly for
terminals that do that.
This commit is contained in:
Nathan Vegdahl 2020-02-04 20:54:38 +09:00
parent 0b8c590dea
commit f582818387
2 changed files with 17 additions and 1 deletions

View File

@ -357,6 +357,7 @@ impl TermUI {
&line[..],
STYLE_INFO,
);
self.screen.set_cursor(prefix.len() + 1, 0);
},
// Handle input
@ -551,6 +552,7 @@ impl TermUI {
for c in editor.cursors.iter() {
if char_index >= c.range.0 && char_index <= c.range.1 {
at_cursor = true;
self.screen.set_cursor(px as usize, py as usize);
}
}
@ -639,6 +641,7 @@ impl TermUI {
{
self.screen
.draw(px as usize, py as usize, " ", STYLE_CURSOR);
self.screen.set_cursor(px as usize, py as usize);
}
}
}

View File

@ -1,5 +1,5 @@
use std;
use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::io;
use std::io::{BufWriter, Write};
@ -15,6 +15,7 @@ use super::smallstring::SmallString;
pub(crate) struct Screen {
out: RefCell<BufWriter<io::Stdout>>,
buf: RefCell<Vec<Option<(Style, SmallString)>>>,
main_cursor: Cell<(u16, u16)>,
w: usize,
h: usize,
}
@ -40,6 +41,7 @@ impl Screen {
Screen {
out: RefCell::new(out),
buf: RefCell::new(buf),
main_cursor: Cell::new((0, 0)),
w: w as usize,
h: h as usize,
}
@ -113,10 +115,21 @@ impl Screen {
}
}
let cursor_pos = self.main_cursor.get();
queue!(out, crossterm::cursor::MoveTo(cursor_pos.0, cursor_pos.1)).unwrap();
self.main_cursor.set((0, 0));
// Make sure everything is written out from the buffer.
out.flush().unwrap();
}
pub(crate) fn set_cursor(&self, x: usize, y: usize) {
self.main_cursor.set((
x.min(self.w.saturating_sub(1)) as u16,
y.min(self.h.saturating_sub(1)) as u16,
));
}
pub(crate) fn draw(&self, x: usize, y: usize, text: &str, style: Style) {
if y < self.h {
let mut buf = self.buf.borrow_mut();