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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::commands::viewers::BAT_LANGUAGES;
use crate::prelude::*;
use encoding_rs::{Encoding, UTF_8};
use log::debug;
use nu_engine::StringOrBinary;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::{AnchorLocation, Span, Tagged};
use std::path::{Path, PathBuf};
pub struct Open;
impl WholeStreamCommand for Open {
fn name(&self) -> &str {
"open"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required(
"path",
SyntaxShape::FilePath,
"the file path to load values from",
)
.switch(
"raw",
"load content as a string instead of a table",
Some('r'),
)
.named(
"encoding",
SyntaxShape::String,
"encoding to use to open file",
Some('e'),
)
}
fn usage(&self) -> &str {
"Load a file into a cell, convert to table if possible (avoid by appending '--raw')."
}
fn extra_usage(&self) -> &str {
r#"Multiple encodings are supported for reading text files by using
the '--encoding <encoding>' parameter. Here is an example of a few:
big5, euc-jp, euc-kr, gbk, iso-8859-1, utf-16, cp1252, latin5
For a more complete list of encodings please refer to the encoding_rs
documentation link at https://docs.rs/encoding_rs/0.8.28/encoding_rs/#statics"#
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
open(args)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Opens \"users.csv\" and creates a table from the data",
example: "open users.csv",
result: None,
},
Example {
description: "Opens file with iso-8859-1 encoding",
example: "open file.csv --encoding iso-8859-1 | from csv",
result: None,
},
Example {
description: "Lists the contents of a directory (identical to `ls ../projectB`)",
example: "open ../projectB",
result: None,
},
]
}
}
pub fn get_encoding(opt: Option<Tagged<String>>) -> Result<&'static Encoding, ShellError> {
match opt {
None => Ok(UTF_8),
Some(label) => match Encoding::for_label((&label.item).as_bytes()) {
None => Err(ShellError::labeled_error(
format!(
r#"{} is not a valid encoding, refer to https://docs.rs/encoding_rs/0.8.23/encoding_rs/#statics for a valid list of encodings"#,
label.item
),
"invalid encoding",
label.span(),
)),
Some(encoding) => Ok(encoding),
},
}
}
fn open(args: CommandArgs) -> Result<ActionStream, ShellError> {
let scope = args.scope().clone();
let shell_manager = args.shell_manager();
let cwd = PathBuf::from(shell_manager.path());
let name = args.call_info.name_tag.clone();
let ctrl_c = args.ctrl_c();
let path: Tagged<PathBuf> = args.req(0)?;
let raw = args.has_flag("raw");
let encoding: Option<Tagged<String>> = args.get_flag("encoding")?;
if path.is_dir() {
let args = nu_engine::shell::LsArgs {
path: Some(path),
all: false,
long: false,
short_names: false,
du: false,
};
return shell_manager.ls(args, name, ctrl_c);
}
let ext = if raw {
None
} else {
path.extension()
.map(|name| name.to_string_lossy().to_string())
};
if let Some(ext) = ext {
if let Some(_command) = scope.get_command(&format!("from {}", ext)) {
let (_, tagged_contents) = crate::commands::open::fetch(
&cwd,
&PathBuf::from(&path.item),
path.tag.span,
encoding,
)?;
return Ok(ActionStream::one(ReturnSuccess::action(
CommandAction::AutoConvert(tagged_contents, ext),
)));
}
if BAT_LANGUAGES.contains(&ext.as_ref()) {
let (_, tagged_contents) = crate::commands::open::fetch(
&cwd,
&PathBuf::from(&path.item),
path.tag.span,
encoding,
)?;
return Ok(ActionStream::one(ReturnSuccess::value(tagged_contents)));
}
}
let with_encoding = if encoding.is_none() {
None
} else {
Some(get_encoding(encoding)?)
};
let sob_stream = shell_manager.open(&path.item, path.tag.span, with_encoding)?;
let final_stream = sob_stream.map(move |x| {
let file_tag = Tag {
span: path.tag.span,
anchor: Some(AnchorLocation::File(path.to_string_lossy().to_string())),
};
match x {
Ok(StringOrBinary::String(s)) => {
ReturnSuccess::value(UntaggedValue::string(s).into_value(file_tag))
}
Ok(StringOrBinary::Binary(b)) => ReturnSuccess::value(
UntaggedValue::binary(b.into_iter().collect()).into_value(file_tag),
),
Err(se) => Err(se),
}
});
Ok(ActionStream::new(final_stream))
}
pub fn fetch(
cwd: &Path,
location: &Path,
span: Span,
encoding_choice: Option<Tagged<String>>,
) -> Result<(Option<String>, Value), ShellError> {
let mut cwd = PathBuf::from(cwd);
cwd.push(location);
let nice_location = dunce::canonicalize(&cwd).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => ShellError::labeled_error(
format!("Cannot find file {:?}", cwd),
"cannot find file",
span,
),
std::io::ErrorKind::PermissionDenied => {
ShellError::labeled_error("Permission denied", "permission denied", span)
}
_ => ShellError::labeled_error(
format!("Cannot open file {:?} because {:?}", &cwd, e),
"Cannot open",
span,
),
})?;
let ext = location
.extension()
.map(|name| name.to_string_lossy().to_string());
let file_tag = Tag {
span,
anchor: Some(AnchorLocation::File(
nice_location.to_string_lossy().to_string(),
)),
};
let res = std::fs::read(location)
.map_err(|_| ShellError::labeled_error("Can't open filename given", "can't open", span))?;
let encoding = if encoding_choice.is_none() {
UTF_8
} else {
get_encoding(encoding_choice.clone())?
};
let decoded_res = if encoding_choice.is_some() {
let (cow_res, _replacements) = encoding.decode_with_bom_removal(&res);
cow_res
} else {
let (cow_res, actual_encoding, replacements) = encoding.decode(&res);
if replacements {
return Ok((ext, UntaggedValue::binary(res).into_value(file_tag)));
}
debug!("Decoded using {:?}", actual_encoding);
cow_res
};
let v = UntaggedValue::string(decoded_res.to_string()).into_value(file_tag);
Ok((ext, v))
}
#[cfg(test)]
mod tests {
use super::Open;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(Open {})
}
}