1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-11-08 09:09:17 +01:00

Break help footer on narrow terminals

This commit is contained in:
mo8it 2024-08-17 16:54:44 +02:00
parent 3eaccbb61a
commit 72e557b3a9

@ -2,11 +2,11 @@ use anyhow::{Context, Result};
use ratatui::{ use ratatui::{
layout::{Constraint, Rect}, layout::{Constraint, Rect},
style::{Style, Stylize}, style::{Style, Stylize},
text::{Line, Span}, text::{Span, Text},
widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState}, widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState},
Frame, Frame,
}; };
use std::fmt::Write; use std::{fmt::Write, mem};
use crate::{app_state::AppState, progress_bar::progress_bar_ratatui}; use crate::{app_state::AppState, progress_bar::progress_bar_ratatui};
@ -162,6 +162,9 @@ impl<'a> UiState<'a> {
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> { pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
let area = frame.area(); let area = frame.area();
let narrow = area.width < 95;
let narrow_u16 = u16::from(narrow);
let table_height = area.height - 3 - narrow_u16;
frame.render_stateful_widget( frame.render_stateful_widget(
&self.table, &self.table,
@ -169,7 +172,7 @@ impl<'a> UiState<'a> {
x: 0, x: 0,
y: 0, y: 0,
width: area.width, width: area.width,
height: area.height - 3, height: table_height,
}, },
&mut self.table_state, &mut self.table_state,
); );
@ -183,7 +186,7 @@ impl<'a> UiState<'a> {
.block(Block::default().borders(Borders::BOTTOM)), .block(Block::default().borders(Borders::BOTTOM)),
Rect { Rect {
x: 0, x: 0,
y: area.height - 3, y: table_height,
width: area.width, width: area.width,
height: 2, height: 2,
}, },
@ -191,10 +194,19 @@ impl<'a> UiState<'a> {
let message = if self.message.is_empty() { let message = if self.message.is_empty() {
// Help footer. // Help footer.
let mut text = Text::default();
let mut spans = Vec::with_capacity(4); let mut spans = Vec::with_capacity(4);
spans.push(Span::raw( spans.push(Span::raw(
"↓/j ↑/k home/g end/G │ <c>ontinue at │ <r>eset exercise │ filter ", "↓/j ↑/k home/g end/G │ <c>ontinue at │ <r>eset exercise │",
)); ));
if narrow {
text.push_line(mem::take(&mut spans));
spans.push(Span::raw("filter "));
} else {
spans.push(Span::raw(" filter "));
}
match self.filter { match self.filter {
Filter::Done => { Filter::Done => {
spans.push("<d>one".underlined().magenta()); spans.push("<d>one".underlined().magenta());
@ -206,18 +218,20 @@ impl<'a> UiState<'a> {
} }
Filter::None => spans.push(Span::raw("<d>one/<p>ending")), Filter::None => spans.push(Span::raw("<d>one/<p>ending")),
} }
spans.push(Span::raw(" │ <q>uit list")); spans.push(Span::raw(" │ <q>uit list"));
Line::from(spans) text.push_line(spans);
text
} else { } else {
Line::from(self.message.as_str().light_blue()) Text::from(self.message.as_str().light_blue())
}; };
frame.render_widget( frame.render_widget(
message, message,
Rect { Rect {
x: 0, x: 0,
y: area.height - 1, y: table_height + 2,
width: area.width, width: area.width,
height: 1, height: 1 + narrow_u16,
}, },
); );