mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
let start
open anything and everything (#7580)
# 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.
This commit is contained in:
@ -30,6 +30,7 @@ mod with_column;
|
||||
|
||||
use nu_protocol::engine::StateWorkingSet;
|
||||
|
||||
pub use self::open::OpenDataFrame;
|
||||
pub use append::AppendDF;
|
||||
pub use columns::ColumnsDF;
|
||||
pub use drop::DropDF;
|
||||
@ -43,7 +44,6 @@ pub use get::GetDF;
|
||||
pub use last::LastDF;
|
||||
pub use list::ListDF;
|
||||
pub use melt::MeltDF;
|
||||
pub use open::OpenDataFrame;
|
||||
pub use query_df::QueryDf;
|
||||
pub use rename::RenameDF;
|
||||
pub use sample::SampleDF;
|
||||
|
@ -275,6 +275,7 @@ pub fn create_default_context() -> EngineState {
|
||||
Mkdir,
|
||||
Mv,
|
||||
Open,
|
||||
Start,
|
||||
Rm,
|
||||
Save,
|
||||
Touch,
|
||||
|
@ -8,10 +8,12 @@ mod mv;
|
||||
mod open;
|
||||
mod rm;
|
||||
mod save;
|
||||
mod start;
|
||||
mod touch;
|
||||
mod util;
|
||||
mod watch;
|
||||
|
||||
pub use self::open::Open;
|
||||
pub use cd::Cd;
|
||||
pub use cd_query::query;
|
||||
pub use cp::Cp;
|
||||
@ -19,9 +21,9 @@ pub use glob::Glob;
|
||||
pub use ls::Ls;
|
||||
pub use mkdir::Mkdir;
|
||||
pub use mv::Mv;
|
||||
pub use open::Open;
|
||||
pub use rm::Rm;
|
||||
pub use save::Save;
|
||||
pub use start::Start;
|
||||
pub use touch::Touch;
|
||||
pub use util::BufferedReader;
|
||||
pub use watch::Watch;
|
||||
|
103
crates/nu-command/src/filesystem/start.rs
Normal file
103
crates/nu-command/src/filesystem/start.rs
Normal file
@ -0,0 +1,103 @@
|
||||
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,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user