From 4fdef86b330425132194b20610af0ce18ce2a0eb Mon Sep 17 00:00:00 2001 From: ZHANG Cheng Date: Tue, 15 Feb 2022 00:10:14 +0800 Subject: [PATCH] feat(cli): resize terminal height Description --- To resolve #1728 better Motivation and Context --- As a follow-up of #3827. How Has This Been Tested? --- Tested on macOS Monterey (12.1). Works for system Terminal app. Doesn't work for iTerm2. --- applications/tari_base_node/src/cli.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/applications/tari_base_node/src/cli.rs b/applications/tari_base_node/src/cli.rs index da696c867c..cab727d892 100644 --- a/applications/tari_base_node/src/cli.rs +++ b/applications/tari_base_node/src/cli.rs @@ -23,10 +23,7 @@ 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,11 +105,9 @@ 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)) { - println!("Can't resize terminal to fit the box. Error: {}", e) - } +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) } } @@ -158,8 +153,6 @@ pub fn print_banner(commands: Vec, 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, 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); }