Skip to content

Commit cf6ac01

Browse files
committed
browser: Sort the preview directories
Signed-off-by: Howard Chu <[email protected]>
1 parent 25476c4 commit cf6ac01

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

Diff for: src/browser.rs

+24-30
Original file line numberDiff line numberDiff line change
@@ -170,40 +170,33 @@ impl Browser {
170170

171171
/// Get directory content preview window as a vector of strings
172172
fn get_preview(&self) -> Vec<String> {
173-
let mut ret: Vec<String> = Vec::new();
173+
let empty: Vec<String> = Vec::new();
174174

175175
if self.current_dir.len() == 0 {
176-
return ret;
176+
return empty;
177177
}
178178

179-
let mut dir_under_cursor = self.current_path.clone();
180-
dir_under_cursor.push(&self.current_dir[self.cursor]);
181-
182-
if dir_under_cursor.is_dir() == false {
183-
return ret;
179+
let mut _dir = self.current_path.clone();
180+
_dir.push(&self.current_dir[self.cursor]);
181+
if _dir.is_dir() == false {
182+
return empty;
184183
}
185184

186-
if let Ok(entries) = read_dir(&dir_under_cursor) {
187-
for entry in entries {
188-
let entry = entry.expect(&format!(
189-
"Failed to interate through {}",
190-
dir_under_cursor.to_str().unwrap()
191-
));
192-
193-
let s = entry.file_name().into_string();
194-
195-
match s {
196-
Ok(v) => {
197-
ret.push(v);
198-
}
199-
Err(_) => {
200-
let str = entry.file_name().to_string_lossy().into_owned();
201-
ret.push(str);
202-
}
203-
}
204-
}
205-
}
206-
ret
185+
let dir = _dir.to_str().expect("Failed to construct preview path");
186+
187+
let mut preview = read_dir(&dir)
188+
.expect(&format!("Failed to read files from {}", dir))
189+
.map(|e| {
190+
e.expect("Failed to get preview entry")
191+
.file_name()
192+
.into_string()
193+
.expect("Failed to get preview file name")
194+
})
195+
.collect::<Vec<String>>();
196+
197+
preview.sort_by(|d1, d2| d1.to_lowercase().cmp(&d2.to_lowercase()));
198+
199+
preview
207200
}
208201

209202
/// Set cursor position, centered in the window
@@ -452,7 +445,7 @@ impl Browser {
452445
/// Read the file and directory names in the current directory
453446
fn read_current_dir(&mut self, path: &String) {
454447
self.current_dir = read_dir(path)
455-
.expect(&format!("Read files from {} failed", path))
448+
.expect(&format!("Failed to read files from {}", path))
456449
.map(|_e| {
457450
let e = _e.expect(&format!("Failed to read a file from {}", path));
458451
match e.file_name().into_string() {
@@ -462,7 +455,8 @@ impl Browser {
462455
})
463456
.collect::<Vec<String>>();
464457

465-
self.current_dir.sort_by(|d1, d2| d1.to_lowercase().cmp(&d2.to_lowercase()));
458+
self.current_dir
459+
.sort_by(|d1, d2| d1.to_lowercase().cmp(&d2.to_lowercase()));
466460
}
467461

468462
/// Goto the directory in the left side window, while quitting trans

0 commit comments

Comments
 (0)