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:
- 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
- Now bookmarks and recents are sorted in the UI (bookmarks are sorted by name; recents are sorted by connection datetime)
## 0.3.2

View file

@ -41,18 +41,14 @@ impl AuthActivity {
pub(super) fn del_bookmark(&mut self, idx: usize) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() {
// Iterate over kyes
let mut name: Option<String> = None;
for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() {
if i == idx {
name = Some(key.clone());
break;
}
}
let name: Option<&String> = self.bookmarks_list.get(idx);
if let Some(name) = name {
bookmarks_cli.del_bookmark(&name);
// 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) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
// Iterate over bookmarks
for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() {
if i == idx {
if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) {
// Load parameters
self.address = bookmark.0;
self.port = bookmark.1.to_string();
self.protocol = bookmark.2;
self.username = bookmark.3;
if let Some(password) = bookmark.4 {
self.password = password;
}
if let Some(key) = self.bookmarks_list.get(idx) {
if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) {
// Load parameters
self.address = bookmark.0;
self.port = bookmark.1.to_string();
self.protocol = bookmark.2;
self.username = bookmark.3;
if let Some(password) = bookmark.4 {
self.password = password;
}
// Break
break;
}
}
}
@ -112,7 +104,7 @@ impl AuthActivity {
DialogYesNoOption::No => None,
};
bookmarks_cli.add_bookmark(
name,
name.clone(),
self.address.clone(),
port,
self.protocol,
@ -121,6 +113,9 @@ impl AuthActivity {
);
// Save bookmarks
self.write_bookmarks();
// Push bookmark to list and sort
self.bookmarks_list.push(name);
self.sort_bookmarks();
}
}
/// ### del_recent
@ -128,19 +123,14 @@ impl AuthActivity {
/// Delete recent
pub(super) fn del_recent(&mut self, idx: usize) {
if let Some(client) = self.bookmarks_client.as_mut() {
// Iterate over kyes
let mut name: Option<String> = None;
for (i, key) in client.iter_recents().enumerate() {
if i == idx {
name = Some(key.clone());
break;
}
}
let name: Option<&String> = self.recents_list.get(idx);
if let Some(name) = name {
client.del_recent(&name);
// Save bookmarks
// 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) {
if let Some(client) = self.bookmarks_client.as_ref() {
// Iterate over bookmarks
for (i, key) in client.iter_recents().enumerate() {
if i == idx {
if let Some(bookmark) = client.get_recent(key) {
// Load parameters
self.address = bookmark.0;
self.port = bookmark.1.to_string();
self.protocol = bookmark.2;
self.username = bookmark.3;
// Break
break;
}
if let Some(key) = self.recents_list.get(idx) {
if let Some(bookmark) = client.get_recent(key) {
// Load parameters
self.address = bookmark.0;
self.port = bookmark.1.to_string();
self.protocol = bookmark.2;
self.username = bookmark.3;
}
}
}
@ -233,7 +219,26 @@ impl AuthActivity {
config_dir_path.as_path(),
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) => {
self.popup = Some(Popup::Alert(
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> {
self.bookmarks_client.as_ref()?;
let hosts: Vec<ListItem> = self
.bookmarks_client
.as_ref()
.unwrap()
.iter_bookmarks()
.bookmarks_list
.iter()
.map(|key: &String| {
let entry: (String, u16, FileTransferProtocol, String, _) = self
.bookmarks_client
@ -368,10 +366,8 @@ impl AuthActivity {
fn draw_recents_tab(&self) -> Option<List> {
self.bookmarks_client.as_ref()?;
let hosts: Vec<ListItem> = self
.bookmarks_client
.as_ref()
.unwrap()
.iter_recents()
.recents_list
.iter()
.map(|key: &String| {
let entry: (String, u16, FileTransferProtocol, String) = self
.bookmarks_client

View file

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