Compare commits

..

No commits in common. "f7c76e7ed951b2315fbebd2a8b944719a8e7490a" and "0f207cd70126a717e8a8d301daca55b4c0a9997f" have entirely different histories.

View file

@ -1,9 +1,8 @@
use gag::BufferRedirect; use gag::BufferRedirect;
use std::{ use std::{
error::Error, error::Error,
ffi::{c_char, CStr, CString}, ffi::{c_char, CString},
io::Read, io::Read,
str::Utf8Error,
}; };
use wmenu_sys::bindings; 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 { fn to_c_char(text: &str) -> *mut c_char {
return CString::new(text).unwrap().into_raw(); 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 { enum Output {
Stdout(String), Stdout(String),
Stderr(String), Stderr(String),
@ -77,117 +70,104 @@ impl std::fmt::Display for MenuError {
impl Error for MenuError {} impl Error for MenuError {}
trait Handle {
fn handle_item(&self, menu: Menu, text: &str) -> bool;
}
pub struct Menu { pub struct Menu {
menu: *mut bindings::menu, ctx: *mut bindings::menu,
} }
impl Menu { impl Menu {
pub fn new() -> Menu { pub fn new() -> Menu {
Self { Self {
menu: unsafe { bindings::menu_create(None) }, ctx: unsafe { bindings::menu_create(Some(print_item)) },
} }
} }
pub fn from_menu(menu: *mut bindings::menu) -> Self { fn into_inner(&mut self) -> &mut bindings::menu {
Self { menu } unsafe { &mut *self.ctx }
}
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
} }
pub fn bottom(&mut self, value: bool) -> &mut Self { pub fn bottom(&mut self, value: bool) -> &mut Self {
self.inner().bottom = value; self.into_inner().bottom = value;
self self
} }
pub fn password(&mut self, value: bool) -> &mut Self { pub fn password(&mut self, value: bool) -> &mut Self {
self.inner().passwd = value; self.into_inner().passwd = value;
self self
} }
pub fn font(&mut self, value: &str) -> &mut 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 self
} }
pub fn lines(&mut self, value: i32) -> &mut Self { pub fn lines(&mut self, value: i32) -> &mut Self {
self.inner().lines = value; self.into_inner().lines = value;
self self
} }
pub fn output_name(&mut self, value: &str) -> &mut 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 self
} }
pub fn prompt(&mut self, value: &str) -> &mut 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 self
} }
pub fn normal_bg(&mut self, value: u32) -> &mut Self { pub fn normal_bg(&mut self, value: u32) -> &mut Self {
self.inner().normalbg = value; self.into_inner().normalbg = value;
self self
} }
pub fn normal_fg(&mut self, value: u32) -> &mut Self { pub fn normal_fg(&mut self, value: u32) -> &mut Self {
self.inner().normalfg = value; self.into_inner().normalfg = value;
self self
} }
pub fn prompt_fg(&mut self, value: u32) -> &mut Self { pub fn prompt_fg(&mut self, value: u32) -> &mut Self {
self.inner().promptfg = value; self.into_inner().promptfg = value;
self self
} }
pub fn prompt_bg(&mut self, value: u32) -> &mut Self { pub fn prompt_bg(&mut self, value: u32) -> &mut Self {
self.inner().promptbg = value; self.into_inner().promptbg = value;
self self
} }
pub fn selection_bg(&mut self, value: u32) -> &mut Self { pub fn selection_bg(&mut self, value: u32) -> &mut Self {
self.inner().selectionbg = value; self.into_inner().selectionbg = value;
self self
} }
pub fn selection_fg(&mut self, value: u32) -> &mut Self { pub fn selection_fg(&mut self, value: u32) -> &mut Self {
self.inner().selectionbg = value; self.into_inner().selectionbg = value;
self self
} }
pub fn width(&mut self, value: i32) -> &mut Self { pub fn width(&mut self, value: i32) -> &mut Self {
self.inner().width = value; self.into_inner().width = value;
self self
} }
pub fn height(&mut self, value: i32) -> &mut Self { pub fn height(&mut self, value: i32) -> &mut Self {
self.inner().height = value; self.into_inner().height = value;
self self
} }
pub fn line_height(&mut self, value: i32) -> &mut Self { pub fn line_height(&mut self, value: i32) -> &mut Self {
self.inner().line_height = value; self.into_inner().line_height = value;
self self
} }
pub fn padding(&mut self, value: i32) -> &mut Self { pub fn padding(&mut self, value: i32) -> &mut Self {
self.inner().padding = value; self.into_inner().padding = value;
self self
} }
pub fn add_item(&mut self, item: &str) -> &mut Self { pub fn add_item(&mut self, item: &str) -> &mut Self {
unsafe { unsafe {
bindings::menu_add_item(self.menu, to_c_char(item)); bindings::menu_add_item(self.ctx, to_c_char(item));
} }
self self
@ -205,7 +185,7 @@ impl Menu {
let output = OutputCapture::capture().unwrap(); let output = OutputCapture::capture().unwrap();
unsafe { 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()), (0, Ok(Output::Stdout(value))) => Ok(value.trim_end().to_string()),
(n, Ok(Output::Stdout(value) | Output::Stderr(value))) => { (n, Ok(Output::Stdout(value) | Output::Stderr(value))) => {
Err(MenuError::new(n, value)) Err(MenuError::new(n, value))
@ -219,7 +199,7 @@ impl Menu {
impl Drop for Menu { impl Drop for Menu {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
bindings::menu_destroy(self.menu); bindings::menu_destroy(self.ctx);
} }
} }
} }
@ -232,7 +212,7 @@ mod tests {
fn awawa() { fn awawa() {
let items = ["roewrn", "sybil", "vex"]; let items = ["roewrn", "sybil", "vex"];
let mut menu = Menu::new(print_item); let mut menu = Menu::new();
let menu = menu let menu = menu
.font("monospace 12") .font("monospace 12")
.height(32) .height(32)