nushell/src/commands/view.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

2019-05-18 03:24:13 +02:00
use crate::errors::ShellError;
use crate::prelude::*;
use prettyprint::PrettyPrinter;
pub fn view(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-06-22 05:43:37 +02:00
if args.len() == 0 {
2019-06-15 20:36:17 +02:00
return Err(ShellError::maybe_labeled_error(
"View requires a filename",
"needs parameter",
args.name_span,
));
2019-06-08 00:35:07 +02:00
}
2019-06-22 05:43:37 +02:00
let target = match args.expect_nth(0)?.as_string() {
2019-06-08 00:35:07 +02:00
Ok(s) => s.clone(),
Err(e) => {
if let Some(span) = args.name_span {
return Err(ShellError::labeled_error(
"Expected a string",
"not a filename",
span,
));
} else {
return Err(e);
}
}
2019-05-22 09:12:03 +02:00
};
2019-05-18 03:24:13 +02:00
2019-06-13 23:47:25 +02:00
let cwd = args
.env
.lock()
.unwrap()
2019-06-15 19:52:55 +02:00
.front()
2019-06-13 23:47:25 +02:00
.unwrap()
.path()
.to_path_buf();
2019-05-18 03:24:13 +02:00
2019-05-22 09:12:03 +02:00
let printer = PrettyPrinter::default()
.line_numbers(false)
.header(false)
.grid(false)
.build()
.map_err(|e| ShellError::string(e))?;
2019-05-18 03:24:13 +02:00
2019-06-23 00:20:13 +02:00
let file = cwd.join(&target);
2019-05-18 03:24:13 +02:00
2019-05-22 09:12:03 +02:00
let _ = printer.file(file.display().to_string());
2019-05-18 03:24:13 +02:00
Ok(VecDeque::new().into())
2019-05-18 03:24:13 +02:00
}