mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:24:58 +02:00
Implement job id
command
This cmomit also modifies run_external and job_unfreeze because the engine_state stores the id of the current job in the current_thread_job field, so its usages are accessed differently now.
This commit is contained in:
@ -451,6 +451,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
|
||||
JobSpawn,
|
||||
JobList,
|
||||
JobKill,
|
||||
JobId,
|
||||
Job,
|
||||
};
|
||||
|
||||
|
54
crates/nu-command/src/experimental/job_id.rs
Normal file
54
crates/nu-command/src/experimental/job_id.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use nu_engine::command_prelude::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct JobId;
|
||||
|
||||
impl Command for JobId {
|
||||
fn name(&self) -> &str {
|
||||
"job id"
|
||||
}
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"Get id of current job."
|
||||
}
|
||||
|
||||
fn extra_description(&self) -> &str {
|
||||
"This command returns the job id for the current background job.
|
||||
The special id 0 indicates that this command was not called from a background job thread, and
|
||||
was instead spawned by main nushell execution thread."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("job id")
|
||||
.category(Category::Experimental)
|
||||
.input_output_types(vec![(Type::Nothing, Type::Int)])
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["self", "this", "my-id", "this-id"]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let head = call.head;
|
||||
|
||||
if let Some((id, _)) = &engine_state.thread_job_entry {
|
||||
Ok(Value::int(id.get() as i64, head).into_pipeline_data())
|
||||
} else {
|
||||
Ok(Value::int(0, head).into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
example: "job id",
|
||||
description: "Get id of current job",
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
@ -69,8 +69,11 @@ impl Command for JobSpawn {
|
||||
|
||||
let id = {
|
||||
let thread_job = ThreadJob::new(job_signals);
|
||||
job_state.current_thread_job = Some(thread_job.clone());
|
||||
jobs.add_job(Job::Thread(thread_job))
|
||||
let id = jobs.add_job(Job::Thread(thread_job.clone()));
|
||||
|
||||
job_state.thread_job_entry = Some((id, thread_job));
|
||||
|
||||
id
|
||||
};
|
||||
|
||||
let result = thread::Builder::new()
|
||||
|
@ -115,7 +115,7 @@ fn unfreeze_job(
|
||||
Job::Frozen(FrozenJob { unfreeze: handle }) => {
|
||||
let pid = handle.pid();
|
||||
|
||||
if let Some(thread_job) = &state.current_thread_job {
|
||||
if let Some(thread_job) = &state.current_thread_job() {
|
||||
if !thread_job.try_add_pid(pid) {
|
||||
kill_by_pid(pid.into()).map_err(|err| {
|
||||
ShellError::Io(IoError::new_internal(
|
||||
@ -133,7 +133,7 @@ fn unfreeze_job(
|
||||
.then(|| state.pipeline_externals_state.clone()),
|
||||
);
|
||||
|
||||
if let Some(thread_job) = &state.current_thread_job {
|
||||
if let Some(thread_job) = &state.current_thread_job() {
|
||||
thread_job.remove_pid(pid);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
mod is_admin;
|
||||
mod job;
|
||||
mod job_id;
|
||||
mod job_kill;
|
||||
mod job_list;
|
||||
mod job_spawn;
|
||||
@ -9,9 +10,9 @@ mod job_unfreeze;
|
||||
|
||||
pub use is_admin::IsAdmin;
|
||||
pub use job::Job;
|
||||
pub use job_id::JobId;
|
||||
pub use job_kill::JobKill;
|
||||
pub use job_list::JobList;
|
||||
|
||||
pub use job_spawn::JobSpawn;
|
||||
|
||||
#[cfg(all(unix, feature = "os"))]
|
||||
|
@ -279,7 +279,7 @@ impl Command for External {
|
||||
)
|
||||
})?;
|
||||
|
||||
if let Some(thread_job) = &engine_state.current_thread_job {
|
||||
if let Some(thread_job) = engine_state.current_thread_job() {
|
||||
if !thread_job.try_add_pid(child.pid()) {
|
||||
kill_by_pid(child.pid().into()).map_err(|err| {
|
||||
ShellError::Io(IoError::new_internal(
|
||||
@ -312,8 +312,8 @@ impl Command for External {
|
||||
}
|
||||
|
||||
let jobs = engine_state.jobs.clone();
|
||||
let this_job = engine_state.current_thread_job.clone();
|
||||
let is_interactive = engine_state.is_interactive;
|
||||
let this_job = engine_state.current_thread_job().cloned();
|
||||
let child_pid = child.pid();
|
||||
|
||||
// Wrap the output into a `PipelineData::ByteStream`.
|
||||
|
Reference in New Issue
Block a user