This commit is contained in:
2026-02-02 22:03:20 -07:00
commit 07435e9f9b
4 changed files with 1624 additions and 0 deletions

100
src/main.rs Normal file
View File

@@ -0,0 +1,100 @@
use std::{env::args, io};
use ratatui::{
DefaultTerminal, Frame, crossterm::event::{self, Event}, layout::{self, Constraint, Layout}, style::{Style, palette::material::{BLACK, WHITE}}, widgets::{Block, BorderType, Paragraph}
};
fn main() -> Result<(), std::io::Error> {
let mut opts = Vec::new();
args().skip(1).for_each(|a| opts.push(a));
let mut app = App::new(opts);
let term = ratatui::init();
let res = app.run(term);
ratatui::restore();
let v = &app.options[app.index];
println!("{}", v);
return res;
}
struct App {
exit: bool,
options: Vec<String>,
index: usize,
}
impl App {
pub fn new(opts: Vec<String>) -> Self {
Self {
exit: false,
options: opts,
index: 0,
}
}
pub fn run(&mut self, mut term: DefaultTerminal) -> Result<(), std::io::Error> {
while !self.exit {
term.draw(|frame| self.draw(frame))?;
self.handle_events()?;
}
Ok(())
}
fn draw(&self, frame: &mut Frame) {
// let layout = Layout::default().direction(layout::Direction::Vertical).split(frame.area());
let b = Block::bordered().title_top("Select Option").border_type(BorderType::Rounded);
let screen = frame.area();
let inner = b.inner(screen);
let a = Layout::default()
.direction(layout::Direction::Vertical)
.constraints(self.options.iter().map(|_| Constraint::Max(1)))
.split(inner);
frame.render_widget(b, screen);
for (i, (r, s)) in a.iter().zip(&self.options).enumerate() {
let mut style = Style::default();
if self.index == i {
style = style.bg(WHITE).fg(BLACK);
}
let p = Paragraph::new(format!("(F{}) {}", i+1, *s)).centered().style(style);
frame.render_widget(p, *r);
}
}
fn handle_events(&mut self) -> io::Result<()> {
if let Ok(k) = event::read() {
if let Event::Key(e) = k {
match e.code {
event::KeyCode::Enter => {
self.exit = true;
},
event::KeyCode::F(n) => {
let n = n as usize;
if n <= self.options.len() {
self.index = n-1;
self.exit = true;
}
},
event::KeyCode::Up => self.index = self.index.saturating_sub(1),
event::KeyCode::Down => self.index += 1,
event::KeyCode::Char(c) => {
match c {
'k' => self.index = self.index.saturating_sub(1),
'j' => self.index += 1,
_ => {}
}
},
_ => {}
}
}
}
Ok(())
}
}