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
|
use std::io;
use std::process::Command;
use crate::context::{AppContext, LocalStateContext};
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread};
fn new_local_state(context: &mut AppContext, file_op: FileOperation) -> Option<()> {
let list = context.tab_context_ref().curr_tab_ref().curr_list_ref()?;
let selected = list.get_selected_paths();
let mut local_state = LocalStateContext::new();
local_state.set_paths(selected.into_iter());
local_state.set_file_op(file_op);
context.set_local_state(local_state);
Some(())
}
pub fn cut(context: &mut AppContext) -> JoshutoResult {
new_local_state(context, FileOperation::Cut);
Ok(())
}
pub fn copy(context: &mut AppContext) -> JoshutoResult {
new_local_state(context, FileOperation::Copy);
Ok(())
}
pub fn symlink_absolute(context: &mut AppContext) -> JoshutoResult {
new_local_state(context, FileOperation::Symlink { relative: false });
Ok(())
}
pub fn symlink_relative(context: &mut AppContext) -> JoshutoResult {
new_local_state(context, FileOperation::Symlink { relative: true });
Ok(())
}
pub fn paste(context: &mut AppContext, options: FileOperationOptions) -> JoshutoResult {
match context.take_local_state() {
Some(state) if !state.paths.is_empty() => {
let dest = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
let worker_thread = IoWorkerThread::new(state.file_op, state.paths, dest, options);
context.worker_context_mut().push_worker(worker_thread);
Ok(())
}
_ => Err(JoshutoError::new(
JoshutoErrorKind::Io(io::ErrorKind::InvalidData),
"no files selected".to_string(),
)),
}
}
pub fn copy_filename(context: &mut AppContext) -> JoshutoResult {
let entry_file_name = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
.map(|entry| entry.file_name().to_string());
if let Some(file_name) = entry_file_name {
copy_string_to_buffer(file_name)?;
}
Ok(())
}
pub fn copy_filename_without_extension(context: &mut AppContext) -> JoshutoResult {
let entry_file_name = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
.map(|entry| {
entry
.file_name()
.rsplit_once('.')
.map(|(name, _)| name.to_string())
.unwrap_or_else(|| entry.file_name().to_string())
});
if let Some(file_name) = entry_file_name {
copy_string_to_buffer(file_name)?;
}
Ok(())
}
pub fn copy_filepath(context: &mut AppContext) -> JoshutoResult {
let entry_file_path = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
.and_then(|entry| entry.file_path().to_str())
.map(|s| s.to_string());
if let Some(file_path) = entry_file_path {
copy_string_to_buffer(file_path)?;
}
Ok(())
}
pub fn copy_dirpath(context: &mut AppContext) -> JoshutoResult {
let opt_entry = context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.map(|dirlist| dirlist.file_path());
if let Some(s) = opt_entry.and_then(|p| p.to_str().map(String::from)) {
copy_string_to_buffer(s)?
};
Ok(())
}
fn copy_string_to_buffer(string: String) -> JoshutoResult {
let clipboards = [
(
"wl-copy",
format!("printf '%s' '{}' | {} 2> /dev/null", string, "wl-copy"),
),
(
"xsel",
format!("printf '%s' '{}' | {} -ib 2> /dev/null", string, "xsel"),
),
(
"pbcopy",
format!("printf '%s' '{}' | {} 2> /dev/null", string, "pbcopy"),
),
(
"xclip",
format!(
"printf '%s' '{}' | {} -selection clipboard 2> /dev/null",
string, "xclip"
),
),
];
for (_, command) in clipboards.iter() {
match Command::new("sh").args(&["-c", command.as_str()]).status() {
Ok(s) if s.success() => return Ok(()),
_ => {}
}
}
Err(JoshutoError::new(
JoshutoErrorKind::ClipboardError,
"Failed to copy to clipboard".to_string(),
))
}
|