cri icon indicating copy to clipboard operation
cri copied to clipboard

Platform colour detection uses outdated technique on Windows

Open glennsarti opened this issue 5 years ago • 3 comments

Previously whether to use ANSI (I assume) color codes on Windows was gated on the Win32::Console::ANSI object being defined (https://github.com/ddfreyne/cri/blob/2f172673f602d114e3632817a0d3868b31212dae/lib/cri/platform.rb#L24)

However that gets defined in the win32console gem which has been marked as deprecated for nearly 8 years now (https://github.com/luislavena/win32console)

And given that even the current console for Windows 10 supports colors, let alone the new Windows Terminal or VSCode Terminal or many other terminals support colours, this check seems very outdated.

This check should be modernised, or at least allow the color check to be overridden by a CRI option.

glennsarti avatar Jan 06 '20 07:01 glennsarti

I’m okay with disabling the check entirely, and defaulting to color. It’s forward-thinking, and I can’t come up with a better alternative. What do you think?

the win32console gem which has been marked as deprecated for nearly 8 years now

Haha, that shows how old Cri is (11-ish years).

denisdefreyne avatar Jan 21 '20 07:01 denisdefreyne

I’m okay with disabling the check entirely

Personally, I'm fine with colour-by-default, but unfortunately there needs to be some way due to older operating systems, or CI systems like Appveyor, where the colour codes just spam the output.

glennsarti avatar Jan 21 '20 08:01 glennsarti

As a current workaround, I do this:

require 'cri'

module CriColorExt
  @@color = true
  
  def color=(color)
    @@color = color
  end
  
  def color?(io)
    return @@color
  end
end

Cri::Platform.singleton_class.prepend(CriColorExt)

Then, you can enable/disable it manually easily:

Cri::Platform.color = true
Cri::Platform.color = false

In my app, I have --color and --no-color flags. Sometimes, color needs to be forced when making demos:

$ myapp --color | less -R
$ myapp --no-color > out.txt

If the flags aren't set, I auto-detect it:

Cri::Platform.color = ($stdout.tty?() && ENV['TERM'] != 'dumb')

You can do this in your app too until it's fixed in the gem.

esotericpig avatar Apr 20 '20 16:04 esotericpig