mirror of
https://github.com/fdehau/tui-rs.git
synced 2024-11-17 03:25:41 +00:00
8cdfc883b9
It basically never makes sense to render without syncing the size. Without resizing, if shrinking, we get artefacts. If growing, we may get panics (before this change the Rustbox sample (the only one which didn't handle resizing on its own) panicked because the widget would get an updated size, while the terminal would not).
99 lines
2.9 KiB
Rust
99 lines
2.9 KiB
Rust
extern crate failure;
|
|
extern crate termion;
|
|
extern crate tui;
|
|
|
|
#[allow(dead_code)]
|
|
mod util;
|
|
|
|
use std::io;
|
|
|
|
use termion::event::Key;
|
|
use termion::input::MouseTerminal;
|
|
use termion::raw::IntoRawMode;
|
|
use termion::screen::AlternateScreen;
|
|
use tui::backend::TermionBackend;
|
|
use tui::layout::{Constraint, Direction, Layout};
|
|
use tui::style::{Color, Style};
|
|
use tui::widgets::{Block, Borders, Tabs, Widget};
|
|
use tui::Terminal;
|
|
|
|
use util::event::{Event, Events};
|
|
use util::TabsState;
|
|
|
|
struct App<'a> {
|
|
tabs: TabsState<'a>,
|
|
}
|
|
|
|
fn main() -> Result<(), failure::Error> {
|
|
// Terminal initialization
|
|
let stdout = io::stdout().into_raw_mode()?;
|
|
let stdout = MouseTerminal::from(stdout);
|
|
let stdout = AlternateScreen::from(stdout);
|
|
let backend = TermionBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
terminal.hide_cursor()?;
|
|
|
|
let events = Events::new();
|
|
|
|
// App
|
|
let mut app = App {
|
|
tabs: TabsState::new(vec!["Tab0", "Tab1", "Tab2", "Tab3"]),
|
|
};
|
|
|
|
// Main loop
|
|
loop {
|
|
let size = terminal.size()?;
|
|
|
|
terminal.draw(|mut f| {
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.margin(5)
|
|
.constraints([Constraint::Length(3), Constraint::Min(0)].as_ref())
|
|
.split(size);
|
|
|
|
Block::default()
|
|
.style(Style::default().bg(Color::White))
|
|
.render(&mut f, size);
|
|
Tabs::default()
|
|
.block(Block::default().borders(Borders::ALL).title("Tabs"))
|
|
.titles(&app.tabs.titles)
|
|
.select(app.tabs.index)
|
|
.style(Style::default().fg(Color::Cyan))
|
|
.highlight_style(Style::default().fg(Color::Yellow))
|
|
.render(&mut f, chunks[0]);
|
|
match app.tabs.index {
|
|
0 => Block::default()
|
|
.title("Inner 0")
|
|
.borders(Borders::ALL)
|
|
.render(&mut f, chunks[1]),
|
|
1 => Block::default()
|
|
.title("Inner 1")
|
|
.borders(Borders::ALL)
|
|
.render(&mut f, chunks[1]),
|
|
2 => Block::default()
|
|
.title("Inner 2")
|
|
.borders(Borders::ALL)
|
|
.render(&mut f, chunks[1]),
|
|
3 => Block::default()
|
|
.title("Inner 3")
|
|
.borders(Borders::ALL)
|
|
.render(&mut f, chunks[1]),
|
|
_ => {}
|
|
}
|
|
})?;
|
|
|
|
match events.next()? {
|
|
Event::Input(input) => match input {
|
|
Key::Char('q') => {
|
|
break;
|
|
}
|
|
Key::Right => app.tabs.next(),
|
|
Key::Left => app.tabs.previous(),
|
|
_ => {}
|
|
},
|
|
_ => {}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|