Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Crossterm Performance Update #173

Merged
merged 1 commit into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ log = "0.4"
either = "1.5"
unicode-segmentation = "1.2"
unicode-width = "0.1"
termion = { version = "1.5", optional = true }
termion = { version = "1.5" , optional = true }
rustbox = { version = "0.11", optional = true }
crossterm = { version = "^0.9", optional = true }
crossterm = { version = "^0.10", optional = true }
easycurses = { version = "0.12.2", optional = true }
pancurses = { version = "0.16.1", optional = true, features = ["win32a"] }

Expand Down
7 changes: 4 additions & 3 deletions examples/crossterm_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use std::sync::mpsc;
use std::thread;
use std::time::Duration;

use crossterm::{input, AlternateScreen, InputEvent, KeyEvent};
use crossterm::{input, AlternateScreen, InputEvent, KeyEvent, RawScreen};
use structopt::StructOpt;
use tui::backend::CrosstermBackend;
use tui::Terminal;

use crate::demo::{ui, App};
use std::io::stdout;

enum Event<I> {
Input(I),
Expand All @@ -32,7 +33,7 @@ fn main() -> Result<(), failure::Error> {
stderrlog::new().quiet(!cli.log).verbosity(4).init()?;

let screen = AlternateScreen::to_alternate(true)?;
let backend = CrosstermBackend::with_alternate_screen(screen)?;
let backend = CrosstermBackend::with_alternate_screen(stdout(), screen)?;
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;

Expand All @@ -46,7 +47,7 @@ fn main() -> Result<(), failure::Error> {
for event in reader {
match event {
InputEvent::Keyboard(key) => {
if let Err(_) = tx.send(Event::Input(key)) {
if let Err(_) = tx.send(Event::Input(key.clone())) {
return;
}
if key == KeyEvent::Char('q') {
Expand Down
263 changes: 150 additions & 113 deletions src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@ use std::io;

use crate::backend::Backend;
use crate::style::{Color, Modifier};
use crate::{buffer::Cell, layout::Rect};
use crossterm::{Crossterm, ErrorKind};
use std::io::{stdout, Write};

pub struct CrosstermBackend {
use crate::{buffer::Cell, layout::Rect, style};
use crossterm::{
execute, queue, terminal, Clear, ClearType, Command, Crossterm, ErrorKind, Goto, Hide, Output,
SetAttr, SetBg, SetFg, Show,
};
use std::io::{stdout, Stdout, Write};

pub struct CrosstermBackend<W: Write> {
alternate_screen: Option<crossterm::AlternateScreen>,
crossterm: Crossterm,
stdout: W,
}

impl Default for CrosstermBackend {
fn default() -> CrosstermBackend {
impl<W> CrosstermBackend<W>
where
W: Write,
{
pub fn new(stdout: W) -> CrosstermBackend<W> {
CrosstermBackend {
crossterm: Crossterm::new(),
alternate_screen: None,
stdout,
}
}
}

impl CrosstermBackend {
pub fn new() -> CrosstermBackend {
CrosstermBackend::default()
}

pub fn with_alternate_screen(
stdout: W,
alternate_screen: crossterm::AlternateScreen,
) -> Result<CrosstermBackend, io::Error> {
) -> Result<CrosstermBackend<W>, io::Error> {
Ok(CrosstermBackend {
crossterm: Crossterm::new(),
alternate_screen: Some(alternate_screen),
stdout,
})
}

Expand All @@ -41,8 +42,8 @@ impl CrosstermBackend {
}
}

pub fn crossterm(&self) -> &crossterm::Crossterm {
&self.crossterm
pub fn crossterm(&self) -> crossterm::Crossterm {
Crossterm::new()
}
}

Expand All @@ -62,22 +63,38 @@ fn convert_error(error: ErrorKind) -> io::Error {
}
}

impl Backend for CrosstermBackend {
impl<W> Write for CrosstermBackend<W>
where
W: Write,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stdout.write(buf)
}

fn flush(&mut self) -> io::Result<()> {
self.stdout.flush()
}
}

impl<W> Backend for CrosstermBackend<W>
where
W: Write,
{
fn clear(&mut self) -> io::Result<()> {
let terminal = self.crossterm.terminal();
terminal.clear(crossterm::ClearType::All)?;
queue!(self.stdout, Clear(ClearType::All));
self.stdout.flush();
Ok(())
}

fn hide_cursor(&mut self) -> io::Result<()> {
let cursor = self.crossterm.cursor();
cursor.hide().map_err(convert_error)?;
execute!(self.stdout, Hide);
self.stdout.flush();
Ok(())
}

fn show_cursor(&mut self) -> io::Result<()> {
let cursor = self.crossterm.cursor();
cursor.show().map_err(convert_error)?;
execute!(self.stdout, Show);
self.stdout.flush();
Ok(())
}

Expand All @@ -87,128 +104,148 @@ impl Backend for CrosstermBackend {
}

fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
let cursor = crossterm::cursor();
cursor.goto(x, y).map_err(convert_error)
queue!(self.stdout, Goto(x, y));
self.stdout.flush();
Ok(())
}

fn size(&self) -> io::Result<Rect> {
let terminal = self.crossterm.terminal();
let terminal = terminal();
let (width, height) = terminal.terminal_size();
// crossterm reports max 0-based col/row index instead of count
Ok(Rect::new(0, 0, width + 1, height + 1))
Ok(Rect::new(0, 0, width, height))
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
self.stdout.flush()
}

fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
where
I: Iterator<Item = (u16, u16, &'a Cell)>,
{
let cursor = self.crossterm.cursor();
use std::fmt::Write;

let mut string = String::with_capacity(content.size_hint().0 * 3);
let mut style = style::Style::default();
let mut last_y = 0;
let mut last_x = 0;
let mut first = true;

let stdout = stdout();
let mut handle = stdout.lock();
let mut inst = 0;

for (x, y, cell) in content {
if y != last_y || x != last_x + 1 || first {
cursor.goto(x, y).map_err(convert_error)?;
first = false;
if y != last_y || x != last_x + 1 || inst == 0 {
queue!(string, Goto(x, y));
}
last_x = x;
last_y = y;
let mut s = self.crossterm.style(&cell.symbol);
if let Some(color) = cell.style.fg.into() {
s = s.with(color)
if cell.style.modifier != style.modifier {
for attr in to_crossterm_attributes(cell.style.modifier) {
queue!(string, SetAttr(attr));
}
inst += 1;
style.modifier = cell.style.modifier;
}
if let Some(color) = cell.style.bg.into() {
s = s.on(color)
if cell.style.fg != style.fg {
let color = to_crossterm_color(cell.style.fg);
queue!(string, SetFg(color));
style.fg = cell.style.fg;
inst += 1;
}
if cell.style.bg != style.bg {
let color = to_crossterm_color(cell.style.bg);
queue!(string, SetBg(color));
style.bg = cell.style.bg;
inst += 1;
}
s.object_style.attrs = cell.style.modifier.into();

write!(handle, "{}", s)?;
string.push_str(&cell.symbol);
inst += 1;
}

write!(
self.stdout,
"{}{}{}{}",
string,
SetFg(crossterm::Color::Reset),
SetBg(crossterm::Color::Reset),
SetAttr(crossterm::Attribute::Reset)
);

Crossterm::new().color().reset();

Ok(())
}
}

impl From<Color> for Option<crossterm::Color> {
fn from(color: Color) -> Option<crossterm::Color> {
match color {
Color::Reset => None,
Color::Black => Some(crossterm::Color::Black),
Color::Red => Some(crossterm::Color::DarkRed),
Color::Green => Some(crossterm::Color::DarkGreen),
Color::Yellow => Some(crossterm::Color::DarkYellow),
Color::Blue => Some(crossterm::Color::DarkBlue),
Color::Magenta => Some(crossterm::Color::DarkMagenta),
Color::Cyan => Some(crossterm::Color::DarkCyan),
Color::Gray => Some(crossterm::Color::Grey),
Color::DarkGray => Some(crossterm::Color::DarkGrey),
Color::LightRed => Some(crossterm::Color::Red),
Color::LightGreen => Some(crossterm::Color::Green),
Color::LightBlue => Some(crossterm::Color::Blue),
Color::LightYellow => Some(crossterm::Color::Yellow),
Color::LightMagenta => Some(crossterm::Color::Magenta),
Color::LightCyan => Some(crossterm::Color::Cyan),
Color::White => Some(crossterm::Color::White),
Color::Indexed(i) => Some(crossterm::Color::AnsiValue(i)),
Color::Rgb(r, g, b) => Some(crossterm::Color::Rgb { r, g, b }),
}
#[cfg(unix)]
fn to_crossterm_attributes(modifier: Modifier) -> Vec<crossterm::Attribute> {
let mut result = Vec::new();

if modifier.contains(Modifier::BOLD) {
result.push(crossterm::Attribute::Bold)
}
if modifier.contains(Modifier::DIM) {
result.push(crossterm::Attribute::Dim)
}
if modifier.contains(Modifier::ITALIC) {
result.push(crossterm::Attribute::Italic)
}
if modifier.contains(Modifier::UNDERLINED) {
result.push(crossterm::Attribute::Underlined)
}
if modifier.contains(Modifier::SLOW_BLINK) {
result.push(crossterm::Attribute::SlowBlink)
}
if modifier.contains(Modifier::RAPID_BLINK) {
result.push(crossterm::Attribute::RapidBlink)
}
if modifier.contains(Modifier::REVERSED) {
result.push(crossterm::Attribute::Reverse)
}
if modifier.contains(Modifier::HIDDEN) {
result.push(crossterm::Attribute::Hidden)
}
if modifier.contains(Modifier::CROSSED_OUT) {
result.push(crossterm::Attribute::CrossedOut)
}
}

impl From<Modifier> for Vec<crossterm::Attribute> {
#[cfg(unix)]
fn from(modifier: Modifier) -> Vec<crossterm::Attribute> {
let mut result = Vec::new();
result
}

if modifier.contains(Modifier::BOLD) {
result.push(crossterm::Attribute::Bold)
}
if modifier.contains(Modifier::DIM) {
result.push(crossterm::Attribute::Dim)
}
if modifier.contains(Modifier::ITALIC) {
result.push(crossterm::Attribute::Italic)
}
if modifier.contains(Modifier::UNDERLINED) {
result.push(crossterm::Attribute::Underlined)
}
if modifier.contains(Modifier::SLOW_BLINK) {
result.push(crossterm::Attribute::SlowBlink)
}
if modifier.contains(Modifier::RAPID_BLINK) {
result.push(crossterm::Attribute::RapidBlink)
}
if modifier.contains(Modifier::REVERSED) {
result.push(crossterm::Attribute::Reverse)
}
if modifier.contains(Modifier::HIDDEN) {
result.push(crossterm::Attribute::Hidden)
}
if modifier.contains(Modifier::CROSSED_OUT) {
result.push(crossterm::Attribute::CrossedOut)
}
#[cfg(windows)]
fn to_crossterm_attributes(modifier: Modifier) -> Vec<crossterm::Attribute> {
let mut result = Vec::new();

result
if modifier.contains(Modifier::BOLD) {
result.push(crossterm::Attribute::Bold)
}
if modifier.contains(Modifier::UNDERLINED) {
result.push(crossterm::Attribute::Underlined)
}

#[cfg(windows)]
fn from(modifier: Modifier) -> Vec<crossterm::Attribute> {
let mut result = Vec::new();

if modifier.contains(Modifier::BOLD) {
result.push(crossterm::Attribute::Bold)
}
if modifier.contains(Modifier::UNDERLINED) {
result.push(crossterm::Attribute::Underlined)
}
result
}

result
fn to_crossterm_color(color: Color) -> crossterm::Color {
match color {
Color::Reset => crossterm::Color::Reset,
Color::Black => crossterm::Color::Black,
Color::Red => crossterm::Color::DarkRed,
Color::Green => crossterm::Color::DarkGreen,
Color::Yellow => crossterm::Color::DarkYellow,
Color::Blue => crossterm::Color::DarkBlue,
Color::Magenta => crossterm::Color::DarkMagenta,
Color::Cyan => crossterm::Color::DarkCyan,
Color::Gray => crossterm::Color::Grey,
Color::DarkGray => crossterm::Color::DarkGrey,
Color::LightRed => crossterm::Color::Red,
Color::LightGreen => crossterm::Color::Green,
Color::LightBlue => crossterm::Color::Blue,
Color::LightYellow => crossterm::Color::Yellow,
Color::LightMagenta => crossterm::Color::Magenta,
Color::LightCyan => crossterm::Color::Cyan,
Color::White => crossterm::Color::White,
Color::Indexed(i) => crossterm::Color::AnsiValue(i),
Color::Rgb(r, g, b) => crossterm::Color::Rgb { r, g, b },
}
}