pancurses
pancurses copied to clipboard
Add stdscr()
Fixes #40
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
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.
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.
If newterm returning a Window works I'd prefer that over exposing stdscr.
Sounds good. Newterm is supposed to replace initscr, so returning a window as well makes sense.
Can we merge/release this one, since it exposes a pretty useful function?
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: https://github.com/ihalila/pancurses/commit/16139e4fa7318e6fde742b7c93ba169adb424ee5.
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("█");