Skip to content

Align ANSI.color() with ANSI.red() formatting - #15868

Open
bougueil wants to merge 1 commit into
elixir-lang:mainfrom
bougueil:main
Open

Align ANSI.color() with ANSI.red() formatting#15868
bougueil wants to merge 1 commit into
elixir-lang:mainfrom
bougueil:main

Conversation

@bougueil

@bougueil bougueil commented Sep 8, 2026

Copy link
Copy Markdown

Align ANSI.color() with ANSI.red() ansi formatting for disabling and append_reset

  • Disabling ansi color strings:
IO.ANSI.format(IO.ANSI.color(0), false) |> to_string == ""
  • Add append_reset for color strings:
[IO.ANSI.format([IO.ANSI.color(3), " color(3) "]), " (no color)"] |> to_string ==
"#{IO.ANSI.color(3)} color(3) #{IO.ANSI.reset()} (no color)"

This change shows a 15% slowness for this bench:

Mix.install([{:benchee, "~> 1.5"}])

data = [:red, "message "]

Benchee.run(
  %{"IO.ANSI.format/1" => &IO.ANSI.format/1},
  inputs:   %{
    "100 ansi code messages" => List.duplicate(data, 100)
    },
  warmup: 2,
  time: 5,
  memory_time: 2
)

@josevalim

Copy link
Copy Markdown
Member

Apologies, but I don't understand what this pull request is doing and which bug it is meant to fix.

@bougueil

bougueil commented Sep 8, 2026

Copy link
Copy Markdown
Author

Apologies,

I had issue with current elixir with these 2 statements:

IO.ANSI.format(IO.ANSI.color(0), false) |> to_string 

which returns "\e[38;5;0m" but "" is expected.

[IO.ANSI.format([IO.ANSI.color(3), " color(3) "]), " (no color)"] |> to_string 

which returns "\e[38;5;3m color(3) (no color)" but "#{IO.ANSI.color(3)} color(3) #{IO.ANSI.reset()} (no color)" is expected.

@josevalim

Copy link
Copy Markdown
Member

I see. Thank you.

We only skip the coloring for atoms. We would need to support {:color, ...} notation for you to achieve what you want (or you check for ansi_enabled? yourself, probably preferable).

@bougueil

bougueil commented Sep 8, 2026

Copy link
Copy Markdown
Author

yeah, it's possible to add afterwards ANSI.reset and avoid color leakage appearing in the 2nd statement :

[IO.ANSI.format([IO.ANSI.color(3), " color(3) "]), " (no color)"] |> IO.puts

but ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants