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

Add stdscr() #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add stdscr() #43

wants to merge 1 commit into from

Conversation

gyscos
Copy link
Contributor

@gyscos gyscos commented Apr 10, 2018

Fixes #40

@gyscos
Copy link
Contributor Author

gyscos commented Apr 11, 2018

The test failure for windows are:

  • Some random SSL error for "Environment: channel=beta, target=i686-pc-windows-msvc"
  • Some pre-existing failures for the windows-gnu toolchain

@ihalila
Copy link
Owner

ihalila commented Apr 15, 2018

I wonder if theres a way to enable the newterm() workflow without exposing a function like this, since I like that currently the only way to get a Window is to call initscr(), ie. a user cannot accidentally attempt any Window specific calls without having initialized the curses system. (There's still space for things not working as shown in #44 so it's not perfect).

This would be a place where I think it'd be warranted to differ from the regular ncurses API a bit more.

How does a program usually do things with newterm()? I haven't used it myself.

@gyscos
Copy link
Contributor Author

gyscos commented Apr 15, 2018

I either call stdscr, or methods that implicitly use stdscr internally.
We could have the newterm method return a window, either as sole or second return value.

@ihalila
Copy link
Owner

ihalila commented Apr 15, 2018

If newterm returning a Window works I'd prefer that over exposing stdscr.

@gyscos
Copy link
Contributor Author

gyscos commented Apr 15, 2018

Sounds good. Newterm is supposed to replace initscr, so returning a window as well makes sense.

@TimonPost
Copy link

TimonPost commented Jan 3, 2020

Can we merge/release this one, since it exposes a pretty useful function?

@TimonPost
Copy link

TimonPost commented Jan 8, 2020

There is one problem with this pull request. This is Unicode support. This PR supports us to use newterm and access it's Window with stdsrc. Though we can't use Unicode on this new window.

Unicode support was implemented by this commit: 16139e4.

Though that commit did not take into account that another source than stdout could be used.

The solution for this is to add platform_specific::pre_init(); to the first line of the function newterm(). Why not in stdsrc()? The reason for this is that it is called after ncurses is already initialized by newterm(). And platform_specific::pre_init(); is required to be called before every other function.

like

pub fn newterm(t: Option<&str>, output: FILE, input: FILE) -> ScrPtr {
    platform_specific::pre_init();
    let term_type = t.map(|x| CString::new(x).unwrap());
    let type_ptr = match term_type {
        Some(ref s) => s.as_ptr(),
        _ => std::ptr::null(),
    };
    unsafe { curses::newterm(type_ptr, output, input) }
}

I am using this code, and the Unicode support worked when I added the pre_init to newterm. But didn't when I added it to stdsrc().

// By default pancurses use stdout.
// We can change this by calling `new_term` with an FILE pointer to the source.
// Which is /dev/tty in our case.
let file = File::open("/dev/tty").unwrap();
let c_file = unsafe {
    libc::fdopen(
        file.into_raw_fd(),
        CStr::from_bytes_with_nul_unchecked(b"w+\0").as_ptr(),
    )
};
// Create screen pointer which we will be using for this backend.
let screen = unsafe { pancurses::newterm(Some(env!("TERM")), c_file, c_file) };
// Get `Window` of the created screen.
let window = pancurses::stdscr();

window.printw("█");

@TimonPost TimonPost mentioned this pull request Jan 8, 2020
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

Successfully merging this pull request may close these issues.

Add stdscr()
3 participants