Skip to content

Commit

Permalink
feat(cli): resize terminal height (#3838)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zhangcheng committed Feb 15, 2022
1 parent ee4b52d commit 9026152
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions applications/tari_base_node/src/cli.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

0 comments on commit 9026152

Please sign in to comment.