Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added timed task output type #3766

Merged
merged 4 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cli/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Change how tasks information is output when running tasks
- `prefix` - Print stdout/stderr by line, prefixed with the task's label
- `interleave` - Print directly to stdout/stderr instead of by line
- `replacing` - Stdout is replaced each time, stderr is printed as is
- `lengthy` - Only show stdout lines if they are displayed for more than 1 second
- `timed` - Only show stdout lines if they are displayed for more than 1 second
- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output
- `quiet` - Don't show extra output
- `silent` - Don't show any output including stdout and stderr from the task except for errors
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/tasks/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Change how tasks information is output when running tasks
- `prefix` - Print stdout/stderr by line, prefixed with the task's label
- `interleave` - Print directly to stdout/stderr instead of by line
- `replacing` - Stdout is replaced each time, stderr is printed as is
- `lengthy` - Only show stdout lines if they are displayed for more than 1 second
- `timed` - Only show stdout lines if they are displayed for more than 1 second
- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output
- `quiet` - Don't show extra output
- `silent` - Don't show any output including stdout and stderr from the task except for errors
Expand Down
4 changes: 2 additions & 2 deletions mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ The name of the script will be the name of the tasks.
flag "-q --quiet" help="Don't show extra output"
flag "-S --silent" help="Don't show any output except for errors"
flag "-o --output" help="Change how tasks information is output when running tasks" {
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label\n- `interleave` - Print directly to stdout/stderr instead of by line\n- `replacing` - Stdout is replaced each time, stderr is printed as is\n- `lengthy` - Only show stdout lines if they are displayed for more than 1 second\n- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output\n- `quiet` - Don't show extra output\n- `silent` - Don't show any output including stdout and stderr from the task except for errors"
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label\n- `interleave` - Print directly to stdout/stderr instead of by line\n- `replacing` - Stdout is replaced each time, stderr is printed as is\n- `timed` - Only show stdout lines if they are displayed for more than 1 second\n- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output\n- `quiet` - Don't show extra output\n- `silent` - Don't show any output including stdout and stderr from the task except for errors"
arg "<OUTPUT>"
}
mount run="mise tasks --usage"
Expand Down Expand Up @@ -1530,7 +1530,7 @@ The name of the script will be the name of the tasks.
flag "-q --quiet" help="Don't show extra output"
flag "-S --silent" help="Don't show any output except for errors"
flag "-o --output" help="Change how tasks information is output when running tasks" {
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label\n- `interleave` - Print directly to stdout/stderr instead of by line\n- `replacing` - Stdout is replaced each time, stderr is printed as is\n- `lengthy` - Only show stdout lines if they are displayed for more than 1 second\n- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output\n- `quiet` - Don't show extra output\n- `silent` - Don't show any output including stdout and stderr from the task except for errors"
long_help "Change how tasks information is output when running tasks\n\n- `prefix` - Print stdout/stderr by line, prefixed with the task's label\n- `interleave` - Print directly to stdout/stderr instead of by line\n- `replacing` - Stdout is replaced each time, stderr is printed as is\n- `timed` - Only show stdout lines if they are displayed for more than 1 second\n- `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output\n- `quiet` - Don't show extra output\n- `silent` - Don't show any output including stdout and stderr from the task except for errors"
arg "<OUTPUT>"
}
arg "[TASK]" help="Tasks to run\nCan specify multiple tasks by separating with `:::`\ne.g.: mise run task1 arg1 arg2 ::: task2 arg1 arg2" required=false default="default"
Expand Down
10 changes: 9 additions & 1 deletion schema/mise.json
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,15 @@
"task_output": {
"description": "Change output style when executing tasks.",
"type": "string",
"enum": ["prefix", "interleave", "keep-order", "quiet", "silent"]
"enum": [
"prefix",
"interleave",
"keep-order",
"replacing",
"timed",
"quiet",
"silent"
]
},
"task_run_auto_install": {
"default": true,
Expand Down
8 changes: 8 additions & 0 deletions settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,14 @@ enum = [
"keep-order",
"print output from tasks in the order they are defined"
],
[
"replacing",
"replace stdout each time a line is printed-this uses similar logic as `mise install`"
],
[
"timed",
"only show stdout lines that take longer than 1s to complete"
],
[
"quiet",
"print only stdout/stderr from tasks and nothing from mise"
Expand Down
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl Cli {
tool: Default::default(),
keep_order_output: Default::default(),
task_prs: Default::default(),
timed_outputs: Default::default(),
}));
} else if let Some(cmd) = external::COMMANDS.get(&task) {
external::execute(
Expand Down
48 changes: 44 additions & 4 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::collections::BTreeMap;
use std::io::Write;
use std::iter::once;
use std::ops::Deref;
use std::panic;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
use std::time::{Duration, SystemTime};
use std::{panic, thread};

use super::args::ToolArg;
use crate::cli::Cli;
Expand Down Expand Up @@ -176,7 +176,7 @@ pub struct Run {
/// - `prefix` - Print stdout/stderr by line, prefixed with the task's label
/// - `interleave` - Print directly to stdout/stderr instead of by line
/// - `replacing` - Stdout is replaced each time, stderr is printed as is
/// - `lengthy` - Only show stdout lines if they are displayed for more than 1 second
/// - `timed` - Only show stdout lines if they are displayed for more than 1 second
/// - `keep-order` - Print stdout/stderr by line, prefixed with the task's label, but keep the order of the output
/// - `quiet` - Don't show extra output
/// - `silent` - Don't show any output including stdout and stderr from the task except for errors
Expand All @@ -191,6 +191,9 @@ pub struct Run {

#[clap(skip)]
pub task_prs: IndexMap<Task, Arc<Box<dyn SingleReport>>>,

#[clap(skip)]
pub timed_outputs: Arc<Mutex<IndexMap<String, (SystemTime, String)>>>,
}

type KeepOrderOutputs = (Vec<(String, String)>, Vec<(String, String)>);
Expand Down Expand Up @@ -232,6 +235,27 @@ impl Run {

ctrlc::exit_on_ctrl_c(false);

if self.output(None) == TaskOutput::Timed {
let timed_outputs = self.timed_outputs.clone();
thread::spawn(move || loop {
{
let mut outputs = timed_outputs.lock().unwrap();
for (prefix, out) in outputs.clone() {
let (time, line) = out;
if time.elapsed().unwrap().as_secs() >= 1 {
if console::colors_enabled() {
prefix_println!(prefix, "{line}\x1b[0m");
} else {
prefix_println!(prefix, "{line}");
}
outputs.shift_remove(&prefix);
}
}
}
thread::sleep(Duration::from_millis(100));
});
}

self.fetch_tasks(&mut tasks)?;
let tasks = Deps::new(tasks)?;
for task in tasks.all() {
Expand All @@ -249,7 +273,6 @@ impl Run {
}
_ => {}
}
if self.output(Some(task)) == TaskOutput::KeepOrder {}
}

let num_tasks = tasks.all().count();
Expand Down Expand Up @@ -647,6 +670,22 @@ impl Run {
let pr = self.task_prs.get(task).unwrap().clone();
cmd = cmd.with_pr_arc(pr);
}
TaskOutput::Timed => {
let timed_outputs = self.timed_outputs.clone();
cmd = cmd.with_on_stdout(move |line| {
timed_outputs
.lock()
.unwrap()
.insert(prefix.to_string(), (SystemTime::now(), line));
});
cmd = cmd.with_on_stderr(|line| {
if console::colors_enabled() {
self.eprint(task, prefix, &format!("{line}\x1b[0m"));
} else {
self.eprint(task, prefix, &line);
}
});
}
TaskOutput::Silent => {
cmd = cmd.stdout(Stdio::null()).stderr(Stdio::null());
}
Expand Down Expand Up @@ -960,6 +999,7 @@ pub enum TaskOutput {
#[default]
Prefix,
Replacing,
Timed,
Quiet,
Silent,
}
Expand Down
2 changes: 1 addition & 1 deletion tasks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ run = "cargo install --path . --debug"
[xxx]
hide = true
tools = { gh = "2.60.0" }
run = "echo {{arg(name='greeting')}}"
run = "echo {{arg(name='greeting')}}; sleep 2; echo 2; sleep 2; echo 3; sleep 0.4; echo 4; sleep 1"
description = "a task for testing"
Loading