Keep index in file_list if possible

This commit is contained in:
veeso
2021-03-21 12:06:42 +01:00
parent 16a8fc3ad8
commit 7caa4575bd

View File

@@ -91,11 +91,13 @@ impl OwnStates {
} }
} }
/// ### reset_list_index /// ### fix_list_index
/// ///
/// Reset list index to 0 /// Keep index if possible, otherwise set to lenght - 1
pub fn reset_list_index(&mut self) { pub fn fix_list_index(&mut self) {
self.list_index = 0; if self.list_index >= self.list_len {
self.list_index = self.list_len - 1;
}
} }
} }
@@ -189,8 +191,8 @@ impl Component for FileList {
Some(tokens) => tokens.len(), Some(tokens) => tokens.len(),
None => 0, None => 0,
}); });
// Reset list index // Fix list index
self.states.reset_list_index(); self.states.fix_list_index();
Msg::None Msg::None
} }
@@ -319,26 +321,26 @@ mod tests {
.build(), .build(),
); );
// Verify states // Verify states
assert_eq!(component.states.list_index, 0); assert_eq!(component.states.list_index, 1); // Kept
assert_eq!(component.states.list_len, 3); assert_eq!(component.states.list_len, 3);
// get value // get value
assert_eq!(component.get_value(), Payload::Unsigned(0)); assert_eq!(component.get_value(), Payload::Unsigned(1));
// Render // Render
assert_eq!(component.states.list_index, 0); assert_eq!(component.states.list_index, 1);
// Handle inputs // Handle inputs
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Down))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::Down))),
Msg::None Msg::None
); );
// Index should be incremented // Index should be incremented
assert_eq!(component.states.list_index, 1); assert_eq!(component.states.list_index, 2);
// Index should be decremented // Index should be decremented
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Up))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::Up))),
Msg::None Msg::None
); );
// Index should be incremented // Index should be incremented
assert_eq!(component.states.list_index, 0); assert_eq!(component.states.list_index, 1);
// Index should be 2 // Index should be 2
assert_eq!( assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageDown))), component.on(InputEvent::Key(KeyEvent::from(KeyCode::PageDown))),