odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.55k stars 570 forks source link

[Odin Test] Ignore ANSI escape codes for non tty outputs #4100

Open MrStevns opened 3 weeks ago

MrStevns commented 3 weeks ago

Context

When using odin test where the output is not printed to a terminal but say the output window in Sublime Text or in my case, Focus, the output is unnecessarily messy. It would be nice if we could ignore the ANSI escape codes or have a flag to disable them.

Odin report

Expected Behavior

When building a project from either Sublime text or Focus, or any other non tty output, i'd expect something like this:

[INFO ] --- [2024-08-17 07:52:29] Starting test runner with 1 thread. Set with -define:ODIN_TEST_THREADS=n.
[INFO ] --- [2024-08-17 07:52:29] The random seed sent to every test is: 39114654177527. Set with -define:ODIN_TEST_RANDOM_SEED=n.
[INFO ] --- [2024-08-17 07:52:29] Memory tracking is enabled. Tests will log their memory usage if there's an issue.
[INFO ] --- [2024-08-17 07:52:29] < Final Mem/ Total Mem> <  Peak Mem> (#Free/Alloc) :: [package.test_name]
[INFO ] --- [2024-08-17 07:52:29] [rendering.odin:9:example()] Debug test
rendering  [|                       ]         1 :: [package done]

Finished 1 test in 270µs. The test was successful.

Current Behavior

Where as right now the output is this.

�[?25l�[?7lrendering  [�[90m|                       �[0m]    0/   1 :: �[90mexample�[0m
�[?7h1 thread                                 0/   1 :: total
�[2F�[J�]2;Odin test runner (0/1)�\�[?7lrendering  [�[33m|                       �[0m]    0/   1 :: �[33mexample�[0m
�[?7h1 thread                                 0/   1 :: total
�[2F�[J�]2;Odin test runner (1/1)�\�[?7lrendering  [�[32m|                       �[0m]         1 :: [package done]
�[?7h
�[?25h�[0m[INFO ] --- �[0m[2024-08-17 07:53:51] Starting test runner with 1 thread. Set with -define:ODIN_TEST_THREADS=n.
�[0m[INFO ] --- �[0m[2024-08-17 07:53:51] The random seed sent to every test is: 39368671393172. Set with -define:ODIN_TEST_RANDOM_SEED=n.
�[0m[INFO ] --- �[0m[2024-08-17 07:53:51] Memory tracking is enabled. Tests will log their memory usage if there's an issue.
�[0m[INFO ] --- �[0m[2024-08-17 07:53:51] < Final Mem/ Total Mem> <  Peak Mem> (#Free/Alloc) :: [package.test_name]
�[0m[INFO ] --- �[0m[2024-08-17 07:53:51] [rendering.odin:9:example()] Debug test
Finished 1 test in 151µs. The test was �[32msuccessful.�[0m

Steps to Reproduce

To Reproduce

  1. Create a odin project like so:
package mytestproject

import "core:log"
import "core:testing"

@test
example :: proc(t: ^testing.T)
{
    log.info("Debug test")
}
  1. Create a focus-config file so you can run the odin project and get the output like so: > odin test /path/to/project/
  2. Run the test project via the command modal in focus

Additional notes

I started out making this a Focus editor issue but as the maintainer responded, shouldn't this be handled by Odin?

Feoramund commented 3 weeks ago

I've been aware of this since the rewrite. This happens with any core:log interface that redirects a console logger, because the logger's created assuming it can use color output, then it's redirected and that may not be the case anymore. It's an issue that will require more OS-specific terminal-aware functionality.

You can mitigate some of this by using -define:ODIN_TEST_FANCY=false to disable the animation though, which will keep from printing the progress bar lines.

laytan commented 3 weeks ago

The compiler has this piece of code to determine if it should do colors, is this a good idea to port over and use here? https://github.com/odin-lang/Odin/blob/f49ebae9562257effe014e3c175496915041d5f2/src/main.cpp#L2860-L2905

Feoramund commented 3 weeks ago

isatty and GetConsoleMode are the OS parts needed I mentioned. Checking the TERM string and the environment variables is good. I think this could do it.