Skip to content

Commit

Permalink
Merge pull request #6525 from roc-lang/fix_text_contrast
Browse files Browse the repository at this point in the history
less colors and more contrast in terminal
  • Loading branch information
Anton-4 authored Feb 20, 2024
2 parents 3b3a330 + adf88e7 commit b5f68bc
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 104 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
use roc_build::program::report_problems_monomorphized;
use roc_load::{ExecutionMode, FunctionKind, LoadConfig, LoadMonomorphizedError};
use roc_packaging::cache;
use roc_reporting::report::ANSI_STYLE_CODES;
use roc_target::TargetInfo;

let start_time = Instant::now();
Expand Down Expand Up @@ -541,7 +542,7 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {

let mut writer = std::io::stdout();

let (failed, passed) = roc_repl_expect::run::run_toplevel_expects(
let (failed_count, passed_count) = roc_repl_expect::run::run_toplevel_expects(
&mut writer,
roc_reporting::report::RenderTarget::ColorTerminal,
arena,
Expand All @@ -555,7 +556,7 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {

let total_time = start_time.elapsed();

if failed == 0 && passed == 0 {
if failed_count == 0 && passed_count == 0 {
// TODO print this in a more nicely formatted way!
println!("No expectations were found.");

Expand All @@ -566,18 +567,20 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
// running tests altogether!
Ok(2)
} else {
let failed_color = if failed == 0 {
32 // green
let failed_color = if failed_count == 0 {
ANSI_STYLE_CODES.green
} else {
31 // red
ANSI_STYLE_CODES.red
};

let passed_color = ANSI_STYLE_CODES.green;

println!(
"\n\x1B[{failed_color}m{failed}\x1B[39m failed and \x1B[32m{passed}\x1B[39m passed in {} ms.\n",
"\n{failed_color}{failed_count}\x1B[39m failed and {passed_color}{passed_count}\x1B[39m passed in {} ms.\n",
total_time.as_millis(),
);

Ok((failed > 0) as i32)
Ok((failed_count > 0) as i32)
}
}

Expand Down
28 changes: 1 addition & 27 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,33 +210,7 @@ fn main() -> io::Result<()> {
threading,
) {
Ok((problems, total_time)) => {
println!(
"\x1B[{}m{}\x1B[39m {} and \x1B[{}m{}\x1B[39m {} found in {} ms.",
if problems.errors == 0 {
32 // green
} else {
33 // yellow
},
problems.errors,
if problems.errors == 1 {
"error"
} else {
"errors"
},
if problems.warnings == 0 {
32 // green
} else {
33 // yellow
},
problems.warnings,
if problems.warnings == 1 {
"warning"
} else {
"warnings"
},
total_time.as_millis(),
);

problems.print_to_stdout(total_time);
Ok(problems.exit_code())
}

Expand Down
3 changes: 2 additions & 1 deletion crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ extern crate roc_module;
mod cli_run {
use cli_utils::helpers::{
extract_valgrind_errors, file_path_from_root, fixture_file, fixtures_dir, has_error,
known_bad_file, run_cmd, run_roc, run_with_valgrind, strip_colors, Out, ValgrindError,
known_bad_file, run_cmd, run_roc, run_with_valgrind, Out, ValgrindError,
ValgrindErrorXWhat,
};
use const_format::concatcp;
use indoc::indoc;
use roc_cli::{CMD_BUILD, CMD_CHECK, CMD_DEV, CMD_FORMAT, CMD_RUN, CMD_TEST};
use roc_reporting::report::strip_colors;
use roc_test_utils::assert_multiline_str_eq;
use serial_test::serial;
use std::iter;
Expand Down
16 changes: 0 additions & 16 deletions crates/cli_utils/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,6 @@ pub fn path_to_binary(binary_name: &str) -> PathBuf {
path
}

pub fn strip_colors(str: &str) -> String {
use roc_reporting::report::ANSI_STYLE_CODES;

str.replace(ANSI_STYLE_CODES.red, "")
.replace(ANSI_STYLE_CODES.green, "")
.replace(ANSI_STYLE_CODES.yellow, "")
.replace(ANSI_STYLE_CODES.blue, "")
.replace(ANSI_STYLE_CODES.magenta, "")
.replace(ANSI_STYLE_CODES.cyan, "")
.replace(ANSI_STYLE_CODES.white, "")
.replace(ANSI_STYLE_CODES.bold, "")
.replace(ANSI_STYLE_CODES.underline, "")
.replace(ANSI_STYLE_CODES.reset, "")
.replace(ANSI_STYLE_CODES.color_reset, "")
}

pub fn run_roc_with_stdin<I, S>(args: I, stdin_vals: &[&str]) -> Out
where
I: IntoIterator<Item = S>,
Expand Down
4 changes: 1 addition & 3 deletions crates/compiler/load/tests/test_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,9 @@ mod test_reporting {
fn human_readable(str: &str) -> String {
str.replace(ANSI_STYLE_CODES.red, "<red>")
.replace(ANSI_STYLE_CODES.white, "<white>")
.replace(ANSI_STYLE_CODES.blue, "<blue>")
.replace(ANSI_STYLE_CODES.yellow, "<yellow>")
.replace(ANSI_STYLE_CODES.green, "<green>")
.replace(ANSI_STYLE_CODES.cyan, "<cyan>")
.replace(ANSI_STYLE_CODES.magenta, "<magenta>")
.replace(ANSI_STYLE_CODES.reset, "<reset>")
.replace(ANSI_STYLE_CODES.bold, "<bold>")
.replace(ANSI_STYLE_CODES.underline, "<underline>")
Expand Down Expand Up @@ -759,7 +757,7 @@ mod test_reporting {
&DEFAULT_PALETTE,
);

assert_eq!(human_readable(&buf), "<blue>activityIndicatorLarge<reset>");
assert_eq!(human_readable(&buf), "<cyan>activityIndicatorLarge<reset>");
}

#[test]
Expand Down
18 changes: 7 additions & 11 deletions crates/compiler/load_internal/tests/test_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use roc_module::symbol::{Interns, ModuleId};
use roc_packaging::cache::RocCacheDir;
use roc_problem::can::Problem;
use roc_region::all::LineInfo;
use roc_reporting::report::RenderTarget;
use roc_reporting::report::RocDocAllocator;
use roc_reporting::report::{can_problem, DEFAULT_PALETTE};
use roc_reporting::report::{strip_colors, RenderTarget};
use roc_solve::FunctionKind;
use roc_target::TargetInfo;
use roc_types::pretty_print::name_and_print_var;
Expand Down Expand Up @@ -1218,11 +1218,9 @@ fn non_roc_file_extension() {
I expected a file with extension `.roc` or without extension.
Instead I received a file with extension `.md`."
);
let color_start = String::from_utf8(vec![27, 91, 51, 54, 109]).unwrap();
let color_end = String::from_utf8(vec![27, 91, 48, 109]).unwrap();
let err = multiple_modules("non_roc_file_extension", modules).unwrap_err();
let err = err.replace(&color_start, "");
let err = err.replace(&color_end, "");

let err = strip_colors(&multiple_modules("non_roc_file_extension", modules).unwrap_err());

assert_eq!(err, expected, "\n{}", err);
}

Expand Down Expand Up @@ -1255,10 +1253,8 @@ fn roc_file_no_extension() {
The provided file did not start with a shebang `#!` containing the
string `roc`. Is tmp/roc_file_no_extension/main a Roc file?"
);
let color_start = String::from_utf8(vec![27, 91, 51, 54, 109]).unwrap();
let color_end = String::from_utf8(vec![27, 91, 48, 109]).unwrap();
let err = multiple_modules("roc_file_no_extension", modules).unwrap_err();
let err = err.replace(&color_start, "");
let err = err.replace(&color_end, "");

let err = strip_colors(&multiple_modules("roc_file_no_extension", modules).unwrap_err());

assert_eq!(err, expected, "\n{}", err);
}
9 changes: 3 additions & 6 deletions crates/repl_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use const_format::concatcp;
use roc_load::MonomorphizedModule;
use roc_mono::ir::OptLevel;
use roc_repl_eval::gen::Problems;
use roc_repl_ui::colors::{BLUE, END_COL, PINK};
use roc_repl_ui::colors::{CYAN, END_COL};
use roc_repl_ui::repl_state::{ReplAction, ReplState};
use roc_repl_ui::{format_output, is_incomplete, CONT_PROMPT, PROMPT, SHORT_INSTRUCTIONS, TIPS};
use roc_reporting::report::{ANSI_STYLE_CODES, DEFAULT_PALETTE};
Expand All @@ -21,11 +21,8 @@ use crate::cli_gen::eval_llvm;

pub const WELCOME_MESSAGE: &str = concatcp!(
"\n The rockin' ",
BLUE,
"roc repl",
END_COL,
"\n",
PINK,
CYAN,
"roc repl\n",
"────────────────────────",
END_COL,
"\n\n"
Expand Down
1 change: 1 addition & 0 deletions crates/repl_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bumpalo.workspace = true
indoc.workspace = true
strip-ansi-escapes.workspace = true
target-lexicon.workspace = true
regex.workspace = true
rustyline.workspace = true

[features]
Expand Down
11 changes: 6 additions & 5 deletions crates/repl_test/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bumpalo::Bump;
use regex::Regex;
use roc_wasm_interp::{
wasi, DefaultImportDispatcher, ImportDispatcher, Instance, Value, WasiDispatcher,
};
Expand Down Expand Up @@ -158,11 +159,11 @@ pub fn expect_failure(input: &'static str, expected: &str) {
pub fn expect(input: &'static str, expected: &str) {
let raw_output = run(input);

// We need to get rid of HTML tags, and we can be quite specific about it!
// If we ever write more complex test cases, we might need regex here.
let without_html = raw_output.replace("<span class='color-magenta'> : </span>", " : ");
// remove color HTML tags
let regx = Regex::new("<span class='color-(.*?)'> : </span>").unwrap();
let without_html = regx.replace_all(&raw_output, " : ");

let clean_output = without_html.trim();
let trimmed_output = without_html.trim();

assert_eq!(clean_output, expected);
assert_eq!(trimmed_output, expected);
}
3 changes: 1 addition & 2 deletions crates/repl_ui/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const STYLE_CODES: StyleCodes = if cfg!(target_family = "wasm") {
ANSI_STYLE_CODES
};

pub const BLUE: &str = STYLE_CODES.blue;
pub const PINK: &str = STYLE_CODES.magenta;
pub const GREEN: &str = STYLE_CODES.green;
pub const CYAN: &str = STYLE_CODES.cyan;
pub const END_COL: &str = STYLE_CODES.reset;
22 changes: 10 additions & 12 deletions crates/repl_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ pub mod colors;
pub mod repl_state;

use bumpalo::Bump;
use colors::{BLUE, END_COL, PINK};
use colors::{CYAN, END_COL, GREEN};
use const_format::concatcp;
use repl_state::{parse_src, ParseOutcome};
use roc_parse::ast::{Expr, ValueDef};
use roc_repl_eval::gen::{Problems, ReplOutput};
use roc_reporting::report::StyleCodes;

use crate::colors::GREEN;

// TODO add link to repl tutorial (does not yet exist).
pub const TIPS: &str = concatcp!(
"\nEnter an expression to evaluate, or a definition (like ",
BLUE,
CYAN,
"x = 1",
END_COL,
") to use later.\n\n",
Expand All @@ -25,25 +23,25 @@ pub const TIPS: &str = concatcp!(
} else {
// We use ctrl-v + ctrl-j for newlines because on Unix, terminals cannot distinguish between Shift-Enter and Enter
concatcp!(
BLUE,
CYAN,
" - ",
END_COL,
PINK,
GREEN,
"ctrl-v",
END_COL,
" + ",
PINK,
GREEN,
"ctrl-j",
END_COL,
" makes a newline\n",
BLUE,
CYAN,
" - ",
END_COL,
GREEN,
":q",
END_COL,
" quits\n",
BLUE,
CYAN,
" - ",
END_COL,
GREEN,
Expand All @@ -57,8 +55,8 @@ pub const TIPS: &str = concatcp!(
// For when nothing is entered in the repl
// TODO add link to repl tutorial(does not yet exist).
pub const SHORT_INSTRUCTIONS: &str = "Enter an expression, or :help, or :q to quit.\n\n";
pub const PROMPT: &str = concatcp!(BLUE, "»", END_COL, " ");
pub const CONT_PROMPT: &str = concatcp!(BLUE, "…", END_COL, " ");
pub const PROMPT: &str = concatcp!(CYAN, "»", END_COL, " ");
pub const CONT_PROMPT: &str = concatcp!(CYAN, "…", END_COL, " ");

pub fn is_incomplete(input: &str) -> bool {
let arena = Bump::new();
Expand Down Expand Up @@ -116,7 +114,7 @@ pub fn format_output(
{
buf.push('\n');
buf.push_str(&expr);
buf.push_str(style_codes.magenta); // Color for the type separator
buf.push_str(style_codes.green); // Color for the type separator
buf.push_str(EXPR_TYPE_SEPARATOR);
buf.push_str(style_codes.reset);
buf.push_str(&expr_type);
Expand Down
41 changes: 38 additions & 3 deletions crates/reporting/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use roc_problem::can::Problem;
use roc_region::all::LineInfo;
use roc_solve_problem::TypeError;

use crate::report::ANSI_STYLE_CODES;

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Problems {
pub fatally_errored: bool,
Expand All @@ -26,11 +28,11 @@ impl Problems {
}

pub fn print_to_stdout(&self, total_time: std::time::Duration) {
const GREEN: usize = 32;
const YELLOW: usize = 33;
const GREEN: &str = ANSI_STYLE_CODES.green;
const YELLOW: &str = ANSI_STYLE_CODES.yellow;

print!(
"\x1B[{}m{}\x1B[39m {} and \x1B[{}m{}\x1B[39m {} found in {} ms",
"{}{}\x1B[39m {} and {}{}\x1B[39m {} found in {} ms",
match self.errors {
0 => GREEN,
_ => YELLOW,
Expand All @@ -54,6 +56,39 @@ impl Problems {
}
}

// prints e.g. `1 error and 0 warnings found in 63 ms.`
pub fn print_error_warning_count(
error_count: usize,
warning_count: usize,
total_time: std::time::Duration,
) {
const GREEN: &str = ANSI_STYLE_CODES.green;
const YELLOW: &str = ANSI_STYLE_CODES.yellow;

print!(
"{}{}\x1B[39m {} and {}{}\x1B[39m {} found in {} ms",
match error_count {
0 => GREEN,
_ => YELLOW,
},
error_count,
match error_count {
1 => "error",
_ => "errors",
},
match warning_count {
0 => GREEN,
_ => YELLOW,
},
warning_count,
match warning_count {
1 => "warning",
_ => "warnings",
},
total_time.as_millis()
);
}

pub fn report_problems(
sources: &MutMap<ModuleId, (PathBuf, Box<str>)>,
interns: &Interns,
Expand Down
Loading

0 comments on commit b5f68bc

Please sign in to comment.