mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Keep index in file_list if possible
This commit is contained in:
@@ -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))),
|
||||||
|
|||||||
Reference in New Issue
Block a user