diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2020-03-30 00:23:43 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2020-03-30 00:23:43 +0800 |
commit | e8c00b709fe1d4470d80e086ba615febba0dfd24 (patch) | |
tree | 2a18b2bfef7e787081bf5c2a8974df2d5ff52fec | |
parent | 0838d9ed97f6be0a5a080170c15605581e0088bb (diff) |
use tui-react to draw text…
…for a chance to get it to be faster.
Right now, it isn't though, graphemes are killing it.
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | tui-react/Cargo.toml | 2 | ||||
-rw-r--r-- | tui-react/src/lib.rs | 37 | ||||
-rw-r--r-- | tui-react/src/list.rs | 28 |
4 files changed, 57 insertions, 12 deletions
@@ -616,7 +616,7 @@ dependencies = [ [[package]] name = "tui-react" -version = "0.2.2" +version = "0.2.3" dependencies = [ "log", "tui", diff --git a/tui-react/Cargo.toml b/tui-react/Cargo.toml index d41605b..ae31d6e 100644 --- a/tui-react/Cargo.toml +++ b/tui-react/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tui-react" -version = "0.2.2" +version = "0.2.3" authors = ["Sebastian Thiel <sthiel@thoughtworks.com>"] edition = "2018" repository = "https://github.com/Byron/dua-cli" diff --git a/tui-react/src/lib.rs b/tui-react/src/lib.rs index de9fad1..991e396 100644 --- a/tui-react/src/lib.rs +++ b/tui-react/src/lib.rs @@ -74,6 +74,43 @@ pub fn draw_text_with_ellipsis_nowrap<'a>( total_width as u16 } +pub fn draw_text_without_ellipsis_nowrap<'a>( + bound: Rect, + buf: &mut Buffer, + text: impl AsRef<str>, + style: impl Into<Option<Style>>, +) -> u16 { + let s = style.into(); + let t = text.as_ref(); + let mut graphemes = t.graphemes(true); + let mut total_width = 0; + { + let mut x_offset = 0; + for (g, mut x) in graphemes.by_ref().zip(bound.left()..bound.right()) { + let width = g.width(); + total_width += width; + + x += x_offset; + let cell = buf.get_mut(x, bound.y); + cell.symbol = g.into(); + if let Some(s) = s { + cell.style = s; + } + + x_offset += width.saturating_sub(1) as u16; + if x + x_offset >= bound.right() { + break; + } + let x = x as usize; + for x in x + 1..x + width { + let i = buf.index_of(x as u16, bound.y); + buf.content[i].reset(); + } + } + } + total_width as u16 +} + pub fn draw_text_nowrap_fn( bound: Rect, buf: &mut Buffer, diff --git a/tui-react/src/list.rs b/tui-react/src/list.rs index 6c7699a..aacb7d9 100644 --- a/tui-react/src/list.rs +++ b/tui-react/src/list.rs @@ -1,7 +1,10 @@ +use crate::draw_text_without_ellipsis_nowrap; +use crate::util::rect::offset_x; use tui::{ buffer::Buffer, layout::Rect, - widgets::{Block, Paragraph, Text, Widget}, + style::Style, + widgets::{Block, Text, Widget}, }; #[derive(Default)] @@ -62,15 +65,20 @@ impl List { .take(list_area.height as usize) { let (x, y) = (list_area.left(), list_area.top() + i as u16); - Paragraph::new(text_iterator.iter()).draw( - Rect { - x, - y, - width: list_area.width, - height: 1, - }, - buf, - ); + let mut bound = Rect { + x, + y, + width: list_area.width, + height: 1, + }; + for text in text_iterator.into_iter() { + let (text, style) = match text { + Text::Raw(s) => (s, Style::default()), + Text::Styled(s, style) => (s, style), + }; + let offset = draw_text_without_ellipsis_nowrap(bound, buf, text, Some(style)); + bound = offset_x(bound, offset); + } } } } |