Skip to content

Commit 967f95f

Browse files
committed
Refactor code to improve readability and error handling
1 parent 8dbfa78 commit 967f95f

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/app_logic.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,11 @@ impl App {
8383
}
8484

8585
pub fn get_current_string_path(&self) -> String {
86-
let mut all_names = self
86+
let all_names = self
8787
.parent_nodes
8888
.iter()
8989
.map(|node| node.name.to_string())
9090
.collect::<Vec<String>>();
91-
if !self.starting_dir.is_empty() {
92-
all_names.insert(0, self.starting_dir.clone());
93-
}
9491
if all_names.is_empty() {
9592
return "/".to_string();
9693
}
@@ -103,8 +100,9 @@ impl App {
103100

104101
let nodes_result = list_files(std::path::Path::new(&path));
105102
if nodes_result.is_err() {
106-
self.error_message = Some(nodes_result.unwrap_err().to_string());
103+
self.error_message = Some(contextualized_error(&nodes_result.unwrap_err()));
107104
self.child_nodes = vec![];
105+
self.render_tree_nodes();
108106
return;
109107
}
110108
let mut nodes = nodes_result.unwrap();
@@ -185,16 +183,12 @@ impl App {
185183
}
186184
let selected_node: FileNode = selected_node_o.unwrap();
187185

188-
let mut all_names = self
189-
.parent_nodes
190-
.iter()
191-
.map(|node| node.name.to_string())
192-
.collect::<Vec<String>>();
193-
if !self.starting_dir.is_empty() {
194-
all_names.insert(0, self.starting_dir.clone());
195-
}
196-
all_names.push(selected_node.name.to_string());
197-
let selected_path = normalize_path(all_names.join("/"));
186+
let current_path = self.get_current_string_path();
187+
let selected_path = normalize_path(format!(
188+
"{}/{}",
189+
current_path,
190+
selected_node.name.to_string()
191+
));
198192

199193
self.picked_path = Some(selected_path);
200194
self.quit();

src/filesystem.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub enum FileType {
2020
}
2121

2222
pub fn list_files(dir_path: &Path) -> Result<Vec<FileNode>> {
23-
let dir_entries: ReadDir = fs::read_dir(dir_path).context("failed to read directory")?;
23+
let dir_entries: ReadDir = fs::read_dir(dir_path)
24+
.with_context(|| format!("failed to read directory '{}'", dir_path.to_string_lossy()))?;
2425

2526
let files: Vec<FileNode> = dir_entries
2627
.filter_map(|entry| {
@@ -74,7 +75,12 @@ pub fn get_path_file_nodes(path: &String) -> Result<Vec<FileNode>> {
7475
true => PathBuf::from("."),
7576
false => PathBuf::from(&path),
7677
};
77-
let absolute = fs::canonicalize(&start_pathbuf).context("evaluating absolute path")?;
78+
let absolute = fs::canonicalize(&start_pathbuf).with_context(|| {
79+
format!(
80+
"evaluating absolute path '{}'",
81+
start_pathbuf.to_string_lossy()
82+
)
83+
})?;
7884
let path_parts: Vec<&str> = absolute.to_str().unwrap().split('/').collect();
7985

8086
let nodes: Vec<FileNode> = path_parts

src/ui.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,30 @@ fn render_error_popup(app: &mut App, frame: &mut Frame) {
7878
.bg(Color::Red)
7979
.border_type(BorderType::Rounded);
8080

81-
let widget = Paragraph::new(error_message)
81+
let error_window = Paragraph::new(error_message)
8282
.wrap(Wrap { trim: true })
8383
.block(title)
8484
.style(Style::default().fg(Color::White));
8585

86+
let ok_label = Paragraph::new("OK")
87+
.style(Style::default().bold().fg(Color::LightRed).bg(Color::White))
88+
.alignment(Alignment::Center);
89+
8690
let width: u16 = (frame.size().width as f32 * 0.75f32) as u16;
8791
let height: u16 = frame.size().height / 2;
8892
let area = centered_rect(width, height, frame.size());
93+
94+
let ok_label_area = Rect {
95+
x: area.x + 1,
96+
y: area.y + area.height - 2,
97+
width: area.width - 2,
98+
height: 1,
99+
};
100+
89101
let buffer = frame.buffer_mut();
90102
Clear.render(area, buffer);
91-
frame.render_widget(widget, area);
103+
frame.render_widget(error_window, area);
104+
frame.render_widget(ok_label, ok_label_area);
92105
}
93106

94107
fn centered_rect(w: u16, h: u16, r: Rect) -> Rect {

0 commit comments

Comments
 (0)