summaryrefslogtreecommitdiff
path: root/src/widgets/prompt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/prompt.rs')
-rw-r--r--src/widgets/prompt.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/widgets/prompt.rs b/src/widgets/prompt.rs
new file mode 100644
index 0000000..0690840
--- /dev/null
+++ b/src/widgets/prompt.rs
@@ -0,0 +1,68 @@
+use std::ptr;
+
+use crate::bindings;
+use crate::util;
+
+pub struct Prompt {
+ ptr: *mut bindings::Promptwidget,
+}
+
+pub struct PromptBuilder<'a> {
+ lefttop: (u32, u32),
+ width: u32,
+ title: Option<&'a str>,
+}
+
+impl<'a> PromptBuilder<'a> {
+ pub fn add_title<'s: 'a>(&'a mut self, title: &'s str)
+ -> &'a mut PromptBuilder<'a> {
+ self.title = Some(title);
+ self
+ }
+
+ pub fn create(&self) -> Prompt {
+ Prompt {
+ ptr: unsafe {
+ bindings::prw_make(
+ self.lefttop.0 as i32, self.lefttop.1 as i32,
+ self.width as i32,
+ match self.title {
+ None => ptr::null(),
+ Some(s) => s.as_ptr() as *const i8,
+ })
+ }
+ }
+ }
+}
+
+impl Prompt {
+ pub fn new<'a>(lefttop: (u32, u32), width: u32) -> PromptBuilder<'a> {
+ PromptBuilder {
+ lefttop, width,
+ title: None,
+ }
+ }
+
+ pub fn redraw(&self) {
+ unsafe { bindings::prw_redraw(self.ptr); }
+ }
+
+ pub fn handle_key(&mut self, key: i32) -> Option<String> {
+ let ptr = unsafe { bindings::prw_handlekey(self.ptr, key) };
+ if ptr.is_null() {
+ None
+ } else {
+ unsafe { Some(util::string_from_utf8_charp(ptr)) }
+ }
+ }
+
+ pub fn change_title(&mut self, title: &str) {
+ unsafe { bindings::prw_changetitle(self.ptr, title.as_ptr() as *const i8); }
+ }
+}
+
+impl Drop for Prompt {
+ fn drop(&mut self) {
+ unsafe { bindings::prw_destroy(self.ptr); }
+ }
+}