works gooder now
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
10
README.md
10
README.md
@@ -1,7 +1,13 @@
|
||||
# Select Option
|
||||
|
||||
Creates a tui selector based off of input args.
|
||||
`select_option a b c` will create a selector for a, b, c.
|
||||
Creates a tui selector based off of `stdin`.
|
||||
`printf '1\n2\n3' | select_option` 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.
|
||||
|
||||
## Usecases
|
||||
|
||||
Selecting a drive from the system
|
||||
```bash
|
||||
lsblk -lno type,name | grep part | awk '{ print "/dev/" $2 }' | select_option
|
||||
```
|
||||
|
||||
36
src/main.rs
36
src/main.rs
@@ -1,12 +1,16 @@
|
||||
use std::{env::args, io};
|
||||
use std::{env::args, io::{self, BufRead, Read, stdin}};
|
||||
|
||||
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 buffer = Vec::new();
|
||||
let mut buffer = String::new();
|
||||
let stdin = io::stdin();
|
||||
let mut handle = stdin.lock();
|
||||
handle.read_to_string(&mut buffer)?;
|
||||
let opts = buffer.lines().collect::<Vec<&str>>();
|
||||
|
||||
let mut app = App::new(opts);
|
||||
let term = ratatui::init();
|
||||
@@ -20,13 +24,13 @@ fn main() -> Result<(), std::io::Error> {
|
||||
return res;
|
||||
}
|
||||
|
||||
struct App {
|
||||
struct App<'a> {
|
||||
exit: bool,
|
||||
options: Vec<String>,
|
||||
options: Vec<&'a str>,
|
||||
index: usize,
|
||||
}
|
||||
impl App {
|
||||
pub fn new(opts: Vec<String>) -> Self {
|
||||
impl<'a> App<'a> {
|
||||
pub fn new(opts: Vec<&'a str>) -> Self {
|
||||
Self {
|
||||
exit: false,
|
||||
options: opts,
|
||||
@@ -69,6 +73,16 @@ impl App {
|
||||
}
|
||||
fn handle_events(&mut self) -> io::Result<()> {
|
||||
|
||||
fn dec(a: &mut App) {
|
||||
a.index = a.index.saturating_sub(1);
|
||||
}
|
||||
|
||||
fn inc(a: &mut App) {
|
||||
if a.index < a.options.len()-1 {
|
||||
a.index += 1
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(k) = event::read() {
|
||||
if let Event::Key(e) = k {
|
||||
match e.code {
|
||||
@@ -82,12 +96,12 @@ impl App {
|
||||
self.exit = true;
|
||||
}
|
||||
},
|
||||
event::KeyCode::Up => self.index = self.index.saturating_sub(1),
|
||||
event::KeyCode::Down => self.index += 1,
|
||||
event::KeyCode::Up => dec(self),
|
||||
event::KeyCode::Down => inc(self),
|
||||
event::KeyCode::Char(c) => {
|
||||
match c {
|
||||
'k' => self.index = self.index.saturating_sub(1),
|
||||
'j' => self.index += 1,
|
||||
'k' => dec(self),
|
||||
'j' => inc(self),
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user