2023-06-12 05:07:15 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
2023-04-13 20:21:35 +00:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
2021-11-01 21:27:46 +00:00
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
2020-03-13 01:02:51 +00:00
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{prelude::*, widgets::*};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App<'a> {
|
2021-11-11 14:56:37 +00:00
|
|
|
pub titles: Vec<&'a str>,
|
|
|
|
pub index: usize,
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
impl<'a> App<'a> {
|
|
|
|
fn new() -> App<'a> {
|
|
|
|
App {
|
2021-11-11 14:56:37 +00:00
|
|
|
titles: vec!["Tab0", "Tab1", "Tab2", "Tab3"],
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next(&mut self) {
|
|
|
|
self.index = (self.index + 1) % self.titles.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn previous(&mut self) {
|
|
|
|
if self.index > 0 {
|
|
|
|
self.index -= 1;
|
|
|
|
} else {
|
|
|
|
self.index = self.titles.len() - 1;
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
|
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Event::Key(key) = event::read()? {
|
2023-04-13 20:21:35 +00:00
|
|
|
if key.kind == KeyEventKind::Press {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
2023-12-15 03:11:48 +00:00
|
|
|
KeyCode::Right | KeyCode::Char('l') => app.next(),
|
|
|
|
KeyCode::Left | KeyCode::Char('h') => app.previous(),
|
2023-04-13 20:21:35 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2020-05-16 22:51:57 +00:00
|
|
|
}
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let area = f.size();
|
|
|
|
let vertical = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]);
|
|
|
|
let [tabs_area, inner_area] = area.split(&vertical);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2023-07-01 09:14:16 +00:00
|
|
|
let block = Block::default().on_white().black();
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(block, area);
|
2024-01-11 01:16:44 +00:00
|
|
|
let tabs = app
|
2021-11-01 21:27:46 +00:00
|
|
|
.titles
|
|
|
|
.iter()
|
|
|
|
.map(|t| {
|
|
|
|
let (first, rest) = t.split_at(1);
|
2023-07-01 09:14:16 +00:00
|
|
|
Line::from(vec![first.yellow(), rest.green()])
|
2021-11-01 21:27:46 +00:00
|
|
|
})
|
2024-01-11 01:16:44 +00:00
|
|
|
.collect::<Tabs>()
|
2021-11-01 21:27:46 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).title("Tabs"))
|
2021-11-11 14:56:37 +00:00
|
|
|
.select(app.index)
|
2023-09-26 06:22:14 +00:00
|
|
|
.style(Style::default().cyan().on_gray())
|
|
|
|
.highlight_style(Style::default().bold().on_black());
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(tabs, tabs_area);
|
2021-11-11 14:56:37 +00:00
|
|
|
let inner = match app.index {
|
2021-11-01 21:27:46 +00:00
|
|
|
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!(),
|
|
|
|
};
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(inner, inner_area);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|