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

Compiler: use Colorize.on_tty_only! #4499

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

makenowjust
Copy link
Contributor

Colorize.on_tty_only! is introduced in #4271, but it is not used in compiler. I think it is useful that colorize is turned on/off depending on tty detection result.

In addition, I fixed bin/crystal wrapper script to detect TTY. bin/crystal script's SUPPORTS_COLORS is really buggy, at least it isn't working correct now.

@RX14
Copy link
Contributor

RX14 commented Jun 3, 2017

Could you seperate the changes to bin/crystal and the compiler into seperate commits? The core team might even want them in seperate prs.

@makenowjust
Copy link
Contributor Author

ok. Please wait.

@makenowjust
Copy link
Contributor Author

@RX14 I can separate this PR into the wrapper bug fix and others (using Colorize.on_tty_support! in compiler). This bug fix is just one line, however it causes a conflict with this PR surely (because this PR added TTY detection into this line). Do you want it really?

@makenowjust makenowjust force-pushed the fix/colorize/use-in-crystal-command branch from e9f2a05 to a08ce00 Compare June 3, 2017 10:02
@RX14
Copy link
Contributor

RX14 commented Jun 3, 2017

@makenowjust i'm not sure I understand what the conflict is.

@makenowjust
Copy link
Contributor Author

@RX14 Bug fix patch:

diff --git a/bin/crystal b/bin/crystal
index 54704199d..8b4380f30 100755
--- a/bin/crystal
+++ b/bin/crystal
@@ -115,7 +115,7 @@ __has_colors() {
     return 1
   fi
 }
-SUPPORTS_COLORS=$(__has_colors)
+SUPPORTS_COLORS=$(__has_colors && echo true || echo false)
 __error_msg() {
   if $SUPPORTS_COLORS; then
     # bold red coloring

@makenowjust
Copy link
Contributor Author

I didn't intend to fix a bug. I implemented TTY detection on the wrapper script correctly, then I fixed a bug unexpectedly.

Why cause a problem when I make a PR about Colorize!!!!

@makenowjust
Copy link
Contributor Author

This PR diff is a little large, but it is really simple fix. I will explain:

  • Added Colorize.on_tty_only! to Crystal::Command#run.
  • Fixed --no-color option to Colorize.enabled = false, and added --color option as Colorize.enabled = true.
  • Removed @color instance variable and colorize, with_color method, then replaced colorize(obj) to obj.colorize. In addition, removed .toggle(@color) on commands/formatter.cr.
  • Two specs about compiler is broken, fixed it.
  • The wrapper is fixed to use colorize on TTY only.

That's all.

@straight-shoota
Copy link
Member

@makenowjust could you rebase this PR on master?

@makenowjust makenowjust force-pushed the fix/colorize/use-in-crystal-command branch from a08ce00 to cfd74df Compare November 29, 2017 02:13
To support TTY detection

Fixed specs and wrapper script
@makenowjust makenowjust force-pushed the fix/colorize/use-in-crystal-command branch from cfd74df to d6c6e18 Compare November 30, 2017 03:05
@makenowjust
Copy link
Contributor Author

@straight-shoota rebased.

@@ -636,7 +636,9 @@ describe "Semantic: generic class" do
begin
semantic(nodes)
rescue ex : TypeException
old_enabled, Colorize.enabled = Colorize.enabled?, false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better as two statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you but I don't think that.

@straight-shoota
Copy link
Member

straight-shoota commented Nov 30, 2017

I'd suggest to add the option to explicitly set color mode to auto (which is the default). This would be additional to the already included --color and --no-color.
Example from man git-diff(1):

--color[=<when>]
Show colored diff.  --color (i.e. without =<when>) is the same as --color=always.  <when> can be one of always, never, or auto. [...]
 --no-color
Turn off colored diff. This can be used to override configuration settings. It is the same as --color=never.

A lot of command line application do it like this, and I think this would be the best way to handle this.

@makenowjust
Copy link
Contributor Author

@straight-shoota It was implemented in #3925...

case color
when "auto"
Colorize.on_tty_only!
when "always"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color = "always" if color.empty? => when "always", .empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when "always", ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, color = "always" if color.empty? shows "always" as default clearly, so I would keep this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but it adds an completely unnecessary comparison and assignment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be all in one case statement, not a case plus an if.

@makenowjust
Copy link
Contributor Author

bin/crystal run --color a.cr means bin/crystal run --color=a.cr, so --color's default value does not make a sense. I'll remove this.

For example `crystal run --color a.cr` means `crystal run --color=a.cr`.
It is not useful.
@straight-shoota
Copy link
Member

straight-shoota commented Dec 1, 2017

Why not use --color[=<always,never,auto>]?

@makenowjust
Copy link
Contributor Author

@straight-shoota It does not change meaning.

@straight-shoota
Copy link
Member

Yes it does. If --color expects a = to be assigned a value, --color a.cr will not be confused.

@makenowjust
Copy link
Contributor Author

makenowjust commented Dec 1, 2017

Yes but it is not implemented unfortunately.

@straight-shoota
Copy link
Member

You mean OptionParser doesn't differentiate between --color=<when> and --color <when>? Then maybe it should or you could implement this differently.

The commands should provide an arg-less --color flag to enable colorization.

@bew
Copy link
Contributor

bew commented Dec 1, 2017

Maybe it has already been said, but to me, the <when> is useless:

  • --color argument: force colors
  • --no-color argument: disable colors
  • no color-related argument: automatic colors (on tty)

@straight-shoota
Copy link
Member

It is not useless but adds expressiveness.

It makes it easy to inject a configuration value into the command line arguments, like crystal --color=$MY_COLOR_SETTING ... instead of some complicated decision tree between --color, --no-color and ``.

@larubujo
Copy link
Contributor

larubujo commented Dec 1, 2017

more complex things. global state in colorize used by all apps. global state is bad. the design is being ruined. more command line switches. the direction should be to simplify things.

@straight-shoota
Copy link
Member

@larubujo This PR is about the compiler, not "all apps".

@makenowjust
Copy link
Contributor Author

The commands should provide an arg-less --color flag to enable colorization.

I totally agree with this. But it can be merged this before implementing this feature in OptionParser, and then fixes this in PR about OptionParser.

@RX14
Copy link
Contributor

RX14 commented Dec 1, 2017

I personally prefer @bew's suggestion. At least, if this PR is blocked on an OptionParser enhancement, do that first.

@makenowjust
Copy link
Contributor Author

@straight-shoota I want to revert --color=when support for now because it blocks to merge this PR and I don't think this option is needed strongly. If you want this feature really, then you can create a new pull request after fixed OptionParser.

@makenowjust makenowjust closed this Dec 1, 2017
@makenowjust makenowjust reopened this Dec 1, 2017
@makenowjust
Copy link
Contributor Author

Sorry... 🙇

@jkthorne
Copy link
Contributor

@makenowjust are you still working on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:needs-work A PR requires modifications by the author. topic:compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants