diff options
author | DLFW <daniel@llin.info> | 2022-08-30 19:46:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 13:46:22 -0400 |
commit | 826e24eaf4a10921bdd087079c4d7330896b79c2 (patch) | |
tree | ecce44e4243f7b58d110e071759a5ec32c6be2fb /src/history.rs | |
parent | edf620b4aceaaadffedd14d85d120d485a6ad8f3 (diff) |
Add "Visual-Mode" (#192)
* Add "Visual-Mode"
This adds a "visual-mode" file selection feature where a range-selection follows the file-cursor.
Description of usage is added to the docs.
Also, the "normal" and the new "visual-mode-selection" are now preserved
when a `DirList` is reloaded.
Wrap-up of changes:
* Add command `toggle_visual`, mapped to `V`
* Add command `escape`, mapped to `ESCAPE`
* Add style `[visual_mode_selection]` for file entries which are
"temporarily" selected by visual-mode
* For `JoshutoDirEntry`, the attribute `selected` has been renamed to
`permanent_selected`, and a second selection-attribute
`visual_mode_selected` has been added. "Setters" and "getters" have
been adapted/added accordingly. The former "getter" for the `selecetd`
attribute still exists and returns `True` for an entry which is
"permanant selected" _or_ "visual-mode selected". So any higher logic
which acts on selected files does not need to care about "how" and
entry is selected.
* Each `JoshutoDirList` has an optional index of the file where
visual-mode has been entered and re-calculates the "visual-mode
selecetd" status for each entry any time the cursor-index changes.
* The footer has been extended so it shows a "VIS" marker when the user
is in visual-mode.
This implementation of visual-mode is a bit different from the ranger
one, where the visual-selection is turned into a "normal selection" when
a command (like `copy-files`) is issued. This implementation keeps
both selections separate until the user toggles back to "normal mode".
Only then the visual-selection is taken over to the "normal selection".
The user also can withdraw the visual-selection with `escape`.
The `escape` command may be used also for other "reset"-actions in the
future.
* fix syntax for Rust stable
* cargo clippy
Diffstat (limited to 'src/history.rs')
-rw-r--r-- | src/history.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/history.rs b/src/history.rs index 42c5b0a..162c3bd 100644 --- a/src/history.rs +++ b/src/history.rs @@ -156,6 +156,8 @@ pub fn create_dirlist_with_history( ) -> io::Result<JoshutoDirList> { let filter_func = options.filter_func(); let mut contents = read_directory(path, filter_func, options)?; + + // re-use directory size information on reload for entry in contents.iter_mut() { if entry.metadata.is_dir() { if let Some(lst) = history.get(entry.file_path()) { @@ -164,6 +166,19 @@ pub fn create_dirlist_with_history( } } + // preserve selection status of entries on reload + if let Some(former_dir_list) = history.get(path) { + let former_entries_by_file_name = HashMap::<&str, &JoshutoDirEntry>::from_iter( + former_dir_list.contents.iter().map(|e| (e.file_name(), e)), + ); + for entry in contents.iter_mut() { + if let Some(former_entry) = former_entries_by_file_name.get(entry.file_name()) { + entry.set_permanent_selected(former_entry.is_permanent_selected()); + entry.set_visual_mode_selected(former_entry.is_visual_mode_selected()); + } + } + } + let sort_options = tab_options.sort_options_ref(); contents.sort_by(|f1, f2| sort_options.compare(f1, f2)); @@ -199,6 +214,20 @@ pub fn create_dirlist_with_history( None => 0, } }; + let visual_mode_anchor_index = match history.get(path) { + None => None, + Some(dirlist) => { + dirlist + .get_visual_mode_anchor_index() + .map(|old_visual_mode_anchor_index| { + if old_visual_mode_anchor_index < contents_len { + old_visual_mode_anchor_index + } else { + contents_len - 1 + } + }) + } + }; let metadata = JoshutoMetadata::from(path)?; let dirlist = JoshutoDirList::new( @@ -206,6 +235,7 @@ pub fn create_dirlist_with_history( contents, index, viewport_index, + visual_mode_anchor_index, metadata, ); |