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

Command Line Resize #1728

Closed
StriderDM opened this issue Apr 18, 2020 · 12 comments · Fixed by #3838
Closed

Command Line Resize #1728

StriderDM opened this issue Apr 18, 2020 · 12 comments · Fixed by #3838
Labels
C-bug Category - fixes a bug, typically associated with an issue. E-good_first_issue Experience Level - Good for newcomers

Comments

@StriderDM
Copy link
Contributor

StriderDM commented Apr 18, 2020

The Tari Base Node, does not automatically resize the terminal window on startup, leading to text being wrapped onto subsequent lines and breaking up the intended UI

To Reproduce
In a new un-resized terminal window, run cargo run --bin tari_base_node

Expected behavior
The terminal should resize to fit the longest string on startup when displaying the UI

Screenshots
Current:
Screen Shot 2020-04-18 at 8 55 37 AM

Expected
Screen Shot 2020-04-18 at 9 00 24 AM

@StriderDM StriderDM added C-bug Category - fixes a bug, typically associated with an issue. E-good_first_issue Experience Level - Good for newcomers labels Apr 18, 2020
@dunxen
Copy link

dunxen commented Apr 30, 2020

I could take a crack at this. 🙂

@StriderDM
Copy link
Contributor Author

@duncandean contributions are welcome :)

@flipchan
Copy link

redo this function then

pub fn print_banner(commands: Vec<String>, chunk_size: i32) {

@flipchan
Copy link

you want to fingerprint stdout then , which is different libc functions on different sys

https://github.com/eminence/terminal-size/blob/master/src/unix.rs here is one with unsafe rust

@flipchan
Copy link

flipchan commented Jun 1, 2020

cat src/main.rs

root@burk0:/tmp/asd# cat src/main.rs 
use libc::ioctl;
use libc::isatty;
use libc::{winsize as WinSize, TIOCGWINSZ};



fn main() {
        let fd = libc::STDOUT_FILENO;
        let is_tty: bool = unsafe { isatty(fd) == 1 };
    if !is_tty {
        panic!("no tty");
    }
    let mut winsize = WinSize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    if unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut winsize) } == -1 {
        panic!("nope");
    }
    let rows = winsize.ws_row as usize;
    let cols = winsize.ws_col as usize;
    if rows > 0 && cols > 0 {
        println!("fluff: {} {}",cols, rows) 
        }
        let fluff = "h".repeat(cols);
    println!("{}", fluff);

}

@flipchan
Copy link

flipchan commented Jun 1, 2020

Working:
terminal resize

@flipchan
Copy link

flipchan commented Jun 1, 2020

working code:

use libc::ioctl;
use libc::isatty;
use libc::{winsize as WinSize, TIOCGWINSZ};

fn box_line(length: usize, is_top: bool) -> String {
    if length < 2 {
        return format!("");
    }
    if is_top {
        format!("{}{}{}", "┌", "─".repeat(length - 2), "┐")
    } else {
        format!("{}{}{}", "└", "─".repeat(length - 2), "┘")
    }
}

fn box_data(data: String, target_length: usize) -> String {
    let padding = if ((target_length - 2) / 2) > (data.chars().count() / 2) {
        ((target_length - 2) / 2) - (data.chars().count() / 2)
    } else {
        0
    };
    let mut s = format!("{}{}{}{}", " ".repeat(padding), data, " ".repeat(padding), "│");
    // for integer rounding error, usually only 1 char, to ensure border lines up
    while s.chars().count() < target_length - 1 {
        s = format!("{}{}", " ", s);
    }
    format!("{}{}", "│", s)
}


fn main() {
	let fd = libc::STDOUT_FILENO;
	let is_tty: bool = unsafe { isatty(fd) == 1 };
    if !is_tty {
        panic!("no tty");
    }
    let mut winsize = WinSize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    if unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut winsize) } == -1 {
        panic!("nope");
    }
    let rows = winsize.ws_row as usize;
    let cols = winsize.ws_col as usize;
    if rows > 0 && cols > 0 {
        println!("fluff: {} {}",cols, rows) 
	}
	let fluff = "h".repeat(cols);
	println!("{}",box_line(cols, true));
	let rows = rows-3;
	for tari in 0..rows{
		println!("{}", box_data("test".to_string(),cols))
		}
	println!("{}",box_line(cols, false));
//    println!("{}", fluff);

}

@flipchan
Copy link

flipchan commented Jun 1, 2020

Working when resizing terminals in tmux
first smaller one:
first
then make the window bigger:
second
and re run the script it finds the new screen width
third

@flipchan
Copy link

flipchan commented Jun 1, 2020

lets give find terminal size in to a function:

//give me the width and height of your terminal size
fn gimmesize() -> (usize, usize) {
        let fd = libc::STDOUT_FILENO;
        let is_tty: bool = unsafe { isatty(fd) == 1 };
    if !is_tty {
        panic!("no tty");
    }
    let mut winsize = WinSize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    if unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut winsize) } == -1 {
        panic!("nope");
    }
    let height = winsize.ws_row as usize;
    let width = winsize.ws_col as usize;
        return (width, height);
        }

@flipchan
Copy link

flipchan commented Jun 1, 2020

tried with gimmesize function and works will push to dev branch
tari with better ui

upper one is with the update and lower one is regular

@StriderDM
Copy link
Contributor Author

@flipchan Looks good, mind submitting a PR for this and then we can review it?

@zhangcheng
Copy link
Contributor

The above code snippets from @flipchan can only determine the size of the current terminal, but lack of a way to resize it.
Nonetheless, the knowledge in it leads me to discover the crossterm crate, which solves the problem nicely. Hence PR #3827.

zhangcheng added a commit to zhangcheng/tari that referenced this issue Feb 12, 2022
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.
zhangcheng added a commit to zhangcheng/tari that referenced this issue Feb 14, 2022
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.
aviator-app bot pushed a commit that referenced this issue Feb 14, 2022
feat(cli): resolves (#1728) Command Line Resize.

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.
zhangcheng added a commit to zhangcheng/tari that referenced this issue Feb 15, 2022
Description
---
To resolve tari-project#1728 better

Motivation and Context
---
As a follow-up of tari-project#3827.

How Has This Been Tested?
---
Tested on macOS Monterey (12.1).
Works for system Terminal app.
Doesn't work for iTerm2.
aviator-app bot pushed a commit that referenced this issue Feb 15, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category - fixes a bug, typically associated with an issue. E-good_first_issue Experience Level - Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants