forked from extern/nushell
# Description Fixes #7546 's request. I'm unsure, so hopefully someone in charge of design can chip in. # User-Facing Changes `open` now opens directories in the default file manager. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
104 lines
3.2 KiB
Rust
104 lines
3.2 KiB
Rust
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
|
};
|
|
use std::path::Path;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Start;
|
|
|
|
impl Command for Start {
|
|
fn name(&self) -> &str {
|
|
"start"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Open a folder or file in the default application or viewer."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["load", "folder", "directory", "run", "open"]
|
|
}
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
Signature::build("start")
|
|
.input_output_types(vec![(Type::Nothing, Type::Any), (Type::String, Type::Any)])
|
|
.optional("filepath", SyntaxShape::Filepath, "the filepath to open")
|
|
.category(Category::FileSystem)
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
|
let path = call.opt::<Spanned<String>>(engine_state, stack, 0)?;
|
|
|
|
let path = {
|
|
if let Some(path_val) = path {
|
|
Some(Spanned {
|
|
item: nu_utils::strip_ansi_string_unlikely(path_val.item),
|
|
span: path_val.span,
|
|
})
|
|
} else {
|
|
path
|
|
}
|
|
};
|
|
|
|
let path = if let Some(path) = path {
|
|
path
|
|
} else {
|
|
// Collect a filename from the input
|
|
match input {
|
|
PipelineData::Value(Value::Nothing { .. }, ..) => {
|
|
return Err(ShellError::MissingParameter(
|
|
"needs filename".to_string(),
|
|
call.head,
|
|
))
|
|
}
|
|
PipelineData::Value(val, ..) => val.as_spanned_string()?,
|
|
_ => {
|
|
return Err(ShellError::MissingParameter(
|
|
"needs filename".to_string(),
|
|
call.head,
|
|
));
|
|
}
|
|
}
|
|
};
|
|
let path_no_whitespace = &path.item.trim_end_matches(|x| matches!(x, '\x09'..='\x0d'));
|
|
let path = Path::new(path_no_whitespace);
|
|
|
|
open::that(path)?;
|
|
Ok(PipelineData::Empty)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<nu_protocol::Example> {
|
|
vec![
|
|
Example {
|
|
description: "Open a text file with the default text editor",
|
|
example: "start file.txt",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Open an image with the default image viewer",
|
|
example: "start file.jpg",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Open the current directory with the default file manager",
|
|
example: "start .",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Open a pdf with the default pdf viewer",
|
|
example: "start file.pdf",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|