feat(cli): resize terminal height
Description
To resolve better
Motivation and Context
As a follow-up of
How Has This Been Tested?
Tested on macOS Monterey (12.1).
Works for system Terminal app.
Doesn't work for iTerm2.
use std::io::stdout;
use chrono::{Datelike, Utc};
-use crossterm::{
- execute,
- terminal::{size, SetSize},
-};
+use crossterm::{execute, terminal::SetSize};
use tari_app_utilities::consts;
/// returns the top or bottom box line of the specified length
@@ -108,13 +105,11 @@ fn multiline_find_display_length(lines: &str) -> usize {
/// Try to resize terminal to make sure the width is enough.
/// In case of error, just simply print out the error.
-fn resize_terminal_to_fit_the_box(width: usize) {
- if let Ok((_, rows)) = size() {
- if let Err(e) = execute!(stdout(), SetSize(width as u16, rows)) {
+fn resize_terminal_to_fit_the_box(width: usize, height: usize) {
+ if let Err(e) = execute!(stdout(), SetSize(width as u16, height as u16)) {
println!("Can't resize terminal to fit the box. Error: {}", e)
}
}
-}
/// Prints a pretty banner on the console as well as the list of available commands
pub fn print_banner(commands: Vec<String>, chunk_size: i32) {
@@ -158,8 +153,6 @@ pub fn print_banner(commands: Vec<String>, chunk_size: i32) {
let banner = include!("../assets/tari_banner.rs");
let target_line_length = multiline_find_display_length(banner);
- resize_terminal_to_fit_the_box(target_line_length);
-
for line in banner.lines() {
println!("{}", line);
}
@@ -187,8 +180,13 @@ pub fn print_banner(commands: Vec<String>, chunk_size: i32) {
println!("{}", box_data("~~~~~~~~".to_string(), target_line_length));
println!("{}", box_separator(target_line_length));
let rows = box_tabular_data_rows(command_data, row_cell_size, target_line_length, 10);
+ // There are 24 fixed rows besides the possible changed "Commands" rows
+ // and plus 2 more blank rows for better layout.
+ let height_to_resize = &rows.len() + 24 + 2;
for row in rows {
println!("{}", row);
}
println!("{}", box_line(target_line_length, false));
+
+ resize_terminal_to_fit_the_box(target_line_length, height_to_resize);
}
