Skip to content

Commit

Permalink
make cursive work with ncurses v6.0.1 (#778)
Browse files Browse the repository at this point in the history
* make it work with ncurses-rs >=6.0.1

which means that ncurses-rs already has the
needed changes in this PR:
jeaye/ncurses-rs#220
and/or from this PR:
jeaye/ncurses-rs#218

* get rid of a warning when using newterm

newterm https://github.com/jeaye/ncurses-rs/blob/3aa22bc279e4929e3ab69d49f75a18eda3e431e9/src/lib.rs#L1023-L1029
CString::new https://doc.rust-lang.org/std/ffi/struct.CString.html#method.new

bubble up this newterm error

as suggested here: #778 (comment)

Co-authored-by: Alexandre Bury <alexandre.bury@gmail.com>

preserve original error in the panic report

otherwise, we'd not know why ncurses-rs newterm errored

directly include the variable name in the format! expression

as suggested here: #778 (comment)

Co-authored-by: Alexandre Bury <alexandre.bury@gmail.com>

* get rid of unused warning for addstr

---------

Co-authored-by: Alexandre Bury <alexandre.bury@gmail.com>
  • Loading branch information
correabuscar and gyscos authored Jul 12, 2024
1 parent 4f5ebdc commit cc4f36f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cursive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ version = "2"
[dependencies.ncurses]
features = ["wide"]
optional = true
version = "5.99.0"
version = "6.0.1"

[dependencies.pancurses]
features = ["wide"]
Expand Down
43 changes: 24 additions & 19 deletions cursive/src/backends/curses/n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ impl Backend {
let path = CString::new(output_path).unwrap();
unsafe { libc::fopen(path.as_ptr(), mode.as_ptr()) }
};
ncurses::newterm(None, output, input);
ncurses::newterm(None, output, input).map_err(|e| {
io::Error::new(io::ErrorKind::Other, format!("could not call newterm: {e}"))
})?;

// Enable keypad (like arrows)
ncurses::keypad(ncurses::stdscr(), true);

Expand Down Expand Up @@ -357,28 +360,28 @@ impl backend::Backend for Backend {

fn set_effect(&self, effect: Effect) {
let style = match effect {
Effect::Reverse => ncurses::A_REVERSE(),
Effect::Simple => ncurses::A_NORMAL(),
Effect::Dim => ncurses::A_DIM(),
Effect::Bold => ncurses::A_BOLD(),
Effect::Blink => ncurses::A_BLINK(),
Effect::Italic => ncurses::A_ITALIC(),
Effect::Strikethrough => ncurses::A_NORMAL(),
Effect::Underline => ncurses::A_UNDERLINE(),
Effect::Reverse => ncurses::A_REVERSE,
Effect::Simple => ncurses::A_NORMAL,
Effect::Dim => ncurses::A_DIM,
Effect::Bold => ncurses::A_BOLD,
Effect::Blink => ncurses::A_BLINK,
Effect::Italic => ncurses::A_ITALIC,
Effect::Strikethrough => ncurses::A_NORMAL,
Effect::Underline => ncurses::A_UNDERLINE,
};
ncurses::attron(style);
}

fn unset_effect(&self, effect: Effect) {
let style = match effect {
Effect::Reverse => ncurses::A_REVERSE(),
Effect::Simple => ncurses::A_NORMAL(),
Effect::Dim => ncurses::A_DIM(),
Effect::Bold => ncurses::A_BOLD(),
Effect::Blink => ncurses::A_BLINK(),
Effect::Italic => ncurses::A_ITALIC(),
Effect::Strikethrough => ncurses::A_NORMAL(),
Effect::Underline => ncurses::A_UNDERLINE(),
Effect::Reverse => ncurses::A_REVERSE,
Effect::Simple => ncurses::A_NORMAL,
Effect::Dim => ncurses::A_DIM,
Effect::Bold => ncurses::A_BOLD,
Effect::Blink => ncurses::A_BLINK,
Effect::Italic => ncurses::A_ITALIC,
Effect::Strikethrough => ncurses::A_NORMAL,
Effect::Underline => ncurses::A_UNDERLINE,
};
ncurses::attroff(style);
}
Expand All @@ -402,7 +405,9 @@ impl backend::Backend for Backend {
}

fn print(&self, text: &str) {
ncurses::addstr(text);
// &str is assured it doesn't contain any \0 aka nuls here due to PR 786
// thus we can ignore the return value and avoid warning: unused `Result` that must be used
let _ = ncurses::addstr(text);
}
}

Expand Down Expand Up @@ -533,7 +538,7 @@ fn initialize_keymap() -> HashMap<i32, Event> {
}

// Ncurses provides a F1 variable, but no modifiers
add_fn(ncurses::KEY_F1, Event::Key, &mut map);
add_fn(ncurses::KEY_F(1), Event::Key, &mut map);
add_fn(277, Event::Shift, &mut map);
add_fn(289, Event::Ctrl, &mut map);
add_fn(301, Event::CtrlShift, &mut map);
Expand Down

0 comments on commit cc4f36f

Please sign in to comment.