Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resize base node terminal on startup #3827

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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