Live data from Hacker News

Show HN: Grep with colours written in Go

github.com

71–80 of 90 posts

Re: Show HN: Grep with colours written in Go

#71
post #62

Earlier quoted context omitted.

> Another workaround is to merely pipe the output to a file in certain cases. If that works for a certain tool, it's author really has no excuse to not implement NO_COLOR env as well.

Piping the output does not guarantee the removal of the color codes. This is yet another standard some programs have chosen to implement. The program checks if the output is a tty, if it is not a tty it will drop color information as it is either a pipe or a file. The escape codes would look ugly in a file and could get in your way with some text processing programs. Even if the program did drop color when not sendin…

> Even if the program did drop color when not sending output to a tty it still is a crappy way to remove color as the pipe would need another program as a receiver such as pager.

You don't need a pager. cat works. Compare

    grep --color=auto hacker /usr/share/dict/words
with

    grep --color=auto hacker /usr/share/dict/words | cat
The same applies to GNU ls, which actually changes more than just colors depending on whether it's printing to a tty or not.

Programs that emit colors by default when not sending output to a tty are buggy.

A wild card in this mix is Windows. On UNIX-like systems, detecting a tty is simple. On Windows, it is... not so simple. But the OP's tool, blush, doesn't appear to support Windows at all anyway. (Which is totally cool. I very much understand why you might not.)

Re: Show HN: Grep with colours written in Go

#72
post #61

Earlier quoted context omitted.

Thanks mate, I will definitely have a read.

Note that you don't need Boyer Moore for the common case. ripgrep for example will very rarely use Boyer Moore. Its work horse is much simpler and typically faster: https://github.com/rust-lang/regex/blob/master/src/literal/m... In Go-land, you should be able to replace uses of memchr with IndexByte[1], which should be implemented in Assembly on most platforms. Of course, for any of this to have a big impact, you'll…

So far I've been only concerned about code's simplicity until I understand what there needs to be done. This is not going to be grep or ripgrep. My intent was to make a tool I needed so I started working on it. I thought someone else might like it, now it is joyful to see people are looking at the project.

There are a couple of places I wish I would have done better. Using bufio.Scanner actually bothers me a lot. Also in the Read() method it reads everything from all readers into a buffer instead of pulling what it needs to check.

Thanks for suggestions :)

Re: Show HN: Grep with colours written in Go

#74

Earlier quoted context omitted.

Piping the output does not guarantee the removal of the color codes. This is yet another standard some programs have chosen to implement. The program checks if the output is a tty, if it is not a tty it will drop color information as it is either a pipe or a file. The escape codes would look ugly in a file and could get in your way with some text processing programs. Even if the program did drop color when not sendin…

> Even if the program did drop color when not sending output to a tty it still is a crappy way to remove color as the pipe would need another program as a receiver such as pager. You don't need a pager. cat works. Compare grep --color=auto hacker /usr/share/dict/words with grep --color=auto hacker /usr/share/dict/words | cat The same applies to GNU ls, which actually changes more than just colors depending on whether…

I don't think it matters so much on Windows, because the colour information is sent out of band. When you call SetConsoleTextAttribute on the STD_OUTPUT_HANDLE, and that handle is a file, the call simply fails.

Re: Show HN: Grep with colours written in Go

#75
post #34

Earlier quoted context omitted.

lol people's reading comprensión is so bad sometimes

In fairness, it's the GPs fault on this occasion for not punctuating his or her post. Decarep just read the GPs post as it was literally written (I had to read it 3 times myself to gauge what I thought the post meant).

contextualization is a component of reading comprehension

Re: Show HN: Grep with colours written in Go

#76

Earlier quoted context omitted.

lol people's reading comprensión is so bad sometimes

Reading comprehension is difficult when there's missing punctuation. For example, "Let's eat grandma" versus "Let's eat, grandma".

contextualization is a component of reading comprehension

Re: Show HN: Grep with colours written in Go

#77
post #74

Earlier quoted context omitted.

> Even if the program did drop color when not sending output to a tty it still is a crappy way to remove color as the pipe would need another program as a receiver such as pager. You don't need a pager. cat works. Compare grep --color=auto hacker /usr/share/dict/words with grep --color=auto hacker /usr/share/dict/words | cat The same applies to GNU ls, which actually changes more than just colors depending on whether…

I don't think it matters so much on Windows, because the colour information is sent out of band. When you call SetConsoleTextAttribute on the STD_OUTPUT_HANDLE, and that handle is a file, the call simply fails.

I am not speaking without experience. If you support Windows, you probably need to support non-console environments like cygwin and msys. Moreover, Windows 10 has opt-in support for ANSI style coloring. At some point, coloring via the console APIs will probably stop being used.

Re: Show HN: Grep with colours written in Go

#78
post #74

Earlier quoted context omitted.

I don't think it matters so much on Windows, because the colour information is sent out of band. When you call SetConsoleTextAttribute on the STD_OUTPUT_HANDLE, and that handle is a file, the call simply fails.

I am not speaking without experience. If you support Windows, you probably need to support non-console environments like cygwin and msys. Moreover, Windows 10 has opt-in support for ANSI style coloring. At some point, coloring via the console APIs will probably stop being used.

[deleted]

Re: Show HN: Grep with colours written in Go

#79
post #53

Earlier quoted context omitted.

| sed -e /\x1b\[[0-9;]+m// ^ I have not run the above program, only proven it to be correct.

Sadly that only removes 1 of the 3 methods of escaping terminal colours. Plus there is also the risk of it removing other SGR (Select Graphic Rendition) escape codes such as underline. That latter bug might be an acceptable casualty though.

It was meant to remove underline, bold, dim, and even blink.

Are there xterm codes for generating terminal color that don't end in m? It handily covers 8/88/256/24bit. What else is needed?

Re: Show HN: Grep with colours written in Go

#80

Earlier quoted context omitted.

Piping the output does not guarantee the removal of the color codes. This is yet another standard some programs have chosen to implement. The program checks if the output is a tty, if it is not a tty it will drop color information as it is either a pipe or a file. The escape codes would look ugly in a file and could get in your way with some text processing programs. Even if the program did drop color when not sendin…

> Even if the program did drop color when not sending output to a tty it still is a crappy way to remove color as the pipe would need another program as a receiver such as pager. You don't need a pager. cat works. Compare grep --color=auto hacker /usr/share/dict/words with grep --color=auto hacker /usr/share/dict/words | cat The same applies to GNU ls, which actually changes more than just colors depending on whether…

Yeah, I did not mean to imply you needed a pager, just something to receive the output, a pager is just one choice.

I guess the program implemented the tty method you could always alias your stuff to something like blush="/bin/blush | cat" if you decided you did not like color.

While a part of me says piping programs to get the result you want is the *nix way. Another part of me feels like it would just be a dirty hack to avoid color.

Post reply on HN