Skip to content

Commit

Permalink
rework examples/demo to not use termion
Browse files Browse the repository at this point in the history
  • Loading branch information
gin66 committed Aug 19, 2023
1 parent 328c5f1 commit e718d4d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 66 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.9.5:
- rework examples/demo to not use termion

0.9.4:
- fix breaking change in 0.9.3 as reported by issue #43

Expand Down
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tui-logger"
version = "0.9.4"
version = "0.9.5"
authors = ["Jochen Kiemes <jochen@kiemes.de>"]
edition = "2021"
license = "MIT"
Expand All @@ -25,8 +25,11 @@ fxhash = "0.2"
parking_lot = "0.12"
slog = { version = "2.7.0", optional = true }

# Optional features for demo. Unfortunately cannot move to dep-dependencies
termion = {version = "1.5", optional = true }
crossterm = {version = "0.27", optional = true }

[dev-dependencies]
termion = "1.5"
env_logger = "0.10.0"

[features]
Expand All @@ -37,7 +40,7 @@ ratatui-support = ["ratatui"]
tracing-support = ["tracing", "tracing-subscriber"]

# This features are exclusively used by examples/demo.rs
examples-tui-termion = ["tui/termion"]
examples-tui-crossterm = ["tui/crossterm"]
examples-ratatui-termion = ["ratatui/termion"]
examples-ratatui-crossterm = ["ratatui/crossterm"]
examples-tui-termion = ["termion", "tui/termion"]
examples-tui-crossterm = ["crossterm", "tui/crossterm"]
examples-ratatui-termion = ["termion", "ratatui/termion"]
examples-ratatui-crossterm = ["crossterm", "ratatui/crossterm"]
158 changes: 98 additions & 60 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@ use std::{thread, time};
use log::LevelFilter;
use log::*;

#[cfg(feature = "crossterm")]
use crossterm::event::KeyCode as Key;
#[cfg(feature = "crossterm")]
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};

#[cfg(feature = "termion")]
use termion::{
event::{Event, Key},
input::{MouseTerminal, TermRead},
raw::IntoRawMode,
screen::AlternateScreen,
};

#[cfg(feature = "examples-ratatui-crossterm")]
use ratatui::backend::CrosstermBackend as SelectedBackend;
#[cfg(feature = "examples-ratatui-termion")]
use ratatui::backend::TermionBackend as SelectedBackend;
#[cfg(feature = "ratatui-support")]
use ratatui::prelude::*;
#[cfg(feature = "ratatui-support")]
use ratatui::widgets::*;
#[cfg(feature = "examples-ratatui-termion")]
use ratatui::backend::TermionBackend as SelectedBackend;
#[cfg(feature = "examples-ratatui-crossterm")]
use ratatui::backend::CrosstermBackend as SelectedBackend;

#[cfg(not(feature = "ratatui-support"))]
use tui::backend::Backend;
#[cfg(feature = "examples-tui-termion")]
use tui::backend::TermionBackend as SelectedBackend;
#[cfg(feature = "examples-tui-crossterm")]
use tui::backend::CrosstermBackend as SelectedBackend;
#[cfg(feature = "examples-tui-termion")]
use tui::backend::TermionBackend as SelectedBackend;

#[cfg(not(feature = "ratatui-support"))]
use tui::layout::{Constraint, Direction, Layout, Rect};
Expand All @@ -49,7 +60,7 @@ struct App {

#[derive(Debug)]
enum AppEvent {
Termion(termion::event::Event),
UiEvent(Event),
LoopCnt(Option<u16>),
}

Expand All @@ -74,13 +85,23 @@ fn main() -> std::result::Result<(), std::io::Error> {
set_default_level(LevelFilter::Trace);
info!(target:"DEMO", "Start demo");

#[cfg(feature = "termion")]
let backend = {
let stdout = io::stdout().into_raw_mode().unwrap();
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
SelectedBackend::new(stdout)
};

#[cfg(feature = "crossterm")]
let backend = {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
SelectedBackend::new(stdout)
};

let mut terminal = Terminal::new(backend).unwrap();
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
Expand All @@ -90,11 +111,16 @@ fn main() -> std::result::Result<(), std::io::Error> {
let tx_event = tx.clone();
thread::spawn({
let f = {
let stdin = io::stdin();
move || {
for c in stdin.events() {
#[cfg(feature = "termion")]
for c in io::stdin().events() {
trace!(target:"DEMO", "Stdin event received {:?}", c);
tx_event.send(AppEvent::Termion(c.unwrap())).unwrap();
tx_event.send(AppEvent::UiEvent(c.unwrap())).unwrap();
}
#[cfg(feature = "crossterm")]
while let Ok(c) = event::read() {
trace!(target:"DEMO", "Stdin event received {:?}", c);
tx_event.send(AppEvent::UiEvent(c)).unwrap();
}
}
};
Expand All @@ -119,58 +145,60 @@ fn main() -> std::result::Result<(), std::io::Error> {
None
};
match evt {
AppEvent::Termion(evt) => {
AppEvent::UiEvent(evt) => {
debug!(target: "New event", "{:?}",evt);
use termion::event::{Event, Key};
match evt {
Event::Key(Key::Char('q')) => break,
Event::Key(Key::Char('\t')) => {
// tab
let sel = app.selected_tab;
let sel_tab = if sel + 1 < app.tabs.len() { sel + 1 } else { 0 };
app.selected_tab = sel_tab;
}
evt => {
if let Some(state) = opt_state {
match evt {
Event::Key(Key::Char(' ')) => {
state.transition(&TuiWidgetEvent::SpaceKey);
}
Event::Key(Key::Esc) => {
state.transition(&TuiWidgetEvent::EscapeKey);
}
Event::Key(Key::PageUp) => {
state.transition(&TuiWidgetEvent::PrevPageKey);
}
Event::Key(Key::PageDown) => {
state.transition(&TuiWidgetEvent::NextPageKey);
}
Event::Key(Key::Up) => {
state.transition(&TuiWidgetEvent::UpKey);
}
Event::Key(Key::Down) => {
state.transition(&TuiWidgetEvent::DownKey);
}
Event::Key(Key::Left) => {
state.transition(&TuiWidgetEvent::LeftKey);
}
Event::Key(Key::Right) => {
state.transition(&TuiWidgetEvent::RightKey);
}
Event::Key(Key::Char('+')) => {
state.transition(&TuiWidgetEvent::PlusKey);
}
Event::Key(Key::Char('-')) => {
state.transition(&TuiWidgetEvent::MinusKey);
}
Event::Key(Key::Char('h')) => {
state.transition(&TuiWidgetEvent::HideKey);
}
Event::Key(Key::Char('f')) => {
state.transition(&TuiWidgetEvent::FocusKey);
}
_ => (),
if let Event::Key(key) = evt {
if let Some(state) = opt_state {
#[cfg(feature = "crossterm")]
let code = key.code;
#[cfg(feature = "termion")]
let code = key;

match code {
Key::Char('q') => break,
Key::Char('\t') => {
// tab
let sel = app.selected_tab;
let sel_tab = if sel + 1 < app.tabs.len() { sel + 1 } else { 0 };
app.selected_tab = sel_tab;
}
Key::Char(' ') => {
state.transition(&TuiWidgetEvent::SpaceKey);
}
Key::Esc => {
state.transition(&TuiWidgetEvent::EscapeKey);
}
Key::PageUp => {
state.transition(&TuiWidgetEvent::PrevPageKey);
}
Key::PageDown => {
state.transition(&TuiWidgetEvent::NextPageKey);
}
Key::Up => {
state.transition(&TuiWidgetEvent::UpKey);
}
Key::Down => {
state.transition(&TuiWidgetEvent::DownKey);
}
Key::Left => {
state.transition(&TuiWidgetEvent::LeftKey);
}
Key::Right => {
state.transition(&TuiWidgetEvent::RightKey);
}
Key::Char('+') => {
state.transition(&TuiWidgetEvent::PlusKey);
}
Key::Char('-') => {
state.transition(&TuiWidgetEvent::MinusKey);
}
Key::Char('h') => {
state.transition(&TuiWidgetEvent::HideKey);
}
Key::Char('f') => {
state.transition(&TuiWidgetEvent::FocusKey);
}
_ => (),
}
}
}
Expand All @@ -185,6 +213,16 @@ fn main() -> std::result::Result<(), std::io::Error> {
draw_frame(&mut f, size, &mut app);
})?;
}
#[cfg(feature = "crossterm")]
{
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
}
terminal.show_cursor().unwrap();
terminal.clear().unwrap();

Expand Down

0 comments on commit e718d4d

Please sign in to comment.