init
This commit is contained in:
1510
Cargo.lock
generated
Normal file
1510
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "select_option"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
ratatui = "0.30.0"
|
||||
7
README.md
Normal file
7
README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Select Option
|
||||
|
||||
Creates a tui selector based off of input args.
|
||||
`select_option a b c` will create a selector for a, b, c.
|
||||
Navigate with F-keys, j/k, or arrow keys, then press enter on your selection.
|
||||
This will print out the selected option to stdout.
|
||||
|
||||
100
src/main.rs
Normal file
100
src/main.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user