From b69e823375c3dd96340ba2606391a363a689ab53 Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Sun, 22 Sep 2024 22:21:25 +0530 Subject: [PATCH] fix: Min-term panic (#607) * Fix min-term panic * Align center --- tui/src/state.rs | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/tui/src/state.rs b/tui/src/state.rs index 514b822b..8699a5a5 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -84,44 +84,32 @@ impl AppState { let terminal_size = frame.area(); 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.width, terminal_size.height, MIN_WIDTH, MIN_HEIGHT, - ); - - let warning_paragraph = Paragraph::new(size_warning_message.clone()) + )) .alignment(Alignment::Center) .style(Style::default().fg(ratatui::style::Color::Red).bold()) .wrap(ratatui::widgets::Wrap { trim: true }); - // Get the maximum width and height of text lines - let text_lines: Vec = size_warning_message - .lines() - .map(|line| line.to_string()) - .collect(); - let max_line_length = text_lines.iter().map(|line| line.len()).max().unwrap_or(0); - let num_lines = text_lines.len(); + let centered_layout = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Fill(1), + Constraint::Length(5), + Constraint::Fill(1), + ]) + .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; + return frame.render_widget(warning, centered_layout[1]); } else { self.drawable = true; } - if !self.drawable { - return; - } - let label_block = Block::default() .borders(Borders::all())