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

Terminal size issues #2

Open
Euphrasiologist opened this issue Jan 11, 2023 · 2 comments
Open

Terminal size issues #2

Euphrasiologist opened this issue Jan 11, 2023 · 2 comments

Comments

@Euphrasiologist
Copy link
Owner

As nu_plugin is communicating over the stdout, the terminal_size crate doesn't work in its current implementation. It's ideal to use this crate as it's actively supported, and what the rest of nushell uses.

@fdncred has suggested this from discord:

fn terminal_size() -> Option<(Width, Height)> {
    use windows_sys::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE};
    let handle = unsafe { GetStdHandle(STD_ERROR_HANDLE) as RawHandle };
    terminal_size_using_handle(handle)
}

Or something similar. To think about!

@fdncred
Copy link
Contributor

fdncred commented Jan 11, 2023

The only real downside to this is you have to have a dependency on windows-sys (i think that's the crate terminal-size uses). But if you were including terminal-size, you'd have that dependency anyway. But if you decide to forward, I'd do something similar to what the term-size crate does, but in terminal-size syntax.

This is the term-size syntax. It should be easy-ish to retrofit for terminal-size I think.

  if unsafe { GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &mut console_data) } != 0 ||
        unsafe { GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &mut console_data) } != 0 ||
        unsafe { GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &mut console_data) } != 0 {
        Some(((console_data.srWindow.Right - console_data.srWindow.Left + 1) as usize,
                (console_data.srWindow.Bottom - console_data.srWindow.Top + 1) as usize))
    } else {
        None
    }

@fdncred
Copy link
Contributor

fdncred commented Feb 9, 2023

I just fought this issue with my nu_plugin_dbg and came up with this solution.

#[cfg(not(windows))]
fn termsize() -> usize {
    use std::os::fd::AsRawFd;

    // because STDOUT is redirected we need to use STDERR descriptor
    let (cols, _) = match terminal_size::terminal_size_using_fd(std::io::stderr().as_raw_fd()) {
        Some((w, h)) => (w.0, h.0),
        None => (0, 0),
    };

    cols as usize
}

#[cfg(windows)]
fn termsize() -> usize {
    use std::os::windows::io::RawHandle;
    use windows_sys::Win32::System::Console::{GetStdHandle, STD_ERROR_HANDLE};

    let stderr = unsafe { GetStdHandle(STD_ERROR_HANDLE) } as RawHandle;

    // because STDOUT is redirected we need to use STDERR descriptor
    let (cols, _) = match terminal_size::terminal_size_using_handle(stderr) {
        Some((w, h)) => (w.0, h.0),
        None => (0, 0),
    };

    cols as usize
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants