docs(examples): use ratatui::crossterm in examples (#1315)

This commit is contained in:
Josh McKinney 2024-08-10 17:43:13 -07:00 committed by GitHub
parent 730dfd4940
commit d5477b50d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 65 additions and 43 deletions

View file

@ -41,9 +41,11 @@ use octocrab::{
OctocrabBuilder, Page,
};
use ratatui::{
buffer::Buffer,
crossterm::event::{Event, EventStream, KeyCode},
layout::Offset,
prelude::{Buffer, Constraint, Line, Modifier, Rect, Stylize},
layout::{Constraint, Offset, Rect},
style::{Modifier, Stylize},
text::Line,
widgets::{
Block, BorderType, HighlightSpacing, Row, StatefulWidget, Table, TableState, Widget,
},
@ -262,14 +264,18 @@ impl From<&PullRequest> for Row<'_> {
mod terminal {
use std::io;
use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
use ratatui::{
backend::CrosstermBackend,
crossterm::{
execute,
terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
},
},
};
use ratatui::prelude::{CrosstermBackend, Terminal as RatatuiTerminal};
/// A type alias for the terminal type used in this example.
pub type Terminal = RatatuiTerminal<CrosstermBackend<io::Stdout>>;
pub type Terminal = ratatui::Terminal<CrosstermBackend<io::Stdout>>;
pub fn init() -> io::Result<Terminal> {
set_panic_hook();

View file

@ -18,9 +18,11 @@ use std::iter::zip;
use color_eyre::Result;
use ratatui::{
crossterm::event::{self, Event, KeyCode},
prelude::{Color, Constraint, Direction, Frame, Layout, Line, Style},
style::Stylize,
layout::{Constraint, Direction, Layout},
style::{Color, Style, Stylize},
text::Line,
widgets::{Bar, BarChart, BarGroup, Block},
Frame,
};
use self::terminal::Terminal;

View file

@ -18,7 +18,8 @@ use rand::{thread_rng, Rng};
use ratatui::{
crossterm::event::{self, Event, KeyCode},
layout::{Constraint, Direction, Layout},
prelude::{Color, Line, Style, Stylize},
style::{Color, Style, Stylize},
text::Line,
widgets::{Bar, BarChart, BarGroup, Block},
};

View file

@ -136,7 +136,7 @@ fn run_app<B: Backend>(
terminal.draw(|f| ui(f, &app))?;
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if key.code == KeyCode::Char('q') {
return Ok(());

View file

@ -54,7 +54,7 @@ fn run_app<B: Backend>(
terminal.draw(|f| ui::draw(f, &mut app))?;
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press {
match key.code {

View file

@ -25,14 +25,22 @@ use color_eyre::{
config::{EyreHook, HookBuilder, PanicHook},
eyre, Result,
};
use crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use itertools::Itertools;
use ratatui::{prelude::*, widgets::WidgetRef};
use ratatui::{
backend::CrosstermBackend,
buffer::Buffer,
crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
layout::Rect,
style::Stylize,
text::{Line, Text},
widgets::WidgetRef,
Terminal,
};
fn main() -> Result<()> {
init_error_handling()?;

View file

@ -25,6 +25,10 @@ use std::{
use rand::distributions::{Distribution, Uniform};
use ratatui::{
backend::{Backend, CrosstermBackend},
crossterm::{
event,
terminal::{disable_raw_mode, enable_raw_mode},
},
layout::{Alignment, Constraint, Layout, Rect},
style::{Color, Modifier, Style},
symbols,
@ -39,7 +43,7 @@ type DownloadId = usize;
type WorkerId = usize;
enum Event {
Input(crossterm::event::KeyEvent),
Input(event::KeyEvent),
Tick,
Resize,
DownloadUpdate(WorkerId, DownloadId, f64),
@ -87,7 +91,7 @@ struct Worker {
}
fn main() -> Result<(), Box<dyn Error>> {
crossterm::terminal::enable_raw_mode()?;
enable_raw_mode()?;
let stdout = io::stdout();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::with_options(
@ -109,7 +113,7 @@ fn main() -> Result<(), Box<dyn Error>> {
run_app(&mut terminal, workers, downloads, rx)?;
crossterm::terminal::disable_raw_mode()?;
disable_raw_mode()?;
terminal.clear()?;
Ok(())
@ -122,10 +126,10 @@ fn input_handling(tx: mpsc::Sender<Event>) {
loop {
// poll for tick rate duration, if no events, sent tick event.
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout).unwrap() {
match crossterm::event::read().unwrap() {
crossterm::event::Event::Key(key) => tx.send(Event::Input(key)).unwrap(),
crossterm::event::Event::Resize(_, _) => tx.send(Event::Resize).unwrap(),
if event::poll(timeout).unwrap() {
match event::read().unwrap() {
event::Event::Key(key) => tx.send(Event::Input(key)).unwrap(),
event::Event::Resize(_, _) => tx.send(Event::Resize).unwrap(),
_ => {}
};
}
@ -193,7 +197,7 @@ fn run_app<B: Backend>(
match rx.recv()? {
Event::Input(event) => {
if event.code == crossterm::event::KeyCode::Char('q') {
if event.code == event::KeyCode::Char('q') {
break;
}
}

View file

@ -15,11 +15,10 @@
use std::{error::Error, io};
use crossterm::event::KeyEvent;
use ratatui::{
backend::Backend,
buffer::Buffer,
crossterm::event::{self, Event, KeyCode, KeyEventKind},
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
layout::{Constraint, Layout, Rect},
style::{
palette::tailwind::{BLUE, GREEN, SLATE},

View file

@ -35,6 +35,7 @@ use ratatui::{
backend::{Backend, CrosstermBackend},
crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
},
text::Line,
@ -79,7 +80,7 @@ fn main() -> Result<()> {
/// Initializes the terminal.
fn init_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>> {
crossterm::execute!(io::stdout(), EnterAlternateScreen)?;
execute!(io::stdout(), EnterAlternateScreen)?;
enable_raw_mode()?;
let backend = CrosstermBackend::new(io::stdout());
@ -93,7 +94,7 @@ fn init_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>> {
/// Resets the terminal.
fn reset_terminal() -> Result<()> {
disable_raw_mode()?;
crossterm::execute!(io::stdout(), LeaveAlternateScreen)?;
execute!(io::stdout(), LeaveAlternateScreen)?;
Ok(())
}

View file

@ -18,10 +18,9 @@ use std::{
time::{Duration, Instant},
};
use crossterm::event::KeyEventKind;
use ratatui::{
buffer::Buffer,
crossterm::event::{self, Event, KeyCode},
crossterm::event::{self, Event, KeyCode, KeyEventKind},
layout::{Constraint, Layout, Rect},
style::{Color, Stylize},
text::{Line, Masked, Span},
@ -171,11 +170,13 @@ mod common {
config::{EyreHook, HookBuilder, PanicHook},
eyre,
};
use crossterm::ExecutableCommand;
use ratatui::{
backend::CrosstermBackend,
crossterm::terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
crossterm::{
terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
},
ExecutableCommand,
},
Terminal,
};

View file

@ -83,7 +83,7 @@ fn run_app<B: Backend>(
terminal.draw(|f| ui(f, &mut app))?;
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(()),

View file

@ -131,7 +131,7 @@ fn run_app<B: Backend>(
terminal.draw(|f| ui(f, &app))?;
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if crossterm::event::poll(timeout)? {
if event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
if key.code == KeyCode::Char('q') {
return Ok(());

View file

@ -34,13 +34,13 @@ use color_eyre::{
eyre::{self, Context},
Result,
};
use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{
backend::{Backend, CrosstermBackend},
crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
},
widgets::{Block, Paragraph},
Terminal,
};