Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dune runtest and colored strings in terminal #2664

Closed
sapristi opened this issue Sep 22, 2019 · 4 comments · Fixed by #4408
Closed

dune runtest and colored strings in terminal #2664

sapristi opened this issue Sep 22, 2019 · 4 comments · Fixed by #4408

Comments

@sapristi
Copy link

Hello,
I noticed a strange behaviour of dune runtest.
Say I have the following file test_colors.ml :

let f s =
  output_string stdout ( "\027[34m" ^ s ^ "\027[39m"); flush stdout
    in

    for i= 1 to 20 do
      f (string_of_int i)
    done

And the dune file

(test
 (name test_colors) )

When executing dune runtest, i get the following :
Capture d’écran_2019-09-22_14-16-13
where I would have expected all the characters to be colorized instead.

I have (quickly) tested with different versions of dune; I think the problem appeared in version 1.11.0

@sapristi sapristi changed the title dune runtest and colored strings in tern dune runtest and colored strings in terminal Sep 22, 2019
@ghost
Copy link

ghost commented Sep 23, 2019

Ah, it's because of the way Dune handles colors in the output of commands. Basically, it's not completely pass-through; Dune parses the output of command to interpret ANSI escape sequences and then re-synthetise them. This is unless you pass --no-buffer.

I checked with strace and the string dune sends to the terminal is \33[34m1\33[0m\33[34;39;34m2\33[0m\33[34;39;34;39;34m3\33[0m\33[34;39;34;39;34;39;34m4\33[0m\33[34;39;34;39;34;39;34;39;34m5\33[0m\33[34;39;34;39;34;39;34;39;34;39;34m6.... The problem is that the ANSI escape code parser is not good enough.

Any chance you'd be interested in contributing to Dune? That's a fun self-contained project :-)

@sapristi
Copy link
Author

Any chance you'd be interested in contributing to Dune? That's a fun self-contained project :-)
--

I will try to submit a PR when I have time to work on it :)

@ghost
Copy link

ghost commented Sep 24, 2019

Sounds good. The code to modify is in src/stdune/ansi_colors.ml. There is a function parse that parses a string with ANSI escape sequences. It needs to be extended to recognise all SGR parameters as described on https://en.wikipedia.org/wiki/ANSI_escape_code. Then we can use a better representation of a set of styles and avoid such sequences to be generated. Feel free to change the Style.t type as well or to represent a set of styles using something else than a Style.t list.

@sapristi
Copy link
Author

Ok, I'll check that out !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant