Skip to content

Commit

Permalink
Fix colours in subcommands (#666)
Browse files Browse the repository at this point in the history
Fix #598
  • Loading branch information
MrDenkoV authored Sep 22, 2023
1 parent 5ff2b9c commit d5c0df8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion scarb/src/core/manifest/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl ScriptDefinition {
let additional_args = args
.iter()
// surround all the additional arguments in double quotes
// and santize any command substition
// and sanitize any command substitution
.map(|a| {
format!(
"\"{}\"",
Expand Down
36 changes: 34 additions & 2 deletions scarb/src/ops/scripts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
use std::rc::Rc;

use anyhow::Result;
use camino::Utf8Path;
use deno_task_shell::{ExecutableCommand, ShellCommand};
use deno_task_shell::{parser, ExecutableCommand, ShellCommand};

use crate::core::errors::ScriptExecutionError;
use crate::core::manifest::ScriptDefinition;
Expand All @@ -20,7 +21,7 @@ pub fn execute_script(
custom_env: Option<HashMap<OsString, OsString>>,
) -> Result<()> {
let target_dir = Some(ws.target_dir().path_unchecked().to_owned());
let env_vars = get_env_vars(ws.config(), target_dir)?
let mut env_vars: HashMap<String, String> = get_env_vars(ws.config(), target_dir)?
.into_iter()
.map(|(k, v)| {
(
Expand All @@ -46,6 +47,21 @@ pub fn execute_script(
]);
let list = script_definition.parse(args)?;

// HACK: We help deno_task_shell use colors ;)
// We want to avoid the problem of piping the coloured text, by ensuring script contains no pipes.
// Perhaps there's a better way to tackle this issue (Maybe exec_replace instead of using env vars?).
if list.items.iter().all(|x| !has_pipe(&x.sequence)) {
for col_var in ["TERM", "COLORTERM"] {
if let Ok(value) = env::var(col_var) {
env_vars.insert(col_var.into(), value);
}
}
env_vars.insert(
"CLICOLOR".into(),
ws.config().ui().has_colors_enabled().to_string(),
);
}

let runtime = ws.config().tokio_handle();
let exit_code = runtime.block_on(deno_task_shell::execute(
list,
Expand All @@ -60,3 +76,19 @@ pub fn execute_script(
Ok(())
}
}

fn has_pipe(seq: &parser::Sequence) -> bool {
match seq {
parser::Sequence::ShellVar(_) => false,
parser::Sequence::Pipeline(pipeline) => match &pipeline.inner {
parser::PipelineInner::PipeSequence(_) => true,
parser::PipelineInner::Command(command) => match &command.inner {
parser::CommandInner::Simple(_) => false,
parser::CommandInner::Subshell(subshell) => {
subshell.items.iter().map(|x| &x.sequence).any(has_pipe)
}
},
},
parser::Sequence::BooleanList(list) => has_pipe(&list.current) || has_pipe(&list.next),
}
}
4 changes: 4 additions & 0 deletions utils/scarb-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ impl Ui {
pub fn force_colors_enabled(&self, enable: bool) {
console::set_colors_enabled(enable);
}

pub fn has_colors_enabled(&self) -> bool {
console::colors_enabled()
}
}

0 comments on commit d5c0df8

Please sign in to comment.