Browse Source

Fix color=never on errors

Tamo 2 weeks ago
parent
commit
91e6d871c7
4 changed files with 23 additions and 6 deletions
  1. 1 0
      Cargo.lock
  2. 1 0
      numbat-cli/Cargo.toml
  3. 11 4
      numbat-cli/src/main.rs
  4. 10 2
      numbat/src/lib.rs

+ 1 - 0
Cargo.lock

@@ -1254,6 +1254,7 @@ dependencies = [
  "predicates",
  "rustyline",
  "serde",
+ "termcolor",
  "terminal_size",
  "toml",
 ]

+ 1 - 0
numbat-cli/Cargo.toml

@@ -23,6 +23,7 @@ toml = { version = "0.8.8", features = ["parse"] }
 serde = { version = "1.0.195", features = ["derive"] }
 terminal_size = "0.4.2"
 jiff = "0.2"
+termcolor = "1.4.1"
 
 [dependencies.clap]
 version = "4"

+ 11 - 4
numbat-cli/src/main.rs

@@ -28,7 +28,7 @@ use rustyline::{
 };
 use rustyline::{EventHandler, Highlighter, KeyCode, KeyEvent, Modifiers};
 
-use std::io::IsTerminal;
+use std::io::{IsTerminal, Write};
 use std::path::{Path, PathBuf};
 use std::sync::{Arc, Mutex};
 use std::{env, fs, thread};
@@ -391,7 +391,10 @@ impl Cli {
                             CommandControlFlow::NotACommand => {}
                         },
                         Err(err) => {
-                            ctx.print_diagnostic(*err);
+                            ctx.print_diagnostic(
+                                *err,
+                                colored::control::SHOULD_COLORIZE.should_colorize(),
+                            );
                             continue;
                         }
                     }
@@ -535,7 +538,10 @@ impl Cli {
     }
 
     fn print_diagnostic(&mut self, error: impl ErrorDiagnostic) {
-        self.context.lock().unwrap().print_diagnostic(error)
+        self.context
+            .lock()
+            .unwrap()
+            .print_diagnostic(error, colored::control::SHOULD_COLORIZE.should_colorize())
     }
 
     fn get_config_path() -> PathBuf {
@@ -628,7 +634,8 @@ fn main() {
     }
 
     if let Err(e) = Cli::new(args).and_then(|mut cli| cli.run()) {
-        eprintln!("{e:#}");
+        let mut stdout = termcolor::StandardStream::stderr(termcolor::ColorChoice::Never);
+        writeln!(stdout, "{e:#}").unwrap();
         std::process::exit(1);
     }
 }

+ 10 - 2
numbat/src/lib.rs

@@ -815,14 +815,22 @@ impl Context {
         Ok((typed_statements, result))
     }
 
-    pub fn print_diagnostic(&self, error: impl ErrorDiagnostic) {
+    pub fn print_diagnostic(&self, error: impl ErrorDiagnostic, colorize: bool) {
         use codespan_reporting::term::{
             self,
             termcolor::{ColorChoice, StandardStream},
             Config,
         };
 
-        let writer = StandardStream::stderr(ColorChoice::Auto);
+        let color = if colorize {
+            // by leaving it in auto we makes sure it can still
+            // chose between true or ansi colors
+            ColorChoice::Auto
+        } else {
+            ColorChoice::Never
+        };
+
+        let writer = StandardStream::stderr(color);
         let config = Config::default();
 
         // we want to be sure no one can write between our diagnostics