From 7e6d18c25282f4fd1172b79477c4a6edec71d699 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 8 Oct 2022 14:54:07 +0200 Subject: [PATCH] Popup screen can display large texts --- rust/Cargo.lock | 25 +++++++++++++++++++ rust/cli_player/Cargo.toml | 3 ++- rust/cli_player/src/main.rs | 2 +- .../cli_player/src/ui_screens/popup_screen.rs | 25 ++++++++++++++----- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index cc429e9..cef9a3b 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -471,6 +471,7 @@ dependencies = [ "num-derive", "num-traits", "sea_battle_backend", + "textwrap", "tokio", "tui", ] @@ -809,6 +810,9 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] [[package]] name = "heck" @@ -1469,6 +1473,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" +[[package]] +name = "smawk" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" + [[package]] name = "socket2" version = "0.4.7" @@ -1510,6 +1520,11 @@ name = "textwrap" version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width", +] [[package]] name = "thiserror" @@ -1693,6 +1708,16 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" +[[package]] +name = "unicode-linebreak" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5faade31a542b8b35855fff6e8def199853b2da8da256da52f52f1316ee3137" +dependencies = [ + "hashbrown", + "regex", +] + [[package]] name = "unicode-normalization" version = "0.1.22" diff --git a/rust/cli_player/Cargo.toml b/rust/cli_player/Cargo.toml index 81c3ca9..22cb182 100644 --- a/rust/cli_player/Cargo.toml +++ b/rust/cli_player/Cargo.toml @@ -16,4 +16,5 @@ lazy_static = "1.4.0" tokio = "1.21.2" num = "0.4.0" num-traits = "0.2.15" -num-derive = "0.3.3" \ No newline at end of file +num-derive = "0.3.3" +textwrap = "0.15.1" \ No newline at end of file diff --git a/rust/cli_player/src/main.rs b/rust/cli_player/src/main.rs index eab7a2f..802f0f2 100644 --- a/rust/cli_player/src/main.rs +++ b/rust/cli_player/src/main.rs @@ -22,7 +22,7 @@ async fn run_app(terminal: &mut Terminal) -> Result<(), Box( } fn ui(f: &mut Frame, model: &mut PopupScreen) { - let area = centered_rect_size(model.msg.len() as u16 + 2, 5, &f.size()); + // Preprocess message + let lines = textwrap::wrap(model.msg, f.size().width as usize - 20); + let line_max_len = lines.iter().map(|l| l.len()).max().unwrap(); + + let area = centered_rect_size(line_max_len as u16 + 4, 5 + lines.len() as u16, &f.size()); let block = Block::default().borders(Borders::ALL).title(model.title); f.render_widget(block, area); @@ -63,14 +66,24 @@ fn ui(f: &mut Frame, model: &mut PopupScreen) { // Create two chunks with equal horizontal screen space let chunks = Layout::default() .direction(Direction::Vertical) - .constraints([Constraint::Percentage(70), Constraint::Percentage(30)].as_ref()) + .constraints( + [ + Constraint::Length(lines.len() as u16), + Constraint::Length(3), + ] + .as_ref(), + ) .split(area.inner(&Margin { - horizontal: 1, + horizontal: 2, vertical: 1, })); - let text = Paragraph::new(model.msg); - f.render_widget(text, chunks[0]); + let text = lines + .iter() + .map(|s| Spans::from(s.as_ref())) + .collect::>(); + let paragraph = Paragraph::new(text); + f.render_widget(paragraph, chunks[0]); let ok_button = ButtonWidget::new("OK", true); f.render_widget(ok_button, chunks[1]);