clicolors-control icon indicating copy to clipboard operation
clicolors-control copied to clipboard

Colors render incorrectly after running command

Open vallentin opened this issue 4 years ago • 3 comments

Submitted the same issue in mitsuhiko/console#47, but clicolors-control is upstream and experiences it to. So the issue is probably to be fixed here.

As mentioned in the other issue, running a command using std::process::Command, affects any colors rendered after.

  1. In Git Bash the \u{1b}[2m (.dim() in console) renders as if no color was applied. However, after running git diff using std::process::Command, then the color is rendered correctly.
  2. On the other hand, in CMD and Powershell, then after running git diff then the ANSI codes appear literally in the output.

Git Bash

dim-bash

CMD

Renders the same in PowerShell.

dim-cmd

Minimal, Reproducible Example

use std::process::Command;

// clicolors-control = "1.0.1"

fn main() {
    assert!(clicolors_control::colors_enabled());

    println!("Foo \u{1b}[36mBar\u{1b}[0m Baz"); // Cyan
    println!("Foo \u{1b}[2mBar\u{1b}[0m Baz"); // Dim
    println!();

    // Colors render incorrectly if git diff is executed
    Command::new("git").arg("diff").status().unwrap();

    assert!(clicolors_control::colors_enabled());

    println!("Foo \u{1b}[36mBar\u{1b}[0m Baz"); // Cyan
    println!("Foo \u{1b}[2mBar\u{1b}[0m Baz"); // Dim
    println!();
}

vallentin avatar Feb 06 '20 18:02 vallentin

Manually calling configure_terminal() after having executed a command, fixes the CMD and PowerShell issue.

Command::new("git").arg("diff").status().unwrap();

clicolors_control::configure_terminal();

vallentin avatar Feb 06 '20 23:02 vallentin

I'm not sure if this is fixable. This seems to be a limitation in how the ANSI terminal support on windows works :(

mitsuhiko avatar Mar 08 '20 11:03 mitsuhiko

Maybe just a note in the docs mentioning child processes and how it can affect the state of the console. Thus needing to call configure_terminal() after having executed a process.

vallentin avatar Mar 08 '20 12:03 vallentin