denisdefreyne / cri

A tool for building commandline applications
MIT License
120 stars 19 forks source link

Platform colour detection uses outdated technique on Windows #106

Open glennsarti opened 4 years ago

glennsarti commented 4 years ago

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.

denisdefreyne commented 4 years ago

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).

glennsarti commented 4 years ago

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.

esotericpig commented 4 years ago

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.