Skip to content

Commit

Permalink
Fix: invalid handling of empty channels list.
Browse files Browse the repository at this point in the history
When channels list is empty, up/down and default selection used
invalid index 0.
  • Loading branch information
boxdot committed Aug 3, 2020
1 parent 99c3869 commit 8b976bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl App {
data
}
};
if data.channels.state.selected().is_none() {
if data.channels.state.selected().is_none() && !data.channels.items.is_empty() {
data.channels.state.select(Some(0));
data.save(&config.data_path)?;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ fn draw_messages<B: Backend>(f: &mut Frame<B>, app: &App, area: Rect) {
.channels
.state
.selected()
.map(|idx| &app.data.channels.items[idx].messages[..])
.and_then(|idx| app.data.channels.items.get(idx))
.map(|channel| &channel.messages[..])
.unwrap_or(&[]);

let max_username_width = messages
Expand Down
16 changes: 14 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ impl<T> StatefulList<T> {
i + 1
}
}
None => 0,
None => {
if !self.items.is_empty() {
0
} else {
return; // nothing to select
}
}
};
self.state.select(Some(i));
}
Expand All @@ -39,7 +45,13 @@ impl<T> StatefulList<T> {
i - 1
}
}
None => 0,
None => {
if !self.items.is_empty() {
0
} else {
return; // nothing to select
}
}
};
self.state.select(Some(i));
}
Expand Down

0 comments on commit 8b976bc

Please sign in to comment.