Skip to content

Commit

Permalink
content added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Apr 29, 2024
1 parent 30a4c5a commit f9635ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
16 changes: 4 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;
#[derive(Debug)]
pub struct App {
pub running: bool,
pub counter: u8,
pub content: String,
}

impl Default for App {
fn default() -> Self {
Self {
running: true,
counter: 0,
content: "".into(),
}
}
}
Expand All @@ -28,15 +28,7 @@ impl App {
self.running = false;
}

pub fn increment_counter(&mut self) {
if let Some(res) = self.counter.checked_add(1) {
self.counter = res;
}
}

pub fn decrement_counter(&mut self) {
if let Some(res) = self.counter.checked_sub(1) {
self.counter = res;
}
pub fn append_str(&mut self, text: char) {
self.content.push(text)
}
}
10 changes: 4 additions & 6 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.quit();
}
}
KeyCode::Right => {
app.increment_counter();
}
KeyCode::Left => {
app.decrement_counter();
_ => {
if let KeyCode::Char(c) = key_event.code {
app.append_str(c)
}
}
_ => {}
}
Ok(())
}
4 changes: 2 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn render(app: &mut App, frame: &mut Frame) {
"This is a tui template.\n\
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\
Press left and right to increment and decrement the counter respectively.\n\
Counter: {}",
app.counter
Content:\n {}",
app.content
))
.block(
Block::bordered()
Expand Down

0 comments on commit f9635ed

Please sign in to comment.