Skip to content

Commit

Permalink
[tuify] Refactor event loop code
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmulidris committed Dec 16, 2023
1 parent 62c564d commit 4694e1f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/dd_editor_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl EditorEngine {
}

pub struct EditorBuffer {
// TODO

}
```

Expand Down
24 changes: 9 additions & 15 deletions tuify/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub enum EventLoopResult {
Select,
}

// TODO: add performance using output buffer
pub fn enter_event_loop<W: Write, S: CalculateResizeHint>(
state: &mut S,
function_component: &mut impl FunctionComponent<W, S>,
Expand All @@ -41,15 +40,15 @@ pub fn enter_event_loop<W: Write, S: CalculateResizeHint>(
enable_raw_mode()?;

// Use to handle clean up.
#[allow(unused_assignments)]
let mut maybe_return_this: Option<EventLoopResult> = None;
let return_this: EventLoopResult;

// First render before blocking the main thread for user input.
function_component.render(state)?;

loop {
let key_event = read_key_press();
match on_keypress(state, key_event) {
let it = on_keypress(state, key_event);
match it {
EventLoopResult::ContinueAndRerenderAndClear => {
// Clear the viewport.
function_component.clear_viewport_for_resize(state)?;
Expand All @@ -65,31 +64,26 @@ pub fn enter_event_loop<W: Write, S: CalculateResizeHint>(
}
EventLoopResult::ExitWithResult(it) => {
// Break the loop and return the result.
maybe_return_this = Some(EventLoopResult::ExitWithResult(it));
return_this = EventLoopResult::ExitWithResult(it);
function_component.clear_viewport(state)?;
break;
}
EventLoopResult::ExitWithoutResult => {
// Break the loop and return the result.
maybe_return_this = Some(EventLoopResult::ExitWithoutResult);
return_this = EventLoopResult::ExitWithoutResult;
function_component.clear_viewport(state)?;
break;
}
EventLoopResult::ExitWithError => {
maybe_return_this = Some(EventLoopResult::ExitWithError);
return_this = EventLoopResult::ExitWithError;
function_component.clear_viewport(state)?;
break;
}
}
}

// Perform cleanup of raw mode, and show cursor.
match maybe_return_this {
Some(it) => {
execute!(function_component.get_write(), Show)?;
disable_raw_mode()?;
Ok(it)
}
None => Ok(EventLoopResult::ExitWithoutResult),
}
execute!(function_component.get_write(), Show)?;
disable_raw_mode()?;
Ok(return_this)
}
2 changes: 1 addition & 1 deletion tuify/src/giti/branch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod delete;
pub use delete::*;
pub use delete::*;
2 changes: 1 addition & 1 deletion tuify/src/giti/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod branch;
pub use branch::*;
pub use branch::*;

0 comments on commit 4694e1f

Please sign in to comment.