From bd35af790757dc7a4e65b747689442ed662f992a Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:40:27 +0700 Subject: [PATCH 01/10] (#10) Introduce initial EditField implementation --- examples/05_edit_field.rs | 17 ++++++++++++++ src/edit_field.rs | 47 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 23 +++++++++++++++++-- 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 examples/05_edit_field.rs diff --git a/examples/05_edit_field.rs b/examples/05_edit_field.rs new file mode 100644 index 0000000..71fc677 --- /dev/null +++ b/examples/05_edit_field.rs @@ -0,0 +1,17 @@ +use rcui::*; + +fn main() { + Rcui::exec( + Proxy::wrap( + |field, rcui, event| { + if let Event::KeyStroke(key) = event { + if *key as u8 as char == '\n' { + rcui.quit() + } + } + field.handle_event(rcui, event) + }, + EditField::new() + ) + ) +} diff --git a/src/edit_field.rs b/src/edit_field.rs index 52fea3e..a891136 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -1 +1,48 @@ // TODO(#10): EditField is not implemented + +use super::*; + +pub struct EditField { + text: String, + buffer: Vec, + cursor: usize, +} + +impl EditField { + pub fn new() -> Self { + Self { + text: String::new(), + buffer: Vec::new(), + cursor: 0 + } + } + + pub fn wrap() -> Box { + Box::new(Self::new()) + } +} + +impl Widget for EditField { + fn render(&mut self, _context: &mut Rcui, rect: &Rect, _active: bool) { + let x = rect.x.floor() as i32; + let y = rect.y.floor() as i32; + mv(y, x); + addstr(&self.text); + } + + fn handle_event(&mut self, _context: &mut Rcui, event: &Event) { + // TODO: move the utf8 buffer mechanism to the main event loop + if let Event::KeyStroke(key) = event { + self.buffer.push(*key as u8); + match std::str::from_utf8(&self.buffer) { + Ok(s) => { + self.text.push_str(&s); + self.buffer.clear() + } + Err(_) => if self.buffer.len() >= 4 { + self.buffer.clear() + } + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 5d6184d..552855e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,11 +74,18 @@ impl Rcui { self.event_queue.push_back(event); } + // TODO: no support for nested event loops via Rcui::exec() + pub fn exec(mut ui: Box) { let mut context = Self::new(); + let locale_conf = LcCategory::all; + setlocale(locale_conf, "en_US.UTF-8"); + + initscr(); keypad(stdscr(), true); + timeout(10); start_color(); init_pair(style::REGULAR_PAIR, COLOR_WHITE, COLOR_BLACK); @@ -103,8 +110,20 @@ impl Rcui { } erase(); ui.render(&mut context, &screen_rect(), true); - let key = getch(); - context.push_event(Event::KeyStroke(key)); + + // Busy waiting on the key event + let mut key = getch(); + while key == ERR { + key = getch(); + } + + // Flushing everything we've got + while key != ERR { + context.push_event(Event::KeyStroke(key)); + key = getch(); + } + + // Handling all of the events from the queue while !context.event_queue.is_empty() { if let Some(event) = context.event_queue.pop_front() { if let Event::Quit = event { From 0e8632025b2cefd33a2c047c155c61263dfb3dc4 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:44:07 +0700 Subject: [PATCH 02/10] Remove TODO(#10) --- src/edit_field.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/edit_field.rs b/src/edit_field.rs index a891136..38b3aa5 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -1,5 +1,3 @@ -// TODO(#10): EditField is not implemented - use super::*; pub struct EditField { From 9d69ac0b2c859d35c983b3c26a94fb169b9f7dc3 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:44:12 +0700 Subject: [PATCH 03/10] (#10) Remove cursor field from EditField It's not used yet anyway --- src/edit_field.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/edit_field.rs b/src/edit_field.rs index 38b3aa5..f571bf5 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -3,7 +3,7 @@ use super::*; pub struct EditField { text: String, buffer: Vec, - cursor: usize, + // TODO: EditField does not have any cursor functionality } impl EditField { @@ -11,7 +11,6 @@ impl EditField { Self { text: String::new(), buffer: Vec::new(), - cursor: 0 } } @@ -25,6 +24,7 @@ impl Widget for EditField { let x = rect.x.floor() as i32; let y = rect.y.floor() as i32; mv(y, x); + // TODO: EditField does not wrap during the rendering addstr(&self.text); } From 889ae529abd7e77acfe08326741efc96ffc530dc Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:44:57 +0700 Subject: [PATCH 04/10] (#10) Fix cargo clippy remarks --- src/edit_field.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/edit_field.rs b/src/edit_field.rs index f571bf5..1814272 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -1,5 +1,6 @@ use super::*; +#[derive(Default)] pub struct EditField { text: String, buffer: Vec, From 8579e31497626ce5917e833669d7c42742b88255 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:45:14 +0700 Subject: [PATCH 05/10] (#10) Fix cargo fmt remarks --- examples/05_edit_field.rs | 22 ++++++++++------------ src/edit_field.rs | 6 ++++-- src/lib.rs | 1 - 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/examples/05_edit_field.rs b/examples/05_edit_field.rs index 71fc677..c16b570 100644 --- a/examples/05_edit_field.rs +++ b/examples/05_edit_field.rs @@ -1,17 +1,15 @@ use rcui::*; fn main() { - Rcui::exec( - Proxy::wrap( - |field, rcui, event| { - if let Event::KeyStroke(key) = event { - if *key as u8 as char == '\n' { - rcui.quit() - } + Rcui::exec(Proxy::wrap( + |field, rcui, event| { + if let Event::KeyStroke(key) = event { + if *key as u8 as char == '\n' { + rcui.quit() } - field.handle_event(rcui, event) - }, - EditField::new() - ) - ) + } + field.handle_event(rcui, event) + }, + EditField::new(), + )) } diff --git a/src/edit_field.rs b/src/edit_field.rs index 1814272..a3d68f8 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -38,8 +38,10 @@ impl Widget for EditField { self.text.push_str(&s); self.buffer.clear() } - Err(_) => if self.buffer.len() >= 4 { - self.buffer.clear() + Err(_) => { + if self.buffer.len() >= 4 { + self.buffer.clear() + } } } } diff --git a/src/lib.rs b/src/lib.rs index 552855e..c8c5c4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,6 @@ impl Rcui { let locale_conf = LcCategory::all; setlocale(locale_conf, "en_US.UTF-8"); - initscr(); keypad(stdscr(), true); timeout(10); From fa0ceb470fe73fa3d91f247be1888f48fbc64c52 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:46:36 +0700 Subject: [PATCH 06/10] Add TODO(#34) --- src/edit_field.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edit_field.rs b/src/edit_field.rs index a3d68f8..80185d2 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -4,7 +4,7 @@ use super::*; pub struct EditField { text: String, buffer: Vec, - // TODO: EditField does not have any cursor functionality + // TODO(#34): EditField does not have any cursor functionality } impl EditField { From d074cc00257bb8ede30b8ff9efd6aa08a603bae3 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:46:37 +0700 Subject: [PATCH 07/10] Add TODO(#35) --- src/edit_field.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edit_field.rs b/src/edit_field.rs index 80185d2..0ed5eb4 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -25,7 +25,7 @@ impl Widget for EditField { let x = rect.x.floor() as i32; let y = rect.y.floor() as i32; mv(y, x); - // TODO: EditField does not wrap during the rendering + // TODO(#35): EditField does not wrap during the rendering addstr(&self.text); } From 71c89c06c7391c243dfba13e4643287c3a52dc07 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:46:37 +0700 Subject: [PATCH 08/10] Add TODO(#36) --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index c8c5c4e..d72fc7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ impl Rcui { self.event_queue.push_back(event); } - // TODO: no support for nested event loops via Rcui::exec() + // TODO(#36): no support for nested event loops via Rcui::exec() pub fn exec(mut ui: Box) { let mut context = Self::new(); From 37d04dcb7f23a4c075047622f80ec21d5da5ef0c Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:46:38 +0700 Subject: [PATCH 09/10] Add TODO(#37) --- src/edit_field.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edit_field.rs b/src/edit_field.rs index 0ed5eb4..94b378d 100644 --- a/src/edit_field.rs +++ b/src/edit_field.rs @@ -30,7 +30,7 @@ impl Widget for EditField { } fn handle_event(&mut self, _context: &mut Rcui, event: &Event) { - // TODO: move the utf8 buffer mechanism to the main event loop + // TODO(#37): move the utf8 buffer mechanism to the main event loop if let Event::KeyStroke(key) = event { self.buffer.push(*key as u8); match std::str::from_utf8(&self.buffer) { From 1fa9e84fd8f9abf4ea8bea7c4c720cbe93e03a07 Mon Sep 17 00:00:00 2001 From: rexim Date: Tue, 1 Dec 2020 19:47:03 +0700 Subject: [PATCH 10/10] Remove TODO(#17) --- src/item_list.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/item_list.rs b/src/item_list.rs index fa0e61a..26216e6 100644 --- a/src/item_list.rs +++ b/src/item_list.rs @@ -82,7 +82,6 @@ impl Widget for ItemList { }; attron(COLOR_PAIR(color_pair)); - // TODO(#17): ItemList should extend cursor to the whole available width let x = rect.x.floor() as i32; let y = (rect.y + i as f32).floor() as i32; let w = rect.w.floor() as usize;