how can I change the default syntax coloring in ruby 3.0's IRB
Asked Answered
B

4

14

I'm very happy to be using the most recent ruby 3.0; as well as having access to the updated command-line interpreter which does syntax highlighting and coloring.

However, the colors are a bit hard to see for me. How can I change them? The command line options for IRB allow me to turn off colorization with --nocolorize, but I can't figure out where the configuration files are that would allow me to change the defaults (e.g., to make the blue color lighter.)

Briarwood answered 2/3, 2021 at 21:22 Comment(1)
I was not aware that with Ruby 3, we will have a clourful irb, but I am using in irb the wirble gem with works with Ruby 2.x (perhaps with Ruby 3 too) and allows you to configure the colours.Dele
T
12

I fixed this by changing iTerm2 theme.

Switching to 'Tango Dark' made it readable.

Tango Dark

Here how the new Ruby 3.1 autocomplete feature looks like right now:

Irb autocomplete

Tremendous answered 12/4, 2022 at 11:13 Comment(0)
L
8

(Edit 2024) For Ruby 3.3+:

Use Reline::Face to configure the look. (Thanks to @xyz for the comment!)

For an older Ruby:

Some are hardcoded; but most of it is inside a constant, and thus editable (even though it's private). This should let you change all the pesky blues with cyans. The single downside is that keywords are indeed hardcoded to use CYAN, but we can cheat and change the CYAN constant itself to something else (e.g. BLUE - readability for stuff like nil and true is not that important to me, but feel free to change to something else), and hope no other plugin relies on CYAN actually being cyan :D

module IRB::Color
  TOKEN_SEQ_EXPRS.each do |token, (seq, exprs)|
    seq[0] = CYAN if seq[0] == BLUE
  end
  remove_const :CYAN
  CYAN = BLUE
end

You can put it inside $HOME/.irbrc to make it work across all future irb sessions.

Needless to say, this is a hack, and should IRB::Color change in the future, this may well stop working.

Lacasse answered 6/4, 2022 at 2:18 Comment(5)
How to replace red color for strings?Multiplicity
@Multiplicity I guess you could download color.rb and edit it, redefine TOKEN_SEQ_EXPRS as you want, then require it in order to do arbitrary changes. But without that, the only thing you can do is change one constant for another, as described above (in your case RED).Lacasse
Thanks, yes I have to redefine TOKEN_SEQ_EXPRS because I tried to replace the constant RED with MAGENTA but it doesn't work. Thank you very much.Multiplicity
For IRB's code autocompletion widget (a screenshot in a different Answer shows it), it seems like the colours used are not set within IRB itself, but hardcoded in the separate reline library: github.com/ruby/reline/blob/…Glossography
Since Ruby 3.3 (which includes IRB 1.11), the colours used in the autocompletion widget can now be configured: github.com/ruby/reline/blob/master/doc/reline/face.mdGlossography
C
6

A quick work around until this is configurable is to change the ANSI Cyan colour default in your terminal preferences. In iTerm2 you can go to preferences > Profiles > Colors. I went for a rather fetching 383a59.

fixing irb autocomplete cyan background white text issue

Carbuncle answered 18/5, 2022 at 17:32 Comment(0)
M
2

As far as I can tell reading the source, the colors are hard-coded in the last version or IRB, so there's no configuration (yet!) available for this.

Metagenesis answered 2/3, 2021 at 21:51 Comment(4)
While the color name is hardcoded the far more interesting color value depends on the terminal settings. I'm guessing but maybe OP uses an older Windows version or used to upgrading keeps most settings). Current Windows 10 versions have better default color values. See ANSI escape code on Wikipedia.Lanark
@Lanark the problem if you change the terminal colors is that it changes them everywhere, so it's not very practical if you already like the config you have. I didn't know it was possible on windows though, good to know!Metagenesis
True but if this kind of colored text is barely readable in irb it's very likely barely readable in other applications as well.Lanark
Some are hardcoded; but most of it is inside a constant, and thus editable (even though it's private).Lacasse

© 2022 - 2024 — McMap. All rights reserved.