mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-28 13:31:14 +00:00
Small improvements
This commit is contained in:
parent
c91436baee
commit
85bd76e17d
@ -168,7 +168,7 @@ fn main() {
|
|||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
loop {
|
loop {
|
||||||
tx.send(Event::Tick).unwrap();
|
tx.send(Event::Tick).unwrap();
|
||||||
thread::sleep(time::Duration::from_millis(100));
|
thread::sleep(time::Duration::from_millis(1000));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -8,14 +8,15 @@ use style::Color;
|
|||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Cell {
|
pub struct Cell {
|
||||||
pub symbol: String,
|
|
||||||
pub fg: Color,
|
pub fg: Color,
|
||||||
pub bg: Color,
|
pub bg: Color,
|
||||||
|
pub symbol: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cell {
|
impl Cell {
|
||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.symbol = " ".into();
|
self.symbol.clear();
|
||||||
|
self.symbol.push(' ');
|
||||||
self.fg = Color::Reset;
|
self.fg = Color::Reset;
|
||||||
self.bg = Color::Reset;
|
self.bg = Color::Reset;
|
||||||
}
|
}
|
||||||
@ -93,11 +94,10 @@ impl Buffer {
|
|||||||
self.content[i] = cell;
|
self.content[i] = cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_symbol<S>(&mut self, x: u16, y: u16, symbol: S)
|
pub fn set_symbol(&mut self, x: u16, y: u16, symbol: &str) {
|
||||||
where S: Into<String>
|
|
||||||
{
|
|
||||||
let i = self.index_of(x, y);
|
let i = self.index_of(x, y);
|
||||||
self.content[i].symbol = symbol.into();
|
self.content[i].symbol.clear();
|
||||||
|
self.content[i].symbol.push_str(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_fg(&mut self, x: u16, y: u16, color: Color) {
|
pub fn set_fg(&mut self, x: u16, y: u16, color: Color) {
|
||||||
@ -124,7 +124,8 @@ impl Buffer {
|
|||||||
let graphemes = UnicodeSegmentation::graphemes(string, true).collect::<Vec<&str>>();
|
let graphemes = UnicodeSegmentation::graphemes(string, true).collect::<Vec<&str>>();
|
||||||
let max_index = min((self.area.width - x) as usize, limit);
|
let max_index = min((self.area.width - x) as usize, limit);
|
||||||
for s in graphemes.into_iter().take(max_index) {
|
for s in graphemes.into_iter().take(max_index) {
|
||||||
self.content[index].symbol = s.into();
|
self.content[index].symbol.clear();
|
||||||
|
self.content[index].symbol.push_str(s);
|
||||||
self.content[index].fg = fg;
|
self.content[index].fg = fg;
|
||||||
self.content[index].bg = bg;
|
self.content[index].bg = bg;
|
||||||
index += 1;
|
index += 1;
|
||||||
@ -138,11 +139,10 @@ impl Buffer {
|
|||||||
self.content[i].bg = bg;
|
self.content[i].bg = bg;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_cell<S>(&mut self, x: u16, y: u16, symbol: S, fg: Color, bg: Color)
|
pub fn update_cell(&mut self, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
|
||||||
where S: Into<String>
|
|
||||||
{
|
|
||||||
let i = self.index_of(x, y);
|
let i = self.index_of(x, y);
|
||||||
self.content[i].symbol = symbol.into();
|
self.content[i].symbol.clear();
|
||||||
|
self.content[i].symbol.push_str(symbol);
|
||||||
self.content[i].fg = fg;
|
self.content[i].fg = fg;
|
||||||
self.content[i].bg = bg;
|
self.content[i].bg = bg;
|
||||||
}
|
}
|
||||||
|
80
src/style.rs
80
src/style.rs
@ -20,45 +20,61 @@ pub enum Color {
|
|||||||
Rgb(u8, u8, u8),
|
Rgb(u8, u8, u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! termion_fg {
|
||||||
|
($color:ident) => (format!("{}", termion::color::Fg(termion::color::$color)));
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! termion_fg_rgb {
|
||||||
|
($r:expr, $g:expr, $b:expr) => (format!("{}", termion::color::Fg(termion::color::Rgb($r, $g, $b))));
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! termion_bg {
|
||||||
|
($color:ident) => (format!("{}", termion::color::Bg(termion::color::$color)));
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! termion_bg_rgb {
|
||||||
|
($r:expr, $g:expr, $b:expr) => (format!("{}", termion::color::Bg(termion::color::Rgb($r, $g, $b))));
|
||||||
|
}
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub fn fg(&self) -> String {
|
pub fn fg(&self) -> String {
|
||||||
match *self {
|
match *self {
|
||||||
Color::Reset => format!("{}", termion::color::Fg(termion::color::Reset)),
|
Color::Reset => termion_fg!(Reset),
|
||||||
Color::Black => format!("{}", termion::color::Fg(termion::color::Black)),
|
Color::Black => termion_fg!(Black),
|
||||||
Color::Red => format!("{}", termion::color::Fg(termion::color::Red)),
|
Color::Red => termion_fg!(Red),
|
||||||
Color::Green => format!("{}", termion::color::Fg(termion::color::Green)),
|
Color::Green => termion_fg!(Green),
|
||||||
Color::Yellow => format!("{}", termion::color::Fg(termion::color::Yellow)),
|
Color::Yellow => termion_fg!(Yellow),
|
||||||
Color::Magenta => format!("{}", termion::color::Fg(termion::color::Magenta)),
|
Color::Magenta => termion_fg!(Magenta),
|
||||||
Color::Cyan => format!("{}", termion::color::Fg(termion::color::Cyan)),
|
Color::Cyan => termion_fg!(Cyan),
|
||||||
Color::Gray => format!("{}", termion::color::Fg(termion::color::Rgb(146, 131, 116))),
|
Color::Gray => termion_fg_rgb!(146, 131, 116),
|
||||||
Color::DarkGray => format!("{}", termion::color::Fg(termion::color::Rgb(80, 73, 69))),
|
Color::DarkGray => termion_fg_rgb!(80, 73, 69),
|
||||||
Color::LightRed => format!("{}", termion::color::Fg(termion::color::LightRed)),
|
Color::LightRed => termion_fg!(LightRed),
|
||||||
Color::LightGreen => format!("{}", termion::color::Fg(termion::color::LightGreen)),
|
Color::LightGreen => termion_fg!(LightGreen),
|
||||||
Color::LightYellow => format!("{}", termion::color::Fg(termion::color::LightYellow)),
|
Color::LightYellow => termion_fg!(LightYellow),
|
||||||
Color::LightMagenta => format!("{}", termion::color::Fg(termion::color::LightMagenta)),
|
Color::LightMagenta => termion_fg!(LightMagenta),
|
||||||
Color::LightCyan => format!("{}", termion::color::Fg(termion::color::LightCyan)),
|
Color::LightCyan => termion_fg!(LightCyan),
|
||||||
Color::White => format!("{}", termion::color::Fg(termion::color::White)),
|
Color::White => termion_fg!(White),
|
||||||
Color::Rgb(r, g, b) => format!("{}", termion::color::Fg(termion::color::Rgb(r, g, b))),
|
Color::Rgb(r, g, b) => termion_fg_rgb!(r, g, b),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn bg(&self) -> String {
|
pub fn bg(&self) -> String {
|
||||||
match *self {
|
match *self {
|
||||||
Color::Reset => format!("{}", termion::color::Bg(termion::color::Reset)),
|
Color::Reset => termion_bg!(Reset),
|
||||||
Color::Black => format!("{}", termion::color::Bg(termion::color::Black)),
|
Color::Black => termion_bg!(Black),
|
||||||
Color::Red => format!("{}", termion::color::Bg(termion::color::Red)),
|
Color::Red => termion_bg!(Red),
|
||||||
Color::Green => format!("{}", termion::color::Bg(termion::color::Green)),
|
Color::Green => termion_bg!(Green),
|
||||||
Color::Yellow => format!("{}", termion::color::Bg(termion::color::Yellow)),
|
Color::Yellow => termion_bg!(Yellow),
|
||||||
Color::Magenta => format!("{}", termion::color::Bg(termion::color::Magenta)),
|
Color::Magenta => termion_bg!(Magenta),
|
||||||
Color::Cyan => format!("{}", termion::color::Bg(termion::color::Cyan)),
|
Color::Cyan => termion_bg!(Cyan),
|
||||||
Color::Gray => format!("{}", termion::color::Bg(termion::color::Rgb(146, 131, 116))),
|
Color::Gray => termion_bg_rgb!(146, 131, 116),
|
||||||
Color::DarkGray => format!("{}", termion::color::Bg(termion::color::Rgb(80, 73, 69))),
|
Color::DarkGray => termion_bg_rgb!(80, 73, 69),
|
||||||
Color::LightRed => format!("{}", termion::color::Bg(termion::color::LightRed)),
|
Color::LightRed => termion_bg!(LightRed),
|
||||||
Color::LightGreen => format!("{}", termion::color::Bg(termion::color::LightGreen)),
|
Color::LightGreen => termion_bg!(LightGreen),
|
||||||
Color::LightYellow => format!("{}", termion::color::Bg(termion::color::LightYellow)),
|
Color::LightYellow => termion_bg!(LightYellow),
|
||||||
Color::LightMagenta => format!("{}", termion::color::Bg(termion::color::LightMagenta)),
|
Color::LightMagenta => termion_bg!(LightMagenta),
|
||||||
Color::LightCyan => format!("{}", termion::color::Bg(termion::color::LightCyan)),
|
Color::LightCyan => termion_bg!(LightCyan),
|
||||||
Color::White => format!("{}", termion::color::Bg(termion::color::White)),
|
Color::White => termion_bg!(White),
|
||||||
Color::Rgb(r, g, b) => format!("{}", termion::color::Bg(termion::color::Rgb(r, g, b))),
|
Color::Rgb(r, g, b) => termion_bg_rgb!(r, g, b),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,16 @@ use widgets::Widget;
|
|||||||
use style::Color;
|
use style::Color;
|
||||||
use util::hash;
|
use util::hash;
|
||||||
|
|
||||||
|
pub struct LayoutEntry {
|
||||||
|
chunks: Vec<Rect>,
|
||||||
|
hot: bool,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Terminal {
|
pub struct Terminal {
|
||||||
stdout: RawTerminal<io::Stdout>,
|
stdout: RawTerminal<io::Stdout>,
|
||||||
layout_cache: HashMap<u64, Vec<Rect>>,
|
layout_cache: HashMap<u64, LayoutEntry>,
|
||||||
layout_queue: Vec<(u64, Vec<Rect>)>,
|
buffers: [Buffer; 2],
|
||||||
previous: Buffer,
|
current: usize,
|
||||||
current: Buffer,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Terminal {
|
impl Terminal {
|
||||||
@ -26,9 +30,8 @@ impl Terminal {
|
|||||||
Ok(Terminal {
|
Ok(Terminal {
|
||||||
stdout: stdout,
|
stdout: stdout,
|
||||||
layout_cache: HashMap::new(),
|
layout_cache: HashMap::new(),
|
||||||
layout_queue: Vec::new(),
|
buffers: [Buffer::empty(size), Buffer::empty(size)],
|
||||||
previous: Buffer::empty(size),
|
current: 0,
|
||||||
current: Buffer::empty(size),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,23 +47,30 @@ impl Terminal {
|
|||||||
|
|
||||||
pub fn compute_layout(&mut self, group: &Group, area: &Rect) -> Vec<Rect> {
|
pub fn compute_layout(&mut self, group: &Group, area: &Rect) -> Vec<Rect> {
|
||||||
let hash = hash(group, area);
|
let hash = hash(group, area);
|
||||||
let chunks = match self.layout_cache.get(&hash) {
|
let entry = self.layout_cache
|
||||||
Some(chunks) => chunks.clone(),
|
.entry(hash)
|
||||||
None => split(area, &group.direction, group.margin, &group.sizes),
|
.or_insert({
|
||||||
};
|
let chunks = split(area, &group.direction, group.margin, &group.sizes);
|
||||||
self.layout_queue.push((hash, chunks.clone()));
|
LayoutEntry {
|
||||||
chunks
|
chunks: chunks,
|
||||||
|
hot: true,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
entry.hot = true;
|
||||||
|
entry.chunks.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self) {
|
pub fn draw(&mut self) {
|
||||||
let width = self.current.area.width;
|
let width = self.buffers[self.current].area.width;
|
||||||
let mut string = String::with_capacity(self.current.content.len());
|
let mut string = String::with_capacity(self.buffers[self.current].content.len());
|
||||||
let mut fg = Color::Reset;
|
let mut fg = Color::Reset;
|
||||||
let mut bg = Color::Reset;
|
let mut bg = Color::Reset;
|
||||||
let content = self.current
|
let mut last_y = 0;
|
||||||
|
let mut last_x = 0;
|
||||||
|
let content = self.buffers[self.current]
|
||||||
.content
|
.content
|
||||||
.iter()
|
.iter()
|
||||||
.zip(self.previous.content.iter())
|
.zip(self.buffers[1 - self.current].content.iter())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(i, (c, p))| if c != p {
|
.filter_map(|(i, (c, p))| if c != p {
|
||||||
let i = i as u16;
|
let i = i as u16;
|
||||||
@ -70,33 +80,42 @@ impl Terminal {
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
});
|
});
|
||||||
|
let mut inst = 0;
|
||||||
for (x, y, cell) in content {
|
for (x, y, cell) in content {
|
||||||
string.push_str(&format!("{}", termion::cursor::Goto(x + 1, y + 1)));
|
if y != last_y || x != last_x + 1 {
|
||||||
|
string.push_str(&format!("{}", termion::cursor::Goto(x + 1, y + 1)));
|
||||||
|
inst += 1;
|
||||||
|
}
|
||||||
|
last_x = x;
|
||||||
|
last_y = y;
|
||||||
if cell.fg != fg {
|
if cell.fg != fg {
|
||||||
string.push_str(&cell.fg.fg());
|
string.push_str(&cell.fg.fg());
|
||||||
fg = cell.fg;
|
fg = cell.fg;
|
||||||
|
inst += 1;
|
||||||
}
|
}
|
||||||
if cell.bg != bg {
|
if cell.bg != bg {
|
||||||
string.push_str(&cell.bg.bg());
|
string.push_str(&cell.bg.bg());
|
||||||
bg = cell.bg;
|
bg = cell.bg;
|
||||||
|
inst += 1;
|
||||||
}
|
}
|
||||||
string.push_str(&format!("{}", cell.symbol));
|
string.push_str(&format!("{}", cell.symbol));
|
||||||
|
inst += 1;
|
||||||
}
|
}
|
||||||
string.push_str(&format!("{}{}", Color::Reset.fg(), Color::Reset.bg()));
|
string.push_str(&format!("{}{}", Color::Reset.fg(), Color::Reset.bg()));
|
||||||
info!("{}", string.len());
|
info!("{}", inst);
|
||||||
write!(self.stdout, "{}", string).unwrap();
|
write!(self.stdout, "{}", string).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render<W>(&mut self, widget: &W, area: &Rect)
|
pub fn render<W>(&mut self, widget: &W, area: &Rect)
|
||||||
where W: Widget
|
where W: Widget
|
||||||
{
|
{
|
||||||
widget.buffer(area, &mut self.current);
|
widget.buffer(area, &mut self.buffers[self.current]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, area: Rect) {
|
pub fn resize(&mut self, area: Rect) {
|
||||||
self.current.resize(area);
|
self.buffers[self.current].resize(area);
|
||||||
self.previous.resize(area);
|
self.buffers[1 - self.current].resize(area);
|
||||||
self.previous.reset();
|
self.buffers[1 - self.current].reset();
|
||||||
self.clear();
|
self.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,17 +124,18 @@ impl Terminal {
|
|||||||
// Draw to stdout
|
// Draw to stdout
|
||||||
self.draw();
|
self.draw();
|
||||||
|
|
||||||
// Update layout cache
|
// Clean layout cache
|
||||||
self.layout_cache.clear();
|
let to_remove = self.layout_cache
|
||||||
for (hash, chunks) in self.layout_queue.drain(..) {
|
.iter()
|
||||||
self.layout_cache.insert(hash, chunks);
|
.filter_map(|(h, e)| if !e.hot { Some(*h) } else { None })
|
||||||
|
.collect::<Vec<u64>>();
|
||||||
|
for h in to_remove {
|
||||||
|
self.layout_cache.remove(&h);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap buffers
|
// Swap buffers
|
||||||
for (i, c) in self.current.content.iter().enumerate() {
|
self.buffers[1 - self.current].reset();
|
||||||
self.previous.content[i] = c.clone();
|
self.current = 1 - self.current;
|
||||||
}
|
|
||||||
self.current.reset();
|
|
||||||
|
|
||||||
// Flush
|
// Flush
|
||||||
self.stdout.flush().unwrap();
|
self.stdout.flush().unwrap();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user