feat: bump crossterm to 0.14

pull/212/head
Timon 5 years ago committed by Florian Dehau
parent 7cc4189eb0
commit 60b99cfc66

@ -30,7 +30,7 @@ unicode-segmentation = "1.2"
unicode-width = "0.1"
termion = { version = "1.5" , optional = true }
rustbox = { version = "0.11", optional = true }
crossterm = { version = "0.13", optional = true }
crossterm = { version = "0.14", optional = true }
easycurses = { version = "0.12.2", optional = true }
pancurses = { version = "0.16.1", optional = true, features = ["win32a"] }

@ -3,20 +3,24 @@ mod demo;
#[allow(dead_code)]
mod util;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use std::{
io::{stdout, Write},
sync::mpsc,
thread,
time::Duration,
};
use crossterm::{
input::{input, InputEvent, KeyEvent},
screen::AlternateScreen,
event::{self, Event as CEvent, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
};
use structopt::StructOpt;
use tui::backend::CrosstermBackend;
use tui::Terminal;
use tui::{backend::CrosstermBackend, Terminal};
use crate::demo::{ui, App};
use std::io::stdout;
use crossterm::terminal::LeaveAlternateScreen;
enum Event<I> {
Input(I),
@ -35,39 +39,29 @@ fn main() -> Result<(), failure::Error> {
let cli = Cli::from_args();
stderrlog::new().quiet(!cli.log).verbosity(4).init()?;
let screen = AlternateScreen::to_alternate(true)?;
let backend = CrosstermBackend::with_alternate_screen(stdout(), screen)?;
enable_raw_mode()?;
let mut stdout = stdout();
execute!(stdout, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
// Setup input handling
let (tx, rx) = mpsc::channel();
{
let tx = tx.clone();
thread::spawn(move || {
let input = input();
let mut reader = input.read_sync();
loop {
match reader.next() {
Some(InputEvent::Keyboard(key)) => {
if let Err(_) = tx.send(Event::Input(key.clone())) {
return;
}
if key == KeyEvent::Char('q') {
return;
}
}
_ => {}
}
}
});
}
thread::spawn(move || {
let tx = tx.clone();
loop {
// poll for tick rate duration, if no events, sent tick event.
if event::poll(Duration::from_millis(cli.tick_rate)).unwrap() {
if let CEvent::Key(key) = event::read().unwrap() {
tx.send(Event::Input(key)).unwrap();
}
}
tx.send(Event::Tick).unwrap();
thread::sleep(Duration::from_millis(cli.tick_rate));
}
});
@ -78,12 +72,18 @@ fn main() -> Result<(), failure::Error> {
loop {
ui::draw(&mut terminal, &app)?;
match rx.recv()? {
Event::Input(event) => match event {
KeyEvent::Char(c) => app.on_key(c),
KeyEvent::Left => app.on_left(),
KeyEvent::Up => app.on_up(),
KeyEvent::Right => app.on_right(),
KeyEvent::Down => app.on_down(),
Event::Input(event) => match event.code {
KeyCode::Char('q') => {
disable_raw_mode()?;
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
terminal.show_cursor()?;
break;
}
KeyCode::Char(c) => app.on_key(c),
KeyCode::Left => app.on_left(),
KeyCode::Up => app.on_up(),
KeyCode::Right => app.on_right(),
KeyCode::Down => app.on_down(),
_ => {}
},
Event::Tick => {

@ -6,13 +6,11 @@ use std::{
use crossterm::{
cursor::{Hide, MoveTo, Show},
execute, queue,
screen::AlternateScreen,
style::{
Attribute as CAttribute, Color as CColor, SetAttribute, SetBackgroundColor,
Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
SetForegroundColor,
},
terminal::{self, Clear, ClearType},
Output,
};
use crate::backend::Backend;
@ -20,36 +18,15 @@ use crate::style::{Color, Modifier};
use crate::{buffer::Cell, layout::Rect, style};
pub struct CrosstermBackend<W: Write> {
alternate_screen: Option<AlternateScreen>,
stdout: W,
buffer: W,
}
impl<W> CrosstermBackend<W>
where
W: Write,
{
pub fn new(stdout: W) -> CrosstermBackend<W> {
CrosstermBackend {
alternate_screen: None,
stdout,
}
}
pub fn with_alternate_screen(
stdout: W,
alternate_screen: AlternateScreen,
) -> Result<CrosstermBackend<W>, io::Error> {
Ok(CrosstermBackend {
alternate_screen: Some(alternate_screen),
stdout,
})
}
pub fn alternate_screen(&self) -> Option<&AlternateScreen> {
match &self.alternate_screen {
Some(alt_screen) => Some(&alt_screen),
None => None,
}
pub fn new(buffer: W) -> CrosstermBackend<W> {
CrosstermBackend { buffer }
}
}
@ -58,11 +35,11 @@ where
W: Write,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stdout.write(buf)
self.buffer.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.stdout.flush()
self.buffer.flush()
}
}
@ -115,8 +92,8 @@ where
}
map_error(queue!(
self.stdout,
Output(string),
self.buffer,
Print(string.clone()),
SetForegroundColor(CColor::Reset),
SetBackgroundColor(CColor::Reset),
SetAttribute(CAttribute::Reset)
@ -124,11 +101,11 @@ where
}
fn hide_cursor(&mut self) -> io::Result<()> {
map_error(execute!(self.stdout, Hide))
map_error(execute!(self.buffer, Hide))
}
fn show_cursor(&mut self) -> io::Result<()> {
map_error(execute!(self.stdout, Show))
map_error(execute!(self.buffer, Show))
}
fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
@ -137,11 +114,11 @@ where
}
fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> {
map_error(execute!(self.stdout, MoveTo(x, y)))
map_error(execute!(self.buffer, MoveTo(x, y)))
}
fn clear(&mut self) -> io::Result<()> {
map_error(execute!(self.stdout, Clear(ClearType::All)))
map_error(execute!(self.buffer, Clear(ClearType::All)))
}
fn size(&self) -> io::Result<Rect> {
@ -152,7 +129,7 @@ where
}
fn flush(&mut self) -> io::Result<()> {
<CrosstermBackend<W> as Write>::flush(self)
self.buffer.flush()
}
}

Loading…
Cancel
Save