Compare commits
No commits in common. "f7c76e7ed951b2315fbebd2a8b944719a8e7490a" and "0f207cd70126a717e8a8d301daca55b4c0a9997f" have entirely different histories.
f7c76e7ed9
...
0f207cd701
1 changed files with 25 additions and 45 deletions
70
src/lib.rs
70
src/lib.rs
|
@ -1,9 +1,8 @@
|
|||
use gag::BufferRedirect;
|
||||
use std::{
|
||||
error::Error,
|
||||
ffi::{c_char, CStr, CString},
|
||||
ffi::{c_char, CString},
|
||||
io::Read,
|
||||
str::Utf8Error,
|
||||
};
|
||||
|
||||
use wmenu_sys::bindings;
|
||||
|
@ -17,16 +16,10 @@ unsafe extern "C" fn print_item(menu: *mut bindings::menu, text: *mut i8, exit:
|
|||
}
|
||||
}
|
||||
|
||||
type ItemHandler = unsafe extern "C" fn(menu: *mut bindings::menu, text: *mut i8, exit: bool);
|
||||
|
||||
fn to_c_char(text: &str) -> *mut c_char {
|
||||
return CString::new(text).unwrap().into_raw();
|
||||
}
|
||||
|
||||
fn from_c_string(text: *mut c_char) -> Result<&'static str, Utf8Error> {
|
||||
unsafe { CStr::from_ptr(text).to_str() }
|
||||
}
|
||||
|
||||
enum Output {
|
||||
Stdout(String),
|
||||
Stderr(String),
|
||||
|
@ -77,117 +70,104 @@ impl std::fmt::Display for MenuError {
|
|||
|
||||
impl Error for MenuError {}
|
||||
|
||||
trait Handle {
|
||||
fn handle_item(&self, menu: Menu, text: &str) -> bool;
|
||||
}
|
||||
|
||||
pub struct Menu {
|
||||
menu: *mut bindings::menu,
|
||||
ctx: *mut bindings::menu,
|
||||
}
|
||||
|
||||
impl Menu {
|
||||
pub fn new() -> Menu {
|
||||
Self {
|
||||
menu: unsafe { bindings::menu_create(None) },
|
||||
ctx: unsafe { bindings::menu_create(Some(print_item)) },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_menu(menu: *mut bindings::menu) -> Self {
|
||||
Self { menu }
|
||||
}
|
||||
|
||||
fn inner(&mut self) -> &mut bindings::menu {
|
||||
unsafe { &mut *self.menu }
|
||||
}
|
||||
|
||||
pub fn handler(&mut self, handler: ItemHandler) -> &mut Self {
|
||||
self.inner().callback = Some(handler);
|
||||
self
|
||||
fn into_inner(&mut self) -> &mut bindings::menu {
|
||||
unsafe { &mut *self.ctx }
|
||||
}
|
||||
|
||||
pub fn bottom(&mut self, value: bool) -> &mut Self {
|
||||
self.inner().bottom = value;
|
||||
self.into_inner().bottom = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn password(&mut self, value: bool) -> &mut Self {
|
||||
self.inner().passwd = value;
|
||||
self.into_inner().passwd = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn font(&mut self, value: &str) -> &mut Self {
|
||||
self.inner().font = to_c_char(value);
|
||||
self.into_inner().font = to_c_char(value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn lines(&mut self, value: i32) -> &mut Self {
|
||||
self.inner().lines = value;
|
||||
self.into_inner().lines = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn output_name(&mut self, value: &str) -> &mut Self {
|
||||
self.inner().output_name = to_c_char(value);
|
||||
self.into_inner().output_name = to_c_char(value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn prompt(&mut self, value: &str) -> &mut Self {
|
||||
self.inner().prompt = to_c_char(value);
|
||||
self.into_inner().prompt = to_c_char(value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn normal_bg(&mut self, value: u32) -> &mut Self {
|
||||
self.inner().normalbg = value;
|
||||
self.into_inner().normalbg = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn normal_fg(&mut self, value: u32) -> &mut Self {
|
||||
self.inner().normalfg = value;
|
||||
self.into_inner().normalfg = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn prompt_fg(&mut self, value: u32) -> &mut Self {
|
||||
self.inner().promptfg = value;
|
||||
self.into_inner().promptfg = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn prompt_bg(&mut self, value: u32) -> &mut Self {
|
||||
self.inner().promptbg = value;
|
||||
self.into_inner().promptbg = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn selection_bg(&mut self, value: u32) -> &mut Self {
|
||||
self.inner().selectionbg = value;
|
||||
self.into_inner().selectionbg = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn selection_fg(&mut self, value: u32) -> &mut Self {
|
||||
self.inner().selectionbg = value;
|
||||
self.into_inner().selectionbg = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn width(&mut self, value: i32) -> &mut Self {
|
||||
self.inner().width = value;
|
||||
self.into_inner().width = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn height(&mut self, value: i32) -> &mut Self {
|
||||
self.inner().height = value;
|
||||
self.into_inner().height = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn line_height(&mut self, value: i32) -> &mut Self {
|
||||
self.inner().line_height = value;
|
||||
self.into_inner().line_height = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn padding(&mut self, value: i32) -> &mut Self {
|
||||
self.inner().padding = value;
|
||||
self.into_inner().padding = value;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_item(&mut self, item: &str) -> &mut Self {
|
||||
unsafe {
|
||||
bindings::menu_add_item(self.menu, to_c_char(item));
|
||||
bindings::menu_add_item(self.ctx, to_c_char(item));
|
||||
}
|
||||
|
||||
self
|
||||
|
@ -205,7 +185,7 @@ impl Menu {
|
|||
let output = OutputCapture::capture().unwrap();
|
||||
|
||||
unsafe {
|
||||
match (bindings::menu_run(self.menu), output.get_output()) {
|
||||
match (bindings::menu_run(self.ctx), output.get_output()) {
|
||||
(0, Ok(Output::Stdout(value))) => Ok(value.trim_end().to_string()),
|
||||
(n, Ok(Output::Stdout(value) | Output::Stderr(value))) => {
|
||||
Err(MenuError::new(n, value))
|
||||
|
@ -219,7 +199,7 @@ impl Menu {
|
|||
impl Drop for Menu {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
bindings::menu_destroy(self.menu);
|
||||
bindings::menu_destroy(self.ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -232,7 +212,7 @@ mod tests {
|
|||
fn awawa() {
|
||||
let items = ["roewrn", "sybil", "vex"];
|
||||
|
||||
let mut menu = Menu::new(print_item);
|
||||
let mut menu = Menu::new();
|
||||
let menu = menu
|
||||
.font("monospace 12")
|
||||
.height(32)
|
||||
|
|
Loading…
Add table
Reference in a new issue