ratatui/examples/tabs.rs

115 lines
3 KiB
Rust
Raw Normal View History

2016-11-07 23:35:46 +00:00
extern crate tui;
extern crate termion;
mod util;
use util::*;
use std::io;
use termion::event;
use termion::input::TermRead;
use tui::Terminal;
use tui::backend::TermionBackend;
2016-11-07 23:35:46 +00:00
use tui::widgets::{Widget, Block, border, Tabs};
2017-05-08 19:34:41 +00:00
use tui::layout::{Group, Direction, Size, Rect};
2016-11-07 23:35:46 +00:00
use tui::style::{Style, Color};
struct App<'a> {
2017-05-08 19:34:41 +00:00
size: Rect,
2016-11-07 23:35:46 +00:00
tabs: MyTabs<'a>,
}
fn main() {
// Terminal initialization
let backend = TermionBackend::new().unwrap();
let mut terminal = Terminal::new(backend).unwrap();
// App
let mut app = App {
2017-05-08 19:34:41 +00:00
size: Rect::default(),
2016-11-07 23:35:46 +00:00
tabs: MyTabs {
titles: vec!["Tab0", "Tab1", "Tab2", "Tab3"],
selection: 0,
},
};
// First draw call
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
2017-05-08 19:34:41 +00:00
app.size = terminal.size().unwrap();
2016-11-07 23:35:46 +00:00
draw(&mut terminal, &mut app);
// Main loop
let stdin = io::stdin();
for c in stdin.keys() {
2017-05-08 19:34:41 +00:00
let size = terminal.size().unwrap();
if size != app.size {
terminal.resize(size).unwrap();
app.size = size;
}
2016-11-07 23:35:46 +00:00
let evt = c.unwrap();
match evt {
event::Key::Char('q') => {
break;
}
event::Key::Right => app.tabs.next(),
event::Key::Left => app.tabs.previous(),
_ => {}
}
draw(&mut terminal, &mut app);
}
terminal.show_cursor().unwrap();
}
fn draw(t: &mut Terminal<TermionBackend>, app: &mut App) {
Block::default()
.style(Style::default().bg(Color::White))
2017-05-08 19:34:41 +00:00
.render(t, &app.size);
2016-11-07 23:35:46 +00:00
Group::default()
.direction(Direction::Vertical)
.margin(5)
.sizes(&[Size::Fixed(3), Size::Min(0)])
2017-05-08 19:34:41 +00:00
.render(t, &app.size, |t, chunks| {
2016-11-07 23:35:46 +00:00
Tabs::default()
.block(Block::default().borders(border::ALL).title("Tabs"))
.titles(&app.tabs.titles)
.select(app.tabs.selection)
.style(Style::default().fg(Color::Cyan))
.highlight_style(Style::default().fg(Color::Yellow))
.render(t, &chunks[0]);
match app.tabs.selection {
0 => {
2017-05-21 09:13:24 +00:00
Block::default()
.title("Inner 0")
.borders(border::ALL)
.render(t, &chunks[1]);
2016-11-07 23:35:46 +00:00
}
1 => {
2017-05-21 09:13:24 +00:00
Block::default()
.title("Inner 1")
.borders(border::ALL)
.render(t, &chunks[1]);
2016-11-07 23:35:46 +00:00
}
2 => {
2017-05-21 09:13:24 +00:00
Block::default()
.title("Inner 2")
.borders(border::ALL)
.render(t, &chunks[1]);
2016-11-07 23:35:46 +00:00
}
3 => {
2017-05-21 09:13:24 +00:00
Block::default()
.title("Inner 3")
.borders(border::ALL)
.render(t, &chunks[1]);
2016-11-07 23:35:46 +00:00
}
_ => {}
}
});
t.draw().unwrap();
}