Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I disable ANSI colours in Rails Console output

I connect to a rails console using:

bundle exec rails c

I use enter this option in an attempt to disable ANSI colours:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

irb(main):005:0> ActiveSupport::LogSubscriber.colorize_logging = false
=> false

I still see colours when I look at command outputs.

I use enter this option in an attempt to disable ANSI colours:

irb(main):006:0> IRB.conf[:USE_COLORIZE] = false
=> false

I still see colours when I look at command outputs.

I don’t have permission to change any configuration files or install any Gems.

Full Background

I just want to run some commands to look at data objects via a script and capture the output. I don’t want my file to have ANSI colour codes. I am actually doing this from my local mac using kubectl to connect to a pod where rails is running:

kubectl exec my-cool-pod-name -n my-cool-namespace-name -it -- bash -c "bundle exec rails c<<ENDRAILS
ActiveSupport::LogSubscriber.colorize_logging = false
myStuff = MyPkg::MyStuff.friendly.find("dog1")
myStuff.goodStuff
ENDRAILS
" > railsConsole.out

A work around seems to be to use the the y or j format specifiers to generate yaml or json respectively like this, but I don’t really like these formats for these objects:

y myStuff.goodStuff
j myStuff.goodStuff.inspect

I also considered removing the ANSI colour codes from out redirection output file railsConsole.out after it has been generated, but that seems messy and unreliable from looking at posts on that technique.

Rails is version 7.0.4

irb(main):007:0> Rails.version
=> "7.0.4"

>Solution :

Hmm, the IRB.conf[:USE_COLORIZE] = false should work (whether in the console, or in your ~/.irbrc). There’s a CLI option for this too which you could trigger with: rails c -- --nocolorize. But I’m not sure that’ll work if the IRB.conf doesn’t work. 🤷

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading