2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
2016-11-07 23:35:46 +00:00
|
|
|
mod util;
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
use crate::util::{
|
|
|
|
event::{Event, Events},
|
|
|
|
TabsState,
|
|
|
|
};
|
|
|
|
use std::{error::Error, io};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Constraint, Direction, Layout},
|
|
|
|
style::{Color, Style},
|
|
|
|
widgets::{Block, Borders, Tabs},
|
|
|
|
Terminal,
|
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App<'a> {
|
2018-09-23 18:59:51 +00:00
|
|
|
tabs: TabsState<'a>,
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2016-11-07 23:35:46 +00:00
|
|
|
// Terminal initialization
|
2018-09-23 18:59:51 +00:00
|
|
|
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();
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App {
|
2018-09-23 18:59:51 +00:00
|
|
|
tabs: TabsState::new(vec!["Tab0", "Tab1", "Tab2", "Tab3"]),
|
2016-11-07 23:35:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Main loop
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
|
|
|
terminal.draw(|mut f| {
|
2018-12-07 14:43:05 +00:00
|
|
|
let size = f.size();
|
2018-09-23 18:59:51 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(5)
|
|
|
|
.constraints([Constraint::Length(3), Constraint::Min(0)].as_ref())
|
2018-11-26 02:06:34 +00:00
|
|
|
.split(size);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
let block = Block::default().style(Style::default().bg(Color::White));
|
|
|
|
f.render_widget(block, size);
|
|
|
|
let tabs = Tabs::default()
|
2018-09-23 18:59:51 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).title("Tabs"))
|
|
|
|
.titles(&app.tabs.titles)
|
|
|
|
.select(app.tabs.index)
|
|
|
|
.style(Style::default().fg(Color::Cyan))
|
2019-12-15 20:38:18 +00:00
|
|
|
.highlight_style(Style::default().fg(Color::Yellow));
|
|
|
|
f.render_widget(tabs, chunks[0]);
|
|
|
|
let inner = match app.tabs.index {
|
|
|
|
0 => Block::default().title("Inner 0").borders(Borders::ALL),
|
|
|
|
1 => Block::default().title("Inner 1").borders(Borders::ALL),
|
|
|
|
2 => Block::default().title("Inner 2").borders(Borders::ALL),
|
|
|
|
3 => Block::default().title("Inner 3").borders(Borders::ALL),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
f.render_widget(inner, chunks[1]);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
|
|
|
Event::Input(input) => match input {
|
|
|
|
Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Key::Right => app.tabs.next(),
|
|
|
|
Key::Left => app.tabs.previous(),
|
|
|
|
_ => {}
|
|
|
|
},
|
2018-08-12 17:44:52 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|