Skip to content

Commit 79b91e9

Browse files
committed
Fix forced colors with cargo run
1 parent 344fd28 commit 79b91e9

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

.cargo/config.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

cli/src/main.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn create_formatter(cli: &Cli) -> MessageFormatter {
7979
} else if cli.no_color {
8080
false
8181
} else {
82-
colored::control::SHOULD_COLORIZE.should_colorize()
82+
colored::control::ShouldColorize::from_env().should_colorize()
8383
};
8484
let timestamp_format = cli.timestamp_format.unwrap_or({
8585
if tty {
@@ -209,9 +209,6 @@ async fn main() -> Result<()> {
209209
// Fix broken pipe panics
210210
sigpipe::reset();
211211

212-
// Let us control the coloring instead of colored
213-
colored::control::set_override(true);
214-
215212
let config = load_config()?;
216213
let database = config
217214
.as_ref()

cli/src/message_formatter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anyhow::{anyhow, Result};
55
use chrono::{Local, TimeZone, Utc};
66
use chrono_humanize::HumanTime;
77
use database::{Message, State};
8-
use std::collections::HashMap;
8+
use std::{collections::HashMap, sync::Once};
99

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

106+
static INIT: Once = Once::new();
107+
INIT.call_once(|| {
108+
// Let us control the coloring instead of colored
109+
colored::control::set_override(true);
110+
});
111+
106112
// Display the time differently based on the requested format
107113
let time = match self.timestamp_format {
108114
TimestampFormat::Relative => Some(

cli/src/truncate.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ mod tests {
191191
assert_eq!(line.to_string(), "⭐⭐…");
192192
}
193193

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

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

0 commit comments

Comments
 (0)