Skip to content

Commit

Permalink
feat(cli): resolves (tari-project#1728) Command Line Resize.
Browse files Browse the repository at this point in the history
Description
---
Use [crossterm](https://github.com/crossterm-rs/crossterm) crate to resize cli properly.

Motivation and Context
---
My very first `good first issue` to work on tari codebase. ;-)

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 14, 2022
1 parent 8791e93 commit 6ca0bef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions applications/tari_base_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ anyhow = "1.0.53"
bincode = "1.3.1"
chrono = { version = "0.4.19", default-features = false }
config = { version = "0.9.3" }
crossterm = "0.22"
either = "1.6.1"
futures = { version = "^0.3.16", default-features = false, features = ["alloc"] }
log = { version = "0.4.8", features = ["std"] }
Expand Down
19 changes: 19 additions & 0 deletions applications/tari_base_node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::io::stdout;

use chrono::{Datelike, Utc};
use crossterm::{
execute,
terminal::{size, SetSize},
};
use tari_app_utilities::consts;

/// returns the top or bottom box line of the specified length
Expand Down Expand Up @@ -100,6 +106,16 @@ fn multiline_find_display_length(lines: &str) -> usize {
result
}

/// 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)
}
}
}

/// 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) {
let chunks: Vec<Vec<String>> = commands.chunks(chunk_size as usize).map(|x| x.to_vec()).collect();
Expand Down Expand Up @@ -141,6 +157,9 @@ 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

0 comments on commit 6ca0bef

Please sign in to comment.