fix: Min-term panic (#607)

* Fix min-term panic

* Align center
This commit is contained in:
JEEVITHA KANNAN K S 2024-09-22 22:21:25 +05:30 committed by GitHub
parent aca42f2411
commit b69e823375
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -84,44 +84,32 @@ impl AppState {
let terminal_size = frame.area(); let terminal_size = frame.area();
if terminal_size.width < MIN_WIDTH || terminal_size.height < MIN_HEIGHT { if terminal_size.width < MIN_WIDTH || terminal_size.height < MIN_HEIGHT {
let size_warning_message = format!( let warning = Paragraph::new(format!(
"Terminal size too small:\nWidth = {} Height = {}\n\nMinimum size:\nWidth = {} Height = {}", "Terminal size too small:\nWidth = {} Height = {}\n\nMinimum size:\nWidth = {} Height = {}",
terminal_size.width, terminal_size.width,
terminal_size.height, terminal_size.height,
MIN_WIDTH, MIN_WIDTH,
MIN_HEIGHT, MIN_HEIGHT,
); ))
let warning_paragraph = Paragraph::new(size_warning_message.clone())
.alignment(Alignment::Center) .alignment(Alignment::Center)
.style(Style::default().fg(ratatui::style::Color::Red).bold()) .style(Style::default().fg(ratatui::style::Color::Red).bold())
.wrap(ratatui::widgets::Wrap { trim: true }); .wrap(ratatui::widgets::Wrap { trim: true });
// Get the maximum width and height of text lines let centered_layout = Layout::default()
let text_lines: Vec<String> = size_warning_message .direction(Direction::Vertical)
.lines() .constraints([
.map(|line| line.to_string()) Constraint::Fill(1),
.collect(); Constraint::Length(5),
let max_line_length = text_lines.iter().map(|line| line.len()).max().unwrap_or(0); Constraint::Fill(1),
let num_lines = text_lines.len(); ])
.split(terminal_size);
// Calculate the centered area
let centered_area = ratatui::layout::Rect {
x: terminal_size.x + (terminal_size.width - max_line_length as u16) / 2,
y: terminal_size.y + (terminal_size.height - num_lines as u16) / 2,
width: max_line_length as u16,
height: num_lines as u16,
};
frame.render_widget(warning_paragraph, centered_area);
self.drawable = false; self.drawable = false;
return frame.render_widget(warning, centered_layout[1]);
} else { } else {
self.drawable = true; self.drawable = true;
} }
if !self.drawable {
return;
}
let label_block = let label_block =
Block::default() Block::default()
.borders(Borders::all()) .borders(Borders::all())