diff --git a/src/gleeunit/internal/reporting.gleam b/src/gleeunit/internal/reporting.gleam index 72f766f..f661823 100644 --- a/src/gleeunit/internal/reporting.gleam +++ b/src/gleeunit/internal/reporting.gleam @@ -204,28 +204,49 @@ pub fn test_skipped(state: State, module: String, function: String) -> State { State(..state, skipped: state.skipped + 1) } +@external(erlang, "gleeunit_ffi", "getenv") +fn getenv(name: String) -> Result(String, Nil) + +fn no_color_env_set() -> Bool { + case getenv("NO_COLOR") { + Ok(_) -> True + Error(_) -> + case getenv("NO_COLOUR") { + Ok(_) -> True + Error(_) -> False + } + } +} + +fn color(start: String, end: String, text: String) -> String { + case no_color_env_set() { + True -> text + False -> start <> text <> end + } +} + fn bold(text: String) -> String { - "\u{001b}[1m" <> text <> "\u{001b}[22m" + color("\u{001b}[1m", "\u{001b}[22m", text) } fn cyan(text: String) -> String { - "\u{001b}[36m" <> text <> "\u{001b}[39m" + color("\u{001b}[36m", "\u{001b}[39m", text) } fn yellow(text: String) -> String { - "\u{001b}[33m" <> text <> "\u{001b}[39m" + color("\u{001b}[33m", "\u{001b}[39m", text) } fn green(text: String) -> String { - "\u{001b}[32m" <> text <> "\u{001b}[39m" + color("\u{001b}[32m", "\u{001b}[39m", text) } fn red(text: String) -> String { - "\u{001b}[31m" <> text <> "\u{001b}[39m" + color("\u{001b}[31m", "\u{001b}[39m", text) } fn grey(text: String) -> String { - "\u{001b}[90m" <> text <> "\u{001b}[39m" + color("\u{001b}[90m", "\u{001b}[39m", text) } @external(erlang, "file", "read_file") diff --git a/src/gleeunit_ffi.erl b/src/gleeunit_ffi.erl index 05c7490..15a7f76 100644 --- a/src/gleeunit_ffi.erl +++ b/src/gleeunit_ffi.erl @@ -1,6 +1,6 @@ -module(gleeunit_ffi). --export([find_files/2, run_eunit/2]). +-export([find_files/2, run_eunit/2, getenv/1]). find_files(Pattern, In) -> Results = filelib:wildcard(binary_to_list(Pattern), binary_to_list(In)), @@ -19,3 +19,8 @@ run_eunit(Tests, Options) -> end end. +getenv(Name) -> + case os:getenv(binary_to_list(Name)) of + false -> {error, nil}; + Value -> {ok, list_to_binary(Value)} + end.