1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
use std::io;
use std::path;
use crate::commands::reload;
use crate::config::AppMimetypeEntry;
use crate::context::AppContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::ui::views::TuiTextField;
use crate::ui::TuiBackend;
use crate::util::process::{execute_and_wait, fork_execute};
use super::change_directory;
use crate::MIMETYPE_T;
pub fn get_options<'a>(path: &path::Path) -> Vec<&'a AppMimetypeEntry> {
let mut options: Vec<&AppMimetypeEntry> = Vec::new();
if let Some(file_ext) = path.extension() {
if let Some(file_ext) = file_ext.to_str() {
let ext_entries = MIMETYPE_T.app_list_for_ext(file_ext);
options.extend(ext_entries);
}
}
options
}
pub fn open(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult {
let curr_list = context.tab_context_ref().curr_tab_ref().curr_list_ref();
let entry = curr_list.and_then(|s| s.curr_entry_ref().cloned());
match entry {
None => (),
Some(entry) if entry.file_path().is_dir() => {
let path = entry.file_path().to_path_buf();
change_directory::cd(path.as_path(), context)?;
reload::soft_reload(context.tab_context_ref().index, context)?;
}
Some(entry) => {
let paths = curr_list.map_or_else(Vec::new, |s| s.iter_selected().cloned().collect());
let (path, files) = if paths.is_empty() {
(entry.file_path(), vec![entry.file_name()])
} else {
(
paths.get(0).unwrap().file_path(),
paths.iter().map(|e| e.file_name()).collect(),
)
};
let options = get_options(path);
let option = options.iter().find(|option| option.program_exists());
let config = context.config_ref();
if let Some(option) = option {
open_with_entry(context, backend, option, &files)?;
} else if config.xdg_open {
open_with_xdg(context, backend, path)?;
} else {
open_with_helper(context, backend, options, &files)?;
}
}
}
Ok(())
}
pub fn open_with_entry<S>(
context: &mut AppContext,
backend: &mut TuiBackend,
option: &AppMimetypeEntry,
files: &[S],
) -> std::io::Result<()>
where
S: AsRef<std::ffi::OsStr>,
{
if option.get_fork() {
let (child_id, handle) = fork_execute(option, files, context.clone_event_tx())?;
context.worker_context_mut().push_child(child_id, handle);
} else {
backend.terminal_drop();
execute_and_wait(option, files)?;
backend.terminal_restore()?;
}
Ok(())
}
pub fn open_with_xdg(
context: &mut AppContext,
backend: &mut TuiBackend,
path: &path::Path,
) -> std::io::Result<()> {
let config = context.config_ref();
if config.xdg_open_fork {
open::that_in_background(path);
} else {
backend.terminal_drop();
let result = open::that(path);
backend.terminal_restore()?;
result?;
}
Ok(())
}
pub fn open_with_helper<S>(
context: &mut AppContext,
backend: &mut TuiBackend,
options: Vec<&AppMimetypeEntry>,
files: &[S],
) -> std::io::Result<()>
where
S: AsRef<std::ffi::OsStr>,
{
const PROMPT: &str = "open_with ";
let user_input: Option<String> = {
context.flush_event();
let menu_options: Vec<String> = options
.iter()
.enumerate()
.map(|(i, e)| format!(" {} | {}", i, e))
.collect();
TuiTextField::default()
.prompt(":")
.prefix(PROMPT)
.menu_items(menu_options.iter().map(|s| s.as_str()))
.get_input(backend, context)
};
match user_input.as_ref() {
Some(user_input) if user_input.starts_with(PROMPT) => {
let user_input = &user_input[PROMPT.len()..];
match user_input.parse::<usize>() {
Ok(n) if n >= options.len() => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"option does not exist".to_string(),
))
}
Ok(n) => {
let option = &options[n];
open_with_entry(context, backend, option, files)?;
}
Err(_) => {
let mut args_iter = user_input.split_whitespace();
if let Some(cmd) = args_iter.next() {
backend.terminal_drop();
let mut option = AppMimetypeEntry::new(String::from(cmd));
option.args(args_iter);
let res = execute_and_wait(&option, files);
backend.terminal_restore()?;
res?
}
}
}
}
_ => {}
}
Ok(())
}
pub fn open_with_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult {
let mut paths = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.map_or(vec![], |s| s.iter_selected().cloned().collect());
if paths.is_empty() {
paths.push(
context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|s| s.curr_entry_ref())
.unwrap()
.clone(),
);
}
let files: Vec<&str> = paths.iter().map(|e| e.file_name()).collect();
let options = get_options(paths[0].file_path());
open_with_helper(context, backend, options, &files)?;
Ok(())
}
pub fn open_with_index(
context: &mut AppContext,
backend: &mut TuiBackend,
index: usize,
) -> JoshutoResult {
let paths = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.map_or(vec![], |s| s.iter_selected().cloned().collect());
if paths.is_empty() {
return Err(JoshutoError::new(
JoshutoErrorKind::Io(io::ErrorKind::NotFound),
String::from("No files selected"),
));
}
let files: Vec<&str> = paths.iter().map(|e| e.file_name()).collect();
let options = get_options(paths[0].file_path());
if index >= options.len() {
return Err(JoshutoError::new(
JoshutoErrorKind::Io(std::io::ErrorKind::InvalidData),
"option does not exist".to_string(),
));
}
let option = &options[index];
open_with_entry(context, backend, option, &files)?;
Ok(())
}
|