owenthereal / ccat

Colorizing `cat`
https://github.com/owenthereal/ccat
MIT License
3.16k stars 118 forks source link

Only colorize when the output is stdout #9

Closed gilliek closed 9 years ago

owenthereal commented 9 years ago

The code looks good. But could you give me a use case of not using ccat in a terminal?

gilliek commented 9 years ago

Thanks :)

Simply ccat foo.go > bar.go ;-) One of the use case of cat(1). This way, ccat can be used as a replacement of cat(1).

owenthereal commented 9 years ago

ccat foo.go > bar.go should work without your check of terminal since it outputs to stdout? The > is actually piping stdout to a file.

owenthereal commented 9 years ago

Oh okay, or you want ccat to fall back to cat when there's a >?

gilliek commented 9 years ago

Yes it works, but it also outputs the color codes

gilliek commented 9 years ago

Precisely :)

owenthereal commented 9 years ago

I'm thinking having a flag so that people could disable syntax highlighting like this: ccat -p main.go > foo (p stands for plain text). It's a more generic solution adapting to all cases.

gilliek commented 9 years ago

It could be nice too :) But my point is that the output, outside of a terminal, is completely useless since the ANSI escape codes only work within a terminal so I see no reason in favor of colorizing when there is a >. I think it's more convenient to fallback to a cat like behavior in this very case.

owenthereal commented 9 years ago

Ah, make sense :+1:. I merged it in https://github.com/jingweno/ccat/commit/25c0ff0882947c36556bcce03664c15e15cb1991

gilliek commented 9 years ago

Thanks :)

rolinh commented 9 years ago

The behavior proposed by @gilliek here is similar to --color=auto on some GNU tools (see ls man page for instance) and is the expected behavior by most users I'd say. I would not use a new flag for that and as @gilliek stated, there is no point in having ANSI escape codes in a text file.

owenthereal commented 9 years ago

@Rolinh Sure, the change is in master, feel free to try it out :smiley_cat: