tui-rs/examples/tabs.rs

114 lines
3.0 KiB
Rust
Raw Normal View History

2016-11-07 23:35:46 +00:00
extern crate termion;
2017-10-30 21:28:37 +00:00
extern crate tui;
2016-11-07 23:35:46 +00:00
mod util;
use util::*;
use std::io;
use termion::event;
use termion::input::TermRead;
use tui::Terminal;
2017-09-03 13:34:05 +00:00
use tui::backend::MouseBackend;
2017-12-26 16:09:04 +00:00
use tui::widgets::{Block, Borders, Tabs, Widget};
2017-10-30 21:28:37 +00:00
use tui::layout::{Direction, Group, Rect, Size};
use tui::style::{Color, Style};
2016-11-07 23:35:46 +00:00
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
2017-09-03 13:34:05 +00:00
let backend = MouseBackend::new().unwrap();
2016-11-07 23:35:46 +00:00
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();
}
2017-09-03 13:34:05 +00:00
fn draw(t: &mut Terminal<MouseBackend>, app: &mut App) {
2016-11-07 23:35:46 +00:00
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(Borders::ALL).title("Tabs"))
2016-11-07 23:35:46 +00:00
.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(Borders::ALL)
2017-05-21 09:13:24 +00:00
.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(Borders::ALL)
2017-05-21 09:13:24 +00:00
.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(Borders::ALL)
2017-05-21 09:13:24 +00:00
.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(Borders::ALL)
2017-05-21 09:13:24 +00:00
.render(t, &chunks[1]);
2016-11-07 23:35:46 +00:00
}
_ => {}
}
});
t.draw().unwrap();
}