Skip to content

Commit 49fa7f4

Browse files
committed
fix(tui): prevent panic in HelpBrowserState when sections empty
Changed current_section() to return Option<&HelpSection> and use safe .get() access instead of direct indexing. Updated render_content() to handle None gracefully by early returning, and updated tests to use .expect() for assertions. Fixes #5149
1 parent 66f1b2c commit 49fa7f4

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/cortex-tui/src/widgets/help_browser/render.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ impl<'a> HelpBrowser<'a> {
152152

153153
/// Renders the content pane.
154154
fn render_content(&self, area: Rect, buf: &mut Buffer) {
155-
let section = self.state.current_section();
155+
let Some(section) = self.state.current_section() else {
156+
return;
157+
};
156158
let mut y = area.y;
157159
let scroll = self.state.content_scroll;
158160
let mut line_idx = 0;

src/cortex-tui/src/widgets/help_browser/state.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,13 @@ impl HelpBrowserState {
148148
}
149149

150150
/// Returns the currently selected section.
151-
pub fn current_section(&self) -> &HelpSection {
152-
&self.sections[self.selected_section]
151+
///
152+
/// Returns `None` if the sections vector is empty.
153+
pub fn current_section(&self) -> Option<&HelpSection> {
154+
if self.sections.is_empty() {
155+
return None;
156+
}
157+
self.sections.get(self.selected_section)
153158
}
154159

155160
/// Handles character input for search.

src/cortex-tui/src/widgets/help_browser/tests.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mod tests {
4343
#[test]
4444
fn test_help_browser_state_with_topic() {
4545
let state = HelpBrowserState::new().with_topic(Some("keyboard"));
46-
assert_eq!(state.current_section().id, "keyboard");
46+
assert_eq!(state.current_section().expect("should have section").id, "keyboard");
4747
}
4848

4949
#[test]
@@ -220,10 +220,10 @@ mod tests {
220220
#[test]
221221
fn test_current_section() {
222222
let mut state = HelpBrowserState::new();
223-
assert_eq!(state.current_section().id, "getting-started");
223+
assert_eq!(state.current_section().expect("should have section").id, "getting-started");
224224

225225
state.select_next();
226-
assert_eq!(state.current_section().id, "keyboard");
226+
assert_eq!(state.current_section().expect("should have section").id, "keyboard");
227227
}
228228

229229
#[test]
@@ -232,4 +232,11 @@ mod tests {
232232
assert!(!state.sections.is_empty());
233233
assert_eq!(state.selected_section, 0);
234234
}
235-
}
235+
236+
#[test]
237+
fn test_current_section_empty_sections() {
238+
let mut state = HelpBrowserState::new();
239+
state.sections.clear();
240+
assert!(state.current_section().is_none());
241+
}
242+
}

0 commit comments

Comments
 (0)