diff --git a/src/main.rs b/src/main.rs index f7b4bb3..5b60088 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{env::args, fs::OpenOptions, io::{self, Read, Write}}; 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}}, text::Line, widgets::{Block, BorderType, Paragraph} }; fn main() -> Result<(), std::io::Error> { @@ -36,6 +36,7 @@ struct App<'a> { options: Vec<&'a str>, index: usize, } + impl<'a> App<'a> { pub fn new(opts: Vec<&'a str>) -> Self { Self { @@ -44,6 +45,7 @@ impl<'a> App<'a> { index: 0, } } + pub fn run(&mut self, mut term: DefaultTerminal) -> Result<(), std::io::Error> { while !self.exit { term.draw(|frame| self.draw(frame))?; @@ -54,16 +56,36 @@ impl<'a> App<'a> { 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 b = Block::bordered().title_top(Line::from(" Select Option ").centered()).border_type(BorderType::Rounded); + + let max_width = self.options.iter().fold(0, |prev, next| { + let len = next.len()+5; + if len App<'a> { style = style.bg(WHITE).fg(BLACK); } - let p = Paragraph::new(format!("(F{}) {}", i+1, *s)).centered().style(style); + let p = Paragraph::new(format!("(F{}) {}", i+1, *s)).left_aligned().style(style); frame.render_widget(p, *r); } } + fn handle_events(&mut self) -> io::Result<()> { fn dec(a: &mut App) {