aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Gattozzi <mgattozzi@gmail.com>2020-01-05 23:19:42 -0500
committerMichael Gattozzi <mgattozzi@gmail.com>2020-01-05 23:19:42 -0500
commit7dfa1b8dc9fb6d1fef974e7c29a32bff3146c71d (patch)
treedfa9ea68e30ca43cb99a1043ff37315e7a21eb85
parent03238ddf40330db173b00439139e6e74958b7eb1 (diff)
Fix the ticket tui so it does not panic
The tui for ticket uses indexing in order to have quicker unchecked access into data structures for the data needed that gets displayed in the tui. However, you can't index empty data. We add a check for this in places that expect data to exist so that nothing will cause a panic. We also add fixes to handle the fact that you can't divide by zero in our index modifying logic when pressing the left/right/up/down arrow keys. With this the tui shouldn't panic anymore and can work in an empty state. Closes: 4c37e800-2e38-11ea-b6e0-32f54a3ad7cd 'Having no tickets causes the TUI to crash'
-rw-r--r--.dev-suite/ticket/closed/having-no-tickets-causes-the-tui-to-crash.toml (renamed from .dev-suite/ticket/open/having-no-tickets-causes-the-tui-to-crash.toml)2
-rw-r--r--ticket/src/tui.rs62
2 files changed, 39 insertions, 25 deletions
diff --git a/.dev-suite/ticket/open/having-no-tickets-causes-the-tui-to-crash.toml b/.dev-suite/ticket/closed/having-no-tickets-causes-the-tui-to-crash.toml
index e4ffd42..960ee5d 100644
--- a/.dev-suite/ticket/open/having-no-tickets-causes-the-tui-to-crash.toml
+++ b/.dev-suite/ticket/closed/having-no-tickets-causes-the-tui-to-crash.toml
@@ -1,5 +1,5 @@
title = 'Having no tickets causes the TUI to crash'
-status = 'Open'
+status = 'Closed'
id = '4c37e800-2e38-11ea-b6e0-32f54a3ad7cd'
assignees = [[
'64c00ccc-761e-4484-86ac-904e461bb300',
diff --git a/ticket/src/tui.rs b/ticket/src/tui.rs
index df62106..e20c112 100644
--- a/ticket/src/tui.rs
+++ b/ticket/src/tui.rs
@@ -86,11 +86,13 @@ impl<'a> TabsState<'a> {
}
pub fn next(&mut self) {
- self.index = (self.index + 1) % self.titles.len()
+ if !self.titles.is_empty() {
+ self.index = (self.index + 1) % self.titles.len()
+ }
}
pub fn previous(&mut self) {
- if self.index > 0 {
+ if self.index > 0 && !self.titles.is_empty() {
self.index = (self.index - 1) % self.titles.len()
}
}
@@ -123,11 +125,13 @@ impl TicketState {
}
pub fn next(&mut self) {
- self.index = (self.index + 1) % self.len()
+ if self.len() > 0 {
+ self.index = (self.index + 1) % self.len()
+ }
}
pub fn previous(&mut self) {
- if self.index > 0 {
+ if self.index > 0 && self.len() > 0 {
self.index = (self.index - 1) % self.len()
}
}
@@ -331,28 +335,35 @@ fn handle_event(
KeyCode::Up => app.tickets.previous(),
KeyCode::Down => app.tickets.next(),
KeyCode::Backspace => {
- let _ = app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index]
- .1
- .pop();
+ if app.tickets.len() > 0 {
+ let _ = app.tickets.tickets.get_mut(status).unwrap()
+ [app.tickets.index]
+ .1
+ .pop();
+ }
}
KeyCode::Char(c) => {
- app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index]
- .1
- .push(c);
+ if app.tickets.len() > 0 {
+ app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index]
+ .1
+ .push(c);
+ }
}
KeyCode::Enter => {
- let ticket =
- &mut app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index];
- if !ticket.1.is_empty() {
- let _ = ticket.0.comments.insert(
- uuid_v1()?,
- (
- user_config.uuid,
- Name(user_config.name.clone()),
- Comment(ticket.1.clone()),
- ),
- );
- ticket.1.clear();
+ if app.tickets.len() > 0 {
+ let ticket = &mut app.tickets.tickets.get_mut(status).unwrap()
+ [app.tickets.index];
+ if !ticket.1.is_empty() {
+ let _ = ticket.0.comments.insert(
+ uuid_v1()?,
+ (
+ user_config.uuid,
+ Name(user_config.name.clone()),
+ Comment(ticket.1.clone()),
+ ),
+ );
+ ticket.1.clear();
+ }
}
}
_ => {}
@@ -450,9 +461,12 @@ impl<'a> App<'a> {
#[inline]
fn comment(&self, tab: &'a str, f: &mut Frame<impl Backend>, rect: Rect) {
- let (_, s) = &self.tickets.tickets.get(tab).unwrap()[self.tickets.index];
+ let tickets = self.tickets.tickets.get(tab).unwrap();
let mut text = String::from("> ");
- text.push_str(&s);
+ if !tickets.is_empty() {
+ let (_, s) = &tickets[self.tickets.index];
+ text.push_str(&s);
+ }
Paragraph::new([Text::raw(text)].iter())
.block(Block::default().borders(Borders::ALL).title("Comment"))