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

Update 0.9.4 #153

Merged
merged 6 commits into from
May 17, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cargo.lock
*.log
*.rs.rustfmt
.gdb_history
.idea/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this would live in your git user config and not the repository. Not a blocker IMO

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ appveyor = { repository = "fdehau/tui-rs" }
[features]
default = ["termion"]
curses = ["easycurses", "pancurses"]
crossterm_backend = ["crossterm"]

[dependencies]
bitflags = "1.0"
Expand All @@ -30,7 +31,7 @@ unicode-segmentation = "1.2"
unicode-width = "0.1"
termion = { version = "1.5", optional = true }
rustbox = { version = "0.11", optional = true }
crossterm = { version = "0.6", optional = true }
crossterm = { version = "0.9.4", optional = true }
easycurses = { version = "0.12.2", optional = true }
pancurses = { version = "0.16.1", optional = true, features = ["win32a"] }

Expand Down
4 changes: 1 addition & 3 deletions examples/crossterm_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ fn main() -> Result<(), failure::Error> {
let cli = Cli::from_args();
stderrlog::new().quiet(!cli.log).verbosity(4).init()?;

let screen = crossterm::Screen::default();
let alternate_screen = screen.enable_alternate_modes(true)?;
let backend = CrosstermBackend::with_alternate_screen(alternate_screen)?;
let backend = CrosstermBackend::new();
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;

Expand Down
46 changes: 12 additions & 34 deletions src/backend/crossterm.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
use std::io;

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

pub struct CrosstermBackend {
screen: Option<crossterm::Screen>,
crossterm: crossterm::Crossterm,
// Need to keep the AlternateScreen around even when not using it directly,
// see https://github.com/TimonPost/crossterm/issues/88
alternate_screen: Option<crossterm::AlternateScreen>,
crossterm: Crossterm,
}

impl Default for CrosstermBackend {
fn default() -> CrosstermBackend {
let screen = crossterm::Screen::default();
let crossterm = crossterm::Crossterm::from_screen(&screen);
CrosstermBackend {
screen: Some(screen),
crossterm,
crossterm: Crossterm::new(),
alternate_screen: None,
}
}
Expand All @@ -31,33 +25,15 @@ impl CrosstermBackend {
CrosstermBackend::default()
}

pub fn with_screen(screen: crossterm::Screen) -> CrosstermBackend {
let crossterm = crossterm::Crossterm::from_screen(&screen);
CrosstermBackend {
screen: Some(screen),
crossterm,
alternate_screen: None,
}
}

pub fn with_alternate_screen(
alternate_screen: crossterm::AlternateScreen,
) -> Result<CrosstermBackend, io::Error> {
let crossterm = crossterm::Crossterm::from_screen(&alternate_screen.screen);
Ok(CrosstermBackend {
screen: None,
crossterm,
crossterm: Crossterm::new(),
alternate_screen: Some(alternate_screen),
})
}

pub fn screen(&self) -> Option<&crossterm::Screen> {
match &self.screen {
Some(screen) => Some(&screen),
None => None,
}
}

pub fn alternate_screen(&self) -> Option<&crossterm::AlternateScreen> {
match &self.alternate_screen {
Some(alt_screen) => Some(&alt_screen),
Expand Down Expand Up @@ -89,9 +65,7 @@ fn convert_error(error: ErrorKind) -> io::Error {
impl Backend for CrosstermBackend {
fn clear(&mut self) -> io::Result<()> {
let terminal = self.crossterm.terminal();
terminal
.clear(crossterm::ClearType::All)
.map_err(convert_error)?;
terminal.clear(crossterm::ClearType::All)?;
Ok(())
}

Expand Down Expand Up @@ -135,6 +109,10 @@ impl Backend for CrosstermBackend {
let mut last_y = 0;
let mut last_x = 0;
let mut first = true;

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

for (x, y, cell) in content {
if y != last_y || x != last_x + 1 || first {
cursor.goto(x, y).map_err(convert_error)?;
Expand All @@ -151,7 +129,7 @@ impl Backend for CrosstermBackend {
}
s.object_style.attrs = cell.style.modifier.into();

self.crossterm.paint(s).map_err(convert_error)?;
write!(handle, "{}", s)?;
}
Ok(())
}
Expand Down