shrink and center the box

This commit is contained in:
2026-02-03 13:26:07 -07:00
parent bdfce9a055
commit e7c0701027

View File

@@ -1,7 +1,7 @@
use std::{env::args, fs::OpenOptions, io::{self, Read, Write}}; use std::{env::args, fs::OpenOptions, io::{self, Read, Write}};
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}}, text::Line, widgets::{Block, BorderType, Paragraph}
}; };
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
@@ -36,6 +36,7 @@ struct App<'a> {
options: Vec<&'a str>, options: Vec<&'a str>,
index: usize, index: usize,
} }
impl<'a> App<'a> { impl<'a> App<'a> {
pub fn new(opts: Vec<&'a str>) -> Self { pub fn new(opts: Vec<&'a str>) -> Self {
Self { Self {
@@ -44,6 +45,7 @@ impl<'a> App<'a> {
index: 0, index: 0,
} }
} }
pub fn run(&mut self, mut term: DefaultTerminal) -> Result<(), std::io::Error> { pub fn run(&mut self, mut term: DefaultTerminal) -> Result<(), std::io::Error> {
while !self.exit { while !self.exit {
term.draw(|frame| self.draw(frame))?; term.draw(|frame| self.draw(frame))?;
@@ -54,16 +56,36 @@ impl<'a> App<'a> {
fn draw(&self, frame: &mut Frame) { fn draw(&self, frame: &mut Frame) {
// let layout = Layout::default().direction(layout::Direction::Vertical).split(frame.area()); // let layout = Layout::default().direction(layout::Direction::Vertical).split(frame.area());
let b = Block::bordered().title_top("Select Option").border_type(BorderType::Rounded); let b = Block::bordered().title_top(Line::from(" Select Option ").centered()).border_type(BorderType::Rounded);
let screen = frame.area();
let inner = b.inner(screen); let max_width = self.options.iter().fold(0, |prev, next| {
let len = next.len()+5;
if len<prev {
prev
} else {
len
}
});
let center_col = Layout::default()
.direction(layout::Direction::Horizontal)
.constraints([Constraint::Fill(1), Constraint::Min(max_width as u16), Constraint::Fill(1)])
.split(frame.area())[1];
let center = Layout::default()
.direction(layout::Direction::Vertical)
.constraints([Constraint::Fill(1), Constraint::Max((self.options.len()+2) as u16), Constraint::Fill(1)])
.split(center_col)[1];
let inner = b.inner(center);
let a = Layout::default() let a = Layout::default()
.direction(layout::Direction::Vertical) .direction(layout::Direction::Vertical)
.constraints(self.options.iter().map(|_| Constraint::Max(1))) .constraints(self.options.iter().map(|_| Constraint::Max(1)))
.split(inner); .split(inner);
frame.render_widget(b, screen); frame.render_widget(b, center);
for (i, (r, s)) in a.iter().zip(&self.options).enumerate() { for (i, (r, s)) in a.iter().zip(&self.options).enumerate() {
let mut style = Style::default(); let mut style = Style::default();
@@ -72,12 +94,13 @@ impl<'a> App<'a> {
style = style.bg(WHITE).fg(BLACK); 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); frame.render_widget(p, *r);
} }
} }
fn handle_events(&mut self) -> io::Result<()> { fn handle_events(&mut self) -> io::Result<()> {
fn dec(a: &mut App) { fn dec(a: &mut App) {