Skip to content

Commit 10ea1d8

Browse files
committed
Populate files again after running command
1 parent 12733e5 commit 10ea1d8

File tree

7 files changed

+107
-92
lines changed

7 files changed

+107
-92
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fpick"
3-
version = "0.7.4"
3+
version = "0.7.5"
44
license = "MIT"
55
edition = "2021"
66
description = "Interactive file picker"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@ Set alias for quick access:
9696
```sh
9797
alias cfp='cd "$(fpick)"'
9898
```
99+
100+
Move file by interactively picking the source and destination:
101+
```sh
102+
mv "$(fpick)" "$(fpick)"
103+
```

src/action_menu.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ pub enum Operation {
3838

3939
pub fn generate_known_actions() -> Vec<MenuAction> {
4040
vec![
41+
MenuAction {
42+
name: "Pick absolute path",
43+
operation: Operation::PickAbsolutePath,
44+
},
45+
MenuAction {
46+
name: "Pick relative path",
47+
operation: Operation::PickRelativePath,
48+
},
4149
MenuAction {
4250
name: "View",
4351
operation: Operation::ViewContent,
@@ -80,14 +88,6 @@ pub fn generate_known_actions() -> Vec<MenuAction> {
8088
name: "Create directory",
8189
operation: Operation::CreateDir,
8290
},
83-
MenuAction {
84-
name: "Pick absolute path",
85-
operation: Operation::PickAbsolutePath,
86-
},
87-
MenuAction {
88-
name: "Pick relative path",
89-
operation: Operation::PickRelativePath,
90-
},
9191
MenuAction {
9292
name: "Copy absolute path to clipboard",
9393
operation: Operation::CopyToClipboard {

src/app_logic/app_logic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ impl App {
389389
self.error_message = None;
390390
}
391391

392+
pub fn show_error(&mut self, message: String) {
393+
self.error_message = Some(message);
394+
}
395+
392396
pub fn has_info(&self) -> bool {
393397
self.info_message.is_some()
394398
}

src/app_logic/logic_action_menu.rs

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ impl App {
3939
let current_dir_path: String = self.get_current_dir_abs_path();
4040

4141
let action: &MenuAction = &self.known_menu_actions[self.action_menu_cursor_y];
42+
self.window_focus = WindowFocus::Tree;
43+
self.action_menu_operation = Some(action.operation.clone());
4244
match action.operation {
4345
Operation::ShellCommand { template } => {
44-
self.window_focus = WindowFocus::Tree;
45-
let res = execute_shell_operation(&abs_path, template);
46-
if res.is_err() {
47-
self.error_message = Some(res.err().unwrap().to_string());
46+
let result = execute_shell_operation(&abs_path, template);
47+
match result {
48+
Err(err) => self.show_error(err.to_string()),
49+
_ => {}
4850
}
4951
}
5052
Operation::InteractiveShellCommand { template } => {
51-
let res = execute_interactive_shell_operation(&abs_path, template, tui);
52-
if res.is_err() {
53-
self.error_message = Some(res.err().unwrap().to_string());
53+
let result = execute_interactive_shell_operation(&abs_path, template, tui);
54+
match result {
55+
Err(err) => self.show_error(err.to_string()),
56+
_ => {}
5457
}
55-
self.window_focus = WindowFocus::Tree;
5658
}
5759
Operation::PickAbsolutePath => {
5860
self.pick_selected_node(Some(false));
@@ -62,133 +64,130 @@ impl App {
6264
}
6365
Operation::Rename => {
6466
let filename = abs_path.split('/').last().unwrap().to_string();
65-
self.window_focus = WindowFocus::ActionMenuStep2;
66-
self.action_menu_operation = Some(action.operation.clone());
67-
self.action_menu_title = format!("New name for {}", filename);
68-
self.action_menu_buffer = filename;
69-
self.action_menu_cursor_x = self.action_menu_buffer.chars().count();
67+
self.open_action_menu_step2(format!("New name for {}", filename), filename);
7068
}
7169
Operation::CreateFile => {
72-
self.window_focus = WindowFocus::ActionMenuStep2;
73-
self.action_menu_operation = Some(action.operation.clone());
74-
self.action_menu_title = format!("New file at {}", current_dir_path);
75-
self.action_menu_buffer = "".to_string();
76-
self.action_menu_cursor_x = self.action_menu_buffer.chars().count();
70+
self.open_action_menu_step2(
71+
format!("New file at {}", current_dir_path),
72+
String::new(),
73+
);
7774
}
7875
Operation::CreateDir => {
79-
self.window_focus = WindowFocus::ActionMenuStep2;
80-
self.action_menu_operation = Some(action.operation.clone());
81-
self.action_menu_title = format!("New directory at {}", current_dir_path);
82-
self.action_menu_buffer = "".to_string();
83-
self.action_menu_cursor_x = self.action_menu_buffer.chars().count();
76+
self.open_action_menu_step2(
77+
format!("New directory at {}", current_dir_path),
78+
String::new(),
79+
);
80+
}
81+
Operation::CustomCommand => {
82+
self.open_action_menu_step2(
83+
format!("Run command at {}", current_dir_path),
84+
format!("\"{}\"", abs_path),
85+
);
86+
}
87+
Operation::CustomInteractiveCommand => {
88+
self.open_action_menu_step2(
89+
format!("Run interactive command at {}", current_dir_path),
90+
format!("\"{}\"", abs_path),
91+
);
8492
}
8593
Operation::Delete => {
86-
let res = delete_tree_node(&tree_node, &abs_path);
87-
if res.is_err() {
88-
self.error_message = Some(res.err().unwrap().to_string());
94+
let result = delete_tree_node(&tree_node, &abs_path);
95+
match result {
96+
Err(err) => self.show_error(err.to_string()),
97+
_ => {}
8998
}
90-
self.window_focus = WindowFocus::Tree;
9199
}
92100
Operation::CopyToClipboard { is_relative_path } => {
93-
self.window_focus = WindowFocus::Tree;
94-
let res = match is_relative_path {
101+
let result = match is_relative_path {
95102
true => copy_path_to_clipboard(&relative_path.unwrap()),
96103
false => copy_path_to_clipboard(&abs_path),
97104
};
98-
if res.is_err() {
99-
self.error_message = Some(res.err().unwrap().to_string());
105+
match result {
106+
Err(err) => self.show_error(err.to_string()),
107+
_ => {}
100108
}
101109
}
102110
Operation::FileDetails => {
103-
self.window_focus = WindowFocus::Tree;
104-
let res = get_file_details(&abs_path, is_directory);
105-
if res.is_err() {
106-
self.error_message = Some(res.err().unwrap().to_string());
107-
} else {
108-
self.show_info(res.unwrap());
111+
let result = get_file_details(&abs_path, is_directory);
112+
match result {
113+
Ok(info) => self.show_info(info),
114+
Err(err) => self.show_error(err.to_string()),
109115
}
110116
}
111-
Operation::CustomCommand => {
112-
self.window_focus = WindowFocus::ActionMenuStep2;
113-
self.action_menu_operation = Some(action.operation.clone());
114-
self.action_menu_title = format!("Run command at {}", current_dir_path);
115-
self.action_menu_buffer = format!("\"{}\"", abs_path);
116-
self.action_menu_cursor_x = self.action_menu_buffer.chars().count();
117-
}
118-
Operation::CustomInteractiveCommand => {
119-
self.window_focus = WindowFocus::ActionMenuStep2;
120-
self.action_menu_operation = Some(action.operation.clone());
121-
self.action_menu_title = format!("Run interactive command at {}", current_dir_path);
122-
self.action_menu_buffer = format!("\"{}\"", abs_path);
123-
self.action_menu_cursor_x = self.action_menu_buffer.chars().count();
124-
}
125117
Operation::ViewContent => {
126-
self.window_focus = WindowFocus::Tree;
127118
if !is_directory {
128-
let res = read_file_content(&abs_path);
129-
if res.is_err() {
130-
self.error_message = Some(res.err().unwrap().to_string());
131-
} else {
132-
self.show_info(res.unwrap());
119+
let result = read_file_content(&abs_path);
120+
match result {
121+
Ok(info) => self.show_info(info),
122+
Err(err) => self.show_error(err.to_string()),
133123
}
134124
}
135125
}
136126
}
137127
self.populate_current_child_nodes();
138128
}
139129

130+
fn open_action_menu_step2(&mut self, title: String, buffer: String) {
131+
self.window_focus = WindowFocus::ActionMenuStep2;
132+
self.action_menu_title = title;
133+
self.action_menu_buffer = buffer;
134+
self.action_menu_cursor_x = self.action_menu_buffer.chars().count();
135+
}
136+
140137
pub fn execute_dialog_action_step2(&mut self, tui: &mut Tui) {
141138
let abs_path: String = match self.get_selected_abs_path() {
142139
Some(abs_path) => abs_path,
143140
None => return,
144141
};
145142
if self.action_menu_buffer.is_empty() {
146-
self.error_message = Some("No value given".to_string());
143+
self.show_error("No value given".to_string());
147144
return;
148145
}
149146
let current_dir_path: String = self.get_current_dir_abs_path();
150147

151148
match self.action_menu_operation {
152149
Some(Operation::Rename) => {
153-
let res = rename_file(&abs_path, &self.action_menu_buffer);
154-
if res.is_err() {
155-
self.error_message = Some(res.err().unwrap().to_string());
150+
let result = rename_file(&abs_path, &self.action_menu_buffer);
151+
match result {
152+
Err(err) => self.show_error(err.to_string()),
153+
_ => {}
156154
}
157155
}
158156
Some(Operation::CreateFile) => {
159157
let full_path = format!("{}/{}", current_dir_path, &self.action_menu_buffer);
160-
let res = create_file(&full_path);
161-
if res.is_err() {
162-
self.error_message = Some(res.err().unwrap().to_string());
158+
let result = create_file(&full_path);
159+
match result {
160+
Err(err) => self.show_error(err.to_string()),
161+
_ => {}
163162
}
164163
}
165164
Some(Operation::CreateDir) => {
166165
let full_path = format!("{}/{}", current_dir_path, &self.action_menu_buffer);
167-
let res = create_directory(&full_path);
168-
if res.is_err() {
169-
self.error_message = Some(res.err().unwrap().to_string());
166+
let result = create_directory(&full_path);
167+
match result {
168+
Err(err) => self.show_error(err.to_string()),
169+
_ => {}
170170
}
171171
}
172172
Some(Operation::CustomCommand) => {
173173
let current_dir_path = current_dir_path.clone();
174174
let action_menu_buffer = self.action_menu_buffer.clone();
175-
self.info_message =
176-
Some(format!("Running command...\n{}", &self.action_menu_buffer));
175+
self.show_info(format!("Running command...\n{}", &self.action_menu_buffer));
177176
let result_tx = self.background_event_channel.tx.clone();
178177
std::thread::spawn(move || {
179-
let res = run_custom_command(current_dir_path, &action_menu_buffer);
180-
match res {
178+
let result = run_custom_command(current_dir_path, &action_menu_buffer);
179+
match result {
181180
Ok(output) => result_tx.send(BackgroundEvent::InfoMessage(output)),
182181
Err(err) => result_tx.send(BackgroundEvent::ErrorMessage(err.to_string())),
183182
}
184183
});
185184
}
186185
Some(Operation::CustomInteractiveCommand) => {
187-
let res =
186+
let result =
188187
run_custom_interactive_command(current_dir_path, &self.action_menu_buffer, tui);
189-
match res {
188+
match result {
190189
Ok(output) => self.show_info(output),
191-
Err(err) => self.error_message = Some(err.to_string()),
190+
Err(err) => self.show_error(err.to_string()),
192191
}
193192
}
194193
_ => {}

src/background.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ pub enum BackgroundEvent {
88

99
impl App {
1010
pub fn check_background_events(&mut self) {
11-
self.background_event_channel.rx.try_recv().ok().map(
12-
|event: BackgroundEvent| match event {
13-
BackgroundEvent::InfoMessage(message) => self.show_info(message),
14-
BackgroundEvent::ErrorMessage(message) => {
15-
self.error_message = Some(message);
16-
}
17-
},
18-
);
11+
self.background_event_channel
12+
.rx
13+
.try_recv()
14+
.ok()
15+
.map(|event: BackgroundEvent| {
16+
self.consume_background_event(event);
17+
});
18+
}
19+
20+
fn consume_background_event(&mut self, event: BackgroundEvent) {
21+
match event {
22+
BackgroundEvent::InfoMessage(message) => self.show_info(message),
23+
BackgroundEvent::ErrorMessage(message) => self.show_error(message),
24+
};
25+
self.populate_current_child_nodes();
1926
}
2027
}

0 commit comments

Comments
 (0)