Skip to content

Commit 14aa21b

Browse files
committed
Fix navigation and refactoring
1 parent 9b882b4 commit 14aa21b

1 file changed

Lines changed: 29 additions & 19 deletions

File tree

src/main.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,49 @@ use std::fs;
66
use std::path::Path;
77

88
fn main() {
9-
let home = std::env::var("HOME").unwrap();
10-
let path = Path::new(&home).join(".ssh");
11-
let mut arr = Vec::new();
9+
let mut siv = cursive::default();
10+
show_file_selection(&mut siv);
11+
siv.run();
12+
}
1213

13-
for entry in fs::read_dir(path).expect("Unable to list") {
14-
let entry = entry.expect("unable to get entry");
14+
fn show_file_selection(siv: &mut Cursive) {
15+
let home = std::env::var("HOME").expect("HOME environment variable not set");
16+
let ssh_path = Path::new(&home).join(".ssh");
17+
let mut keys = Vec::new();
18+
19+
for entry in fs::read_dir(&ssh_path).expect("Unable to list directory") {
20+
let entry = entry.expect("Unable to read entry");
1521
let path = entry.path();
16-
let str = path.display().to_string();
22+
let file_path = path.display().to_string();
1723

18-
if !str.contains("known_hosts") {
19-
arr.push(str);
24+
if !file_path.contains("known_hosts") {
25+
keys.push(file_path);
2026
}
2127
}
2228

2329
let mut select = SelectView::new().h_align(HAlign::Center).autojump();
24-
25-
select.add_all_str(arr);
30+
select.add_all_str(keys);
2631
select.set_on_submit(show_next_window);
2732

28-
let mut siv = cursive::default();
29-
3033
siv.add_layer(
31-
Dialog::around(select.scrollable().fixed_size((40, 10))).title("My Keys"),
34+
Dialog::around(select.scrollable().fixed_size((40, 10)))
35+
.title("My SSH Keys")
36+
.button("Quit", |s| s.quit())
3237
);
33-
34-
siv.run();
3538
}
3639

37-
fn show_next_window(siv: &mut Cursive, str: &str) {
40+
fn show_next_window(siv: &mut Cursive, file_path: &str) {
3841
siv.pop_layer();
3942

40-
let contents = fs::read_to_string(str).expect("Should have been able to read the file");
41-
let text = contents;
43+
let contents = fs::read_to_string(file_path).expect("Unable to read the file");
44+
let text_view = TextView::new(contents);
4245

43-
siv.add_layer(Dialog::around(TextView::new(text)).button("Quit", |s| s.quit()));
46+
siv.add_layer(
47+
Dialog::around(text_view)
48+
.button("Back", move |s| {
49+
s.pop_layer();
50+
show_file_selection(s);
51+
})
52+
.button("Quit", |s| s.quit())
53+
);
4454
}

0 commit comments

Comments
 (0)