0

I would like to create a function / alias that colorizes the output of another command. Is there a best-practice way to do so?

For instance, let's take the alias command, whose output looks something like this:

Original

In the standard bash/zsh, this is all in the same colour. However, I'd like to adjust the output so that the part before the alias itself (the part before the =) is green, and the aliased command (the part after =) is red, for instance.

Obviously, alias is just an example, as I would like to add colours to other commands.

My current solution uses awk:

alias | awk -F"=" '{print "\033[1;32m" $1 "\033[0m=\033[1;31m"$2"\033[0m "}'

MWE

I don't find it particularly appealing (especially the colour codes). It also appears that this approach will become much more complicated for more complex commands (e.g. add colour output to top or ps) and also does not seem very maintainable in the long run.

Any ideas on how to make this better / easier ?

PS: I guess this is a Unix/Linux terminal question in general, but since I work primarily on OS X, I decided to start asking here...


EDIT: I now found this answer, that proposes using functions to make awk output look prettier. I'm not sure though if I want to have lots of formatting functions ghosting around my terminal. Still hoping for something cleaner...

Here's their example:

function red(s) {
    printf "\033[1;31m" s "\033[0m "
}

function green(s) {
    printf "\033[1;32m" s "\033[0m "
}

{
    print red($1), green($2), blue($3)
}
  • You could put the escape sequences used to switch colors into environment variables so you don't have to remember them each time (and to make it more easier to read) – nohillside Jan 13 at 6:24
  • I found a similar solution that uses bash functions, rather than env vars (see my edit above). Either way, these solutions would "pollute" my terminal & environment with unnecessary things that I never use directly. – stklik Jan 13 at 11:01
  • Having things you never use directly is the whole idea behind abstractions. Not sure what you mean in term of "cleaner". – nohillside Jan 13 at 11:04
  • Also, you can't call bash functions from awk :-) – nohillside Jan 13 at 11:04
  • 2
    IMHO, you're going to have a much more difficult time trying to colorize the output of commands that weren't colorized from the get go. If it's for a single command or task you use repeatedly, I can see the benefit. But for random commands, I think it would be more of a hindrance. – Allan Jan 13 at 11:14
1

Especially since you're already using zsh, I would recommend using the existing highlighter framework (and builtin highlighters) of zsh-syntax-highlighting or fast-syntax-highlighting Based on a preliminary review, both should have sufficient documentation to hopefully allow you to set up your preferred environment.

Here's a screenshot of zsh-syntax-highlighting's default parser operating on the parsing command you provided (meta, right?)

enter image description here

|improve this answer|||||
  • It seems like this only handles input not the output of a command, right? – stklik Jan 15 at 6:38
  • 1
    @stklik As written, correct, but my suggestion was that you use their parsing structure/code on the output with your own modifications/to set up your own environment. – JMY1000 Jan 15 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .