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

windows colors improvements #645

Merged
merged 8 commits into from
Mar 30, 2015
Merged

Conversation

avih
Copy link
Contributor

@avih avih commented Mar 28, 2015

  • can build also without Makefile.w32
  • fixed pager/pipe colors - fallback to ansi colors for pager/pipe or CLI flag.
  • doesn't show bold colors for everything, more compatible implementation.
  • can show color also for matches (was only showing for path and line number).
  • better includes, removed unused variables and static, initialize others.
  • fixed undefined behavior if the print length is more than BUFSIZ
  • some more stuff - details at the commit messages.

@avih
Copy link
Contributor Author

avih commented Mar 28, 2015

@mattn, any objection to these changes?

@ggreer
Copy link
Owner

ggreer commented Mar 29, 2015

Thanks for making this PR. Since I don't use Windows, I'd love to hear other Windows users chime in on this. Also, I'm finally adding clang-format tests to ag, so you'll have to rebase to get this mergeable again.

@avih
Copy link
Contributor Author

avih commented Mar 29, 2015

Not sure I understand the rebase comment, these patches are on top of the current tip. Or did you mean you will add clang-format tests, and after that these patches will have to be rebased?

As for the other changes, unused variables and no-op static declarations, assuming that a buffer is null terminated when it might not be, interpreting all color codes as bold (making it impossible to use non bold colors), not being able to see the color of --color-match since it resets the color after each fprintf but ag prints the color in one statement and the match in another, the fact that colors are applied to the screen even when the output goes to another program (pager/pipe) - all those are just incorrect.

Do you expect other windows users to apply these patches, compile ag for windows (which is notoriously hard - the last publicly available version for windows is more than a year old and unmaintained), and then report back if these changes are worthwhile?

Despite the big number of patches, the changes to the code are few. It just fixes what was incorrect, and adds the --color-win-ansi option for those who want to bail out of using native windows colors if they use a terminal which support ansi colors.

@avih
Copy link
Contributor Author

avih commented Mar 29, 2015

attr_initialized should take TRUE/FALSE. So should not be WORD.

Thanks. Done.

on environments which can build ag for windows with the default Makefile, such
as MXE, print_w32.c was not built since it only appears at Makefile.w32, and so
the build failed.

include this file also at Makefile.am but put the whole of it in #idfef _WIN32.
also add correct includes, removed some unused variables, and initialize others
this fixes undefined behavior when a single fprintf_w32 call ends up longer
than BUFSIZ - which is only guarenteed to be "at least 256 bytes".

this patch defines an arbitrary limit of 16k chars per call, and
truncates correctly to this size if it's exceeded.

it also falls back to passthrough if stdout is not a tty. this fixes pager
(had bad output regardless of color) and pipe (never had color). now both
of them handle colors with ag like on *nix systems.
on cases where the terminal or the pager have better ansi color support
than the current code has (e.g. less for windows does better, or ConEmu
supports 24 bit SGR ansi sequences which the existing code can't assume
or handle, etc), allow the user to bail out of the native colors and let
other applications handle the ansi sequences.
the code assumed that state doesn't carry between calls, and as such
treated the screen color at the time of call as the "reset" color, and
used this reset color when exiting the function.

it does seem like it at least tried to keep the state (static attr_old)
however it's never actually used as static and therefore overwritten on
each call. (same goes for static stdo which is overwritten every time,
but at least this one doesn't hurt us other than slighly slower code).

this broke the matches colors since the color is printed in one fprintf
call, and the text in another, and so the color was reset after the
first call and didn't carry into the second, so it ended up without color.

this patch initializes attr_old only once, removes the color reset at
the end of each fprintf, and also doesn't reset it before any color change.

also removes the static for stdo since it's never used as static.
- plain 30-37 and 40-47 should not set bold on, but they did
- reset should (and now does) also reset all non color attributes
- blink now used for bg intensity (same as bash/less/ansicon for windows)
- uncommented and fixed fg/bg specific color resets
- replaced a magic number 6 (max sequence length) with something more readable
- removed 100 as reset (90-97 and 100-107 are aixterm codes for bright fg/bg)

it's still not as robust as other ansi color interpreters for windows,
especially when it comes to complex sequences, but now it does get the basics
mostly right and similar to others for simple sequences

also, fprintf_w32 tries to handle several other types of ansi sequences which
ag never uses (cursor position, etc) except for K which is typically no-op
anyway. I didn't look at those parts of the code, but they could probably be
removed safely.
@avih
Copy link
Contributor Author

avih commented Mar 29, 2015

so you'll have to rebase to get this mergeable again

Done.

@mattn
Copy link
Contributor

mattn commented Mar 29, 2015

Where the color_win_ansi is used at?

@mattn
Copy link
Contributor

mattn commented Mar 29, 2015

@avih
Copy link
Contributor Author

avih commented Mar 29, 2015

Hmm.. it seems to me like the Travis CI failure is is unrelated to the patch. But if someone thinks otherwise, let me know.


const char *color_line_number = "\033[1;33m"; /* yellow with black background */
const char *color_line_number = "\033[1;33m"; /* bold yellow */
Copy link
Owner

Choose a reason for hiding this comment

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

The color didn't change but the comment did?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I just fixed the comment. It's bold yellow, the same as 1;32 two lines below is bold green. Yellow on black background is 0;33, or rather 0;33;40 if you want to make sure the background is black in case the default background from reset (0) is not black.

Copy link
Owner

Choose a reason for hiding this comment

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

Ah, got it. That's probably been wrong for years. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd never have noticed it myself, but I happened to spend way too much time recently studying the ansi color escape sequences and how they behave on different systems, wrote an implementation from scratch, and then fixed another one ;)

@ggreer
Copy link
Owner

ggreer commented Mar 30, 2015

The Travis breakage was my fault. I've fixed it in master. I made a couple comments, but they're not show-stoppers. If you could rebase and move --color-win-ansi into the manpage, that'd be sweet. If that doesn't happen in a couple of days, I'll merge this and make the changes myself.

ggreer added a commit that referenced this pull request Mar 30, 2015
@ggreer ggreer merged commit bbf827b into ggreer:master Mar 30, 2015
@avih avih deleted the win-colors-pr333-fixes branch March 30, 2015 09:40
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

Successfully merging this pull request may close these issues.

3 participants