Popup screen can display large texts

This commit is contained in:
Pierre HUBERT 2022-10-08 14:54:07 +02:00
parent dc634fc6bc
commit 7e6d18c252
4 changed files with 47 additions and 8 deletions

25
rust/Cargo.lock generated
View File

@ -471,6 +471,7 @@ dependencies = [
"num-derive", "num-derive",
"num-traits", "num-traits",
"sea_battle_backend", "sea_battle_backend",
"textwrap",
"tokio", "tokio",
"tui", "tui",
] ]
@ -809,6 +810,9 @@ name = "hashbrown"
version = "0.12.3" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash",
]
[[package]] [[package]]
name = "heck" name = "heck"
@ -1469,6 +1473,12 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1"
[[package]]
name = "smawk"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043"
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.4.7" version = "0.4.7"
@ -1510,6 +1520,11 @@ name = "textwrap"
version = "0.15.1" version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16"
dependencies = [
"smawk",
"unicode-linebreak",
"unicode-width",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
@ -1693,6 +1708,16 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" 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]] [[package]]
name = "unicode-normalization" name = "unicode-normalization"
version = "0.1.22" version = "0.1.22"

View File

@ -17,3 +17,4 @@ tokio = "1.21.2"
num = "0.4.0" num = "0.4.0"
num-traits = "0.2.15" num-traits = "0.2.15"
num-derive = "0.3.3" num-derive = "0.3.3"
textwrap = "0.15.1"

View File

@ -22,7 +22,7 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn E
/*let mut rules = GameRules::default(); /*let mut rules = GameRules::default();
rules.boats_can_touch = true; rules.boats_can_touch = true;
let res = set_boats_layout::set_boat_layout(&rules, terminal)?; // select_bot_type::select_bot_type(terminal)?;*/ let res = set_boats_layout::set_boat_layout(&rules, terminal)?; // select_bot_type::select_bot_type(terminal)?;*/
let res = popup_screen::popup_screen("Hello you are my guest today!", terminal); let res = popup_screen::popup_screen("Hi\nWelcome there", terminal);
Err(io::Error::new( Err(io::Error::new(
ErrorKind::Other, ErrorKind::Other,
format!("result: {:?}", res), format!("result: {:?}", res),

View File

@ -5,7 +5,6 @@ use crossterm::event;
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use tui::backend::Backend; use tui::backend::Backend;
use tui::layout::*; use tui::layout::*;
use tui::style::*;
use tui::text::*; use tui::text::*;
use tui::widgets::*; use tui::widgets::*;
use tui::{Frame, Terminal}; use tui::{Frame, Terminal};
@ -55,7 +54,11 @@ pub fn popup_screen<B: Backend>(
} }
fn ui<B: Backend>(f: &mut Frame<B>, model: &mut PopupScreen) { fn ui<B: Backend>(f: &mut Frame<B>, 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); let block = Block::default().borders(Borders::ALL).title(model.title);
f.render_widget(block, area); f.render_widget(block, area);
@ -63,14 +66,24 @@ fn ui<B: Backend>(f: &mut Frame<B>, model: &mut PopupScreen) {
// Create two chunks with equal horizontal screen space // Create two chunks with equal horizontal screen space
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .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 { .split(area.inner(&Margin {
horizontal: 1, horizontal: 2,
vertical: 1, vertical: 1,
})); }));
let text = Paragraph::new(model.msg); let text = lines
f.render_widget(text, chunks[0]); .iter()
.map(|s| Spans::from(s.as_ref()))
.collect::<Vec<_>>();
let paragraph = Paragraph::new(text);
f.render_widget(paragraph, chunks[0]);
let ok_button = ButtonWidget::new("OK", true); let ok_button = ButtonWidget::new("OK", true);
f.render_widget(ok_button, chunks[1]); f.render_widget(ok_button, chunks[1]);