Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/05_edit_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +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()
}
}
field.handle_event(rcui, event)
},
EditField::new(),
))
}
50 changes: 49 additions & 1 deletion src/edit_field.rs
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
// TODO(#10): EditField is not implemented
use super::*;

#[derive(Default)]
pub struct EditField {
text: String,
buffer: Vec<u8>,
// TODO(#34): EditField does not have any cursor functionality
}

impl EditField {
pub fn new() -> Self {
Self {
text: String::new(),
buffer: Vec::new(),
}
}

pub fn wrap() -> Box<Self> {
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);
// TODO(#35): EditField does not wrap during the rendering
addstr(&self.text);
}

fn handle_event(&mut self, _context: &mut Rcui, event: &Event) {
// 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) {
Ok(s) => {
self.text.push_str(&s);
self.buffer.clear()
}
Err(_) => {
if self.buffer.len() >= 4 {
self.buffer.clear()
}
}
}
}
}
}
1 change: 0 additions & 1 deletion src/item_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl<T: ToString + Clone> Widget for ItemList<T> {
};

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;
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ impl Rcui {
self.event_queue.push_back(event);
}

// TODO(#36): no support for nested event loops via Rcui::exec()

pub fn exec(mut ui: Box<dyn Widget>) {
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);
Expand All @@ -103,8 +109,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 {
Expand Down