Now bookmarks and recents are sorted in the UI (bookmarks are sorted by name; recents are sorted by connection datetime)

This commit is contained in:
veeso
2021-02-25 16:15:06 +01:00
parent 669fd23868
commit af830d603d
4 changed files with 71 additions and 50 deletions

View File

@@ -20,6 +20,7 @@ Released on ???
- Enhancements: - Enhancements:
- Default choice for deleting file set to "NO" (way too easy to delete files by mistake) - Default choice for deleting file set to "NO" (way too easy to delete files by mistake)
- Added CLI options to set starting workind directory on both local and remote hosts - Added CLI options to set starting workind directory on both local and remote hosts
- Now bookmarks and recents are sorted in the UI (bookmarks are sorted by name; recents are sorted by connection datetime)
## 0.3.2 ## 0.3.2

View File

@@ -41,18 +41,14 @@ impl AuthActivity {
pub(super) fn del_bookmark(&mut self, idx: usize) { pub(super) fn del_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() { if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
// Iterate over kyes // Iterate over kyes
let mut name: Option<String> = None; let name: Option<&String> = self.bookmarks_list.get(idx);
for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() {
if i == idx {
name = Some(key.clone());
break;
}
}
if let Some(name) = name { if let Some(name) = name {
bookmarks_cli.del_bookmark(&name); bookmarks_cli.del_bookmark(&name);
// Write bookmarks // Write bookmarks
self.write_bookmarks(); self.write_bookmarks();
} }
// Delete element from vec
self.recents_list.remove(idx);
} }
} }
@@ -62,20 +58,16 @@ impl AuthActivity {
pub(super) fn load_bookmark(&mut self, idx: usize) { pub(super) fn load_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() { if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
// Iterate over bookmarks // Iterate over bookmarks
for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() { if let Some(key) = self.bookmarks_list.get(idx) {
if i == idx { if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) {
if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) { // Load parameters
// Load parameters self.address = bookmark.0;
self.address = bookmark.0; self.port = bookmark.1.to_string();
self.port = bookmark.1.to_string(); self.protocol = bookmark.2;
self.protocol = bookmark.2; self.username = bookmark.3;
self.username = bookmark.3; if let Some(password) = bookmark.4 {
if let Some(password) = bookmark.4 { self.password = password;
self.password = password;
}
} }
// Break
break;
} }
} }
} }
@@ -112,7 +104,7 @@ impl AuthActivity {
DialogYesNoOption::No => None, DialogYesNoOption::No => None,
}; };
bookmarks_cli.add_bookmark( bookmarks_cli.add_bookmark(
name, name.clone(),
self.address.clone(), self.address.clone(),
port, port,
self.protocol, self.protocol,
@@ -121,6 +113,9 @@ impl AuthActivity {
); );
// Save bookmarks // Save bookmarks
self.write_bookmarks(); self.write_bookmarks();
// Push bookmark to list and sort
self.bookmarks_list.push(name);
self.sort_bookmarks();
} }
} }
/// ### del_recent /// ### del_recent
@@ -128,19 +123,14 @@ impl AuthActivity {
/// Delete recent /// Delete recent
pub(super) fn del_recent(&mut self, idx: usize) { pub(super) fn del_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_mut() { if let Some(client) = self.bookmarks_client.as_mut() {
// Iterate over kyes let name: Option<&String> = self.recents_list.get(idx);
let mut name: Option<String> = None;
for (i, key) in client.iter_recents().enumerate() {
if i == idx {
name = Some(key.clone());
break;
}
}
if let Some(name) = name { if let Some(name) = name {
client.del_recent(&name); client.del_recent(&name);
// Save bookmarks // Write bookmarks
self.write_bookmarks(); self.write_bookmarks();
} }
// Delete element from vec
self.recents_list.remove(idx);
} }
} }
@@ -150,17 +140,13 @@ impl AuthActivity {
pub(super) fn load_recent(&mut self, idx: usize) { pub(super) fn load_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_ref() { if let Some(client) = self.bookmarks_client.as_ref() {
// Iterate over bookmarks // Iterate over bookmarks
for (i, key) in client.iter_recents().enumerate() { if let Some(key) = self.recents_list.get(idx) {
if i == idx { if let Some(bookmark) = client.get_recent(key) {
if let Some(bookmark) = client.get_recent(key) { // Load parameters
// Load parameters self.address = bookmark.0;
self.address = bookmark.0; self.port = bookmark.1.to_string();
self.port = bookmark.1.to_string(); self.protocol = bookmark.2;
self.protocol = bookmark.2; self.username = bookmark.3;
self.username = bookmark.3;
// Break
break;
}
} }
} }
} }
@@ -233,7 +219,26 @@ impl AuthActivity {
config_dir_path.as_path(), config_dir_path.as_path(),
16, 16,
) { ) {
Ok(cli) => self.bookmarks_client = Some(cli), Ok(cli) => {
// Load bookmarks into list
let mut bookmarks_list: Vec<String> =
Vec::with_capacity(cli.iter_bookmarks().count());
for bookmark in cli.iter_bookmarks() {
bookmarks_list.push(bookmark.clone());
}
// Load recents into list
let mut recents_list: Vec<String> =
Vec::with_capacity(cli.iter_recents().count());
for recent in cli.iter_recents() {
recents_list.push(recent.clone());
}
self.bookmarks_client = Some(cli);
self.bookmarks_list = bookmarks_list;
self.recents_list = recents_list;
// Sort bookmark list
self.sort_bookmarks();
self.sort_recents();
}
Err(err) => { Err(err) => {
self.popup = Some(Popup::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
@@ -256,4 +261,19 @@ impl AuthActivity {
} }
} }
} }
/// ### sort_bookmarks
///
/// Sort bookmarks in list
fn sort_bookmarks(&mut self) {
self.bookmarks_list.sort();
}
/// ### sort_recents
///
/// Sort recents in list
fn sort_recents(&mut self) {
// Reverse order
self.recents_list.sort_by(|a, b| b.cmp(a));
}
} }

View File

@@ -320,10 +320,8 @@ impl AuthActivity {
fn draw_bookmarks_tab(&self) -> Option<List> { fn draw_bookmarks_tab(&self) -> Option<List> {
self.bookmarks_client.as_ref()?; self.bookmarks_client.as_ref()?;
let hosts: Vec<ListItem> = self let hosts: Vec<ListItem> = self
.bookmarks_client .bookmarks_list
.as_ref() .iter()
.unwrap()
.iter_bookmarks()
.map(|key: &String| { .map(|key: &String| {
let entry: (String, u16, FileTransferProtocol, String, _) = self let entry: (String, u16, FileTransferProtocol, String, _) = self
.bookmarks_client .bookmarks_client
@@ -368,10 +366,8 @@ impl AuthActivity {
fn draw_recents_tab(&self) -> Option<List> { fn draw_recents_tab(&self) -> Option<List> {
self.bookmarks_client.as_ref()?; self.bookmarks_client.as_ref()?;
let hosts: Vec<ListItem> = self let hosts: Vec<ListItem> = self
.bookmarks_client .recents_list
.as_ref() .iter()
.unwrap()
.iter_recents()
.map(|key: &String| { .map(|key: &String| {
let entry: (String, u16, FileTransferProtocol, String) = self let entry: (String, u16, FileTransferProtocol, String) = self
.bookmarks_client .bookmarks_client

View File

@@ -115,7 +115,9 @@ pub struct AuthActivity {
input_txt: String, // Input text input_txt: String, // Input text
choice_opt: DialogYesNoOption, // Dialog popup selected option choice_opt: DialogYesNoOption, // Dialog popup selected option
bookmarks_idx: usize, // Index of selected bookmark bookmarks_idx: usize, // Index of selected bookmark
bookmarks_list: Vec<String>, // List of bookmarks
recents_idx: usize, // Index of selected recent recents_idx: usize, // Index of selected recent
recents_list: Vec<String>, // list of recents
} }
impl Default for AuthActivity { impl Default for AuthActivity {
@@ -149,7 +151,9 @@ impl AuthActivity {
input_txt: String::new(), input_txt: String::new(),
choice_opt: DialogYesNoOption::Yes, choice_opt: DialogYesNoOption::Yes,
bookmarks_idx: 0, bookmarks_idx: 0,
bookmarks_list: Vec::new(),
recents_idx: 0, recents_idx: 0,
recents_list: Vec::new(),
} }
} }