Skip to content

Commit

Permalink
Fix forced colors with cargo run
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Jun 19, 2024
1 parent 344fd28 commit 79b91e9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
3 changes: 0 additions & 3 deletions .cargo/config.toml

This file was deleted.

5 changes: 1 addition & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn create_formatter(cli: &Cli) -> MessageFormatter {
} else if cli.no_color {
false
} else {
colored::control::SHOULD_COLORIZE.should_colorize()
colored::control::ShouldColorize::from_env().should_colorize()
};
let timestamp_format = cli.timestamp_format.unwrap_or({
if tty {
Expand Down Expand Up @@ -209,9 +209,6 @@ async fn main() -> Result<()> {
// Fix broken pipe panics
sigpipe::reset();

// Let us control the coloring instead of colored
colored::control::set_override(true);

let config = load_config()?;
let database = config
.as_ref()
Expand Down
8 changes: 7 additions & 1 deletion cli/src/message_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{anyhow, Result};
use chrono::{Local, TimeZone, Utc};
use chrono_humanize::HumanTime;
use database::{Message, State};
use std::collections::HashMap;
use std::{collections::HashMap, sync::Once};

enum Word {
Message,
Expand Down Expand Up @@ -103,6 +103,12 @@ impl MessageFormatter {
pub fn format_message(&self, message: &Message, appendix: Option<String>) -> Result<String> {
use colored::Colorize;

static INIT: Once = Once::new();
INIT.call_once(|| {
// Let us control the coloring instead of colored
colored::control::set_override(true);
});

// Display the time differently based on the requested format
let time = match self.timestamp_format {
TimestampFormat::Relative => Some(
Expand Down
2 changes: 0 additions & 2 deletions cli/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ mod tests {
assert_eq!(line.to_string(), "⭐⭐…");
}

// This test requires CLICOLOR_FORCE=1 set in .cargo/config.toml to use colors, even in CI
#[test]
fn test_colored() {
let mut line = TruncatedLine::new(11);
Expand All @@ -200,7 +199,6 @@ mod tests {
assert_eq!(line.to_string(), "hello \u{1b}[31mworld\u{1b}[0m");
}

// This test requires CLICOLOR_FORCE=1 set in .cargo/config.toml to use colors, even in CI
#[test]
fn test_colored_empty_string() {
let mut line = TruncatedLine::new(5);
Expand Down

0 comments on commit 79b91e9

Please sign in to comment.