Compare commits

...

4 commits

View file

@ -1,8 +1,9 @@
use gag::BufferRedirect; use gag::BufferRedirect;
use std::{ use std::{
error::Error, error::Error,
ffi::{c_char, CString}, ffi::{c_char, CStr, CString},
io::Read, io::Read,
str::Utf8Error,
}; };
use wmenu_sys::bindings; use wmenu_sys::bindings;
@ -16,10 +17,16 @@ 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),
@ -70,104 +77,117 @@ 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 {
ctx: *mut bindings::menu, menu: *mut bindings::menu,
} }
impl Menu { impl Menu {
pub fn new() -> Menu { pub fn new() -> Menu {
Self { Self {
ctx: unsafe { bindings::menu_create(Some(print_item)) }, menu: unsafe { bindings::menu_create(None) },
} }
} }
fn into_inner(&mut self) -> &mut bindings::menu { pub fn from_menu(menu: *mut bindings::menu) -> Self {
unsafe { &mut *self.ctx } 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
} }
pub fn bottom(&mut self, value: bool) -> &mut Self { pub fn bottom(&mut self, value: bool) -> &mut Self {
self.into_inner().bottom = value; self.inner().bottom = value;
self self
} }
pub fn password(&mut self, value: bool) -> &mut Self { pub fn password(&mut self, value: bool) -> &mut Self {
self.into_inner().passwd = value; self.inner().passwd = value;
self self
} }
pub fn font(&mut self, value: &str) -> &mut Self { pub fn font(&mut self, value: &str) -> &mut Self {
self.into_inner().font = to_c_char(value); self.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.into_inner().lines = value; self.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.into_inner().output_name = to_c_char(value); self.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.into_inner().prompt = to_c_char(value); self.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.into_inner().normalbg = value; self.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.into_inner().normalfg = value; self.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.into_inner().promptfg = value; self.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.into_inner().promptbg = value; self.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.into_inner().selectionbg = value; self.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.into_inner().selectionbg = value; self.inner().selectionbg = value;
self self
} }
pub fn width(&mut self, value: i32) -> &mut Self { pub fn width(&mut self, value: i32) -> &mut Self {
self.into_inner().width = value; self.inner().width = value;
self self
} }
pub fn height(&mut self, value: i32) -> &mut Self { pub fn height(&mut self, value: i32) -> &mut Self {
self.into_inner().height = value; self.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.into_inner().line_height = value; self.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.into_inner().padding = value; self.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.ctx, to_c_char(item)); bindings::menu_add_item(self.menu, to_c_char(item));
} }
self self
@ -185,7 +205,7 @@ impl Menu {
let output = OutputCapture::capture().unwrap(); let output = OutputCapture::capture().unwrap();
unsafe { unsafe {
match (bindings::menu_run(self.ctx), output.get_output()) { match (bindings::menu_run(self.menu), 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))
@ -199,7 +219,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.ctx); bindings::menu_destroy(self.menu);
} }
} }
} }
@ -212,7 +232,7 @@ mod tests {
fn awawa() { fn awawa() {
let items = ["roewrn", "sybil", "vex"]; let items = ["roewrn", "sybil", "vex"];
let mut menu = Menu::new(); let mut menu = Menu::new(print_item);
let menu = menu let menu = menu
.font("monospace 12") .font("monospace 12")
.height(32) .height(32)