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
|
# Select Option
|
||||||
|
|
||||||
Creates a tui selector based off of input args.
|
Creates a tui selector based off of `stdin`.
|
||||||
`select_option a b c` will create a selector for a, b, c.
|
`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.
|
Navigate with F-keys, j/k, or arrow keys, then press enter on your selection.
|
||||||
This will print out the selected option to stdout.
|
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::{
|
use ratatui::{
|
||||||
DefaultTerminal, Frame, crossterm::event::{self, Event}, layout::{self, Constraint, Layout}, style::{Style, palette::material::{BLACK, WHITE}}, widgets::{Block, BorderType, Paragraph}
|
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> {
|
fn main() -> Result<(), std::io::Error> {
|
||||||
let mut opts = Vec::new();
|
// let mut buffer = Vec::new();
|
||||||
args().skip(1).for_each(|a| opts.push(a));
|
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 mut app = App::new(opts);
|
||||||
let term = ratatui::init();
|
let term = ratatui::init();
|
||||||
@@ -20,13 +24,13 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct App {
|
struct App<'a> {
|
||||||
exit: bool,
|
exit: bool,
|
||||||
options: Vec<String>,
|
options: Vec<&'a str>,
|
||||||
index: usize,
|
index: usize,
|
||||||
}
|
}
|
||||||
impl App {
|
impl<'a> App<'a> {
|
||||||
pub fn new(opts: Vec<String>) -> Self {
|
pub fn new(opts: Vec<&'a str>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
exit: false,
|
exit: false,
|
||||||
options: opts,
|
options: opts,
|
||||||
@@ -69,6 +73,16 @@ impl App {
|
|||||||
}
|
}
|
||||||
fn handle_events(&mut self) -> io::Result<()> {
|
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 Ok(k) = event::read() {
|
||||||
if let Event::Key(e) = k {
|
if let Event::Key(e) = k {
|
||||||
match e.code {
|
match e.code {
|
||||||
@@ -82,12 +96,12 @@ impl App {
|
|||||||
self.exit = true;
|
self.exit = true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
event::KeyCode::Up => self.index = self.index.saturating_sub(1),
|
event::KeyCode::Up => dec(self),
|
||||||
event::KeyCode::Down => self.index += 1,
|
event::KeyCode::Down => inc(self),
|
||||||
event::KeyCode::Char(c) => {
|
event::KeyCode::Char(c) => {
|
||||||
match c {
|
match c {
|
||||||
'k' => self.index = self.index.saturating_sub(1),
|
'k' => dec(self),
|
||||||
'j' => self.index += 1,
|
'j' => inc(self),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user