Solved index of files list no more kept after 0.3.0 (use set_abs_index instead)

This commit is contained in:
ChristianVisintin
2021-01-16 11:35:33 +01:00
parent 03e1bf53d0
commit 928fc1b450
2 changed files with 44 additions and 4 deletions

View File

@@ -432,6 +432,19 @@ impl FileExplorer {
} }
} }
/// ### set_abs_index
///
/// Set absolute index
pub fn set_abs_index(&mut self, idx: usize) {
self.index = match idx >= self.files.len() {
true => match self.files.len() {
0 => 0,
_ => self.files.len() - 1,
},
false => idx,
};
}
/// ### toggle_hidden_files /// ### toggle_hidden_files
/// ///
/// Enable/disable hidden files /// Enable/disable hidden files
@@ -726,6 +739,33 @@ mod tests {
assert_eq!(explorer.files.get(1).unwrap().get_name(), "README.md"); assert_eq!(explorer.files.get(1).unwrap().get_name(), "README.md");
} }
#[test]
fn test_fs_explorer_set_abs_index() {
let mut explorer: FileExplorer = FileExplorer::default();
explorer.opts.remove(ExplorerOpts::SHOW_HIDDEN_FILES);
// Create files (files are then sorted by name DEFAULT)
explorer.set_files(vec![
make_fs_entry("README.md", false),
make_fs_entry("src/", true),
make_fs_entry(".git/", true),
make_fs_entry("CONTRIBUTING.md", false),
make_fs_entry("CODE_OF_CONDUCT.md", false),
make_fs_entry("CHANGELOG.md", false),
make_fs_entry("LICENSE", false),
make_fs_entry("Cargo.toml", false),
make_fs_entry("Cargo.lock", false),
make_fs_entry("codecov.yml", false),
make_fs_entry(".gitignore", false),
]);
explorer.set_abs_index(3);
assert_eq!(explorer.get_index(), 3);
explorer.set_abs_index(12);
assert_eq!(explorer.get_index(), 10);
explorer.set_files(vec![]);
explorer.set_abs_index(12);
assert_eq!(explorer.get_index(), 0);
}
#[test] #[test]
fn test_fs_explorer_sort_by_creation_time() { fn test_fs_explorer_sort_by_creation_time() {
let mut explorer: FileExplorer = FileExplorer::default(); let mut explorer: FileExplorer = FileExplorer::default();

View File

@@ -603,9 +603,9 @@ impl FileTransferActivity {
let prev_index: usize = self.local.get_index(); let prev_index: usize = self.local.get_index();
self.local.set_files(files); self.local.set_files(files);
// Restore index // Restore index
self.local.set_index(prev_index); self.local.set_abs_index(prev_index);
// Set index; keep if possible, otherwise set to last item // Set index; keep if possible, otherwise set to last item
self.local.set_index(match self.local.get_current_file() { self.local.set_abs_index(match self.local.get_current_file() {
Some(_) => self.local.get_index(), Some(_) => self.local.get_index(),
None => match self.local.count() { None => match self.local.count() {
0 => 0, 0 => 0,
@@ -632,9 +632,9 @@ impl FileTransferActivity {
let prev_index: usize = self.remote.get_index(); let prev_index: usize = self.remote.get_index();
self.remote.set_files(files); self.remote.set_files(files);
// Restore index // Restore index
self.remote.set_index(prev_index); self.remote.set_abs_index(prev_index);
// Set index; keep if possible, otherwise set to last item // Set index; keep if possible, otherwise set to last item
self.remote.set_index(match self.remote.get_current_file() { self.remote.set_abs_index(match self.remote.get_current_file() {
Some(_) => self.remote.get_index(), Some(_) => self.remote.get_index(),
None => match self.remote.count() { None => match self.remote.count() {
0 => 0, 0 => 0,