Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform colour detection uses outdated technique on Windows #106

Open
glennsarti opened this issue Jan 6, 2020 · 3 comments
Open

Platform colour detection uses outdated technique on Windows #106

glennsarti opened this issue Jan 6, 2020 · 3 comments

Comments

@glennsarti
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants