More Pretty Colours

Ever since I went to Tokyo and wandered up the busy streets at night, staring at the fabulous colour adverts, I’ve had a thing about making stuff colourful. None of your washed out pastels mind you.

And so we come to Linux. Powerful, reliable and at the terminal – BORING AS HELL.  Read on for the cure…

I can’t tell you how many times I’ve watched “apt-get update” and similar commands flying past my eyes so quickly that spotting anything relevant is almost impossible. Today as you may have gathered if you’ve been following the blog,  we not only got the sync setup for copying from one NanoPi to another – but also modified rpi-clone and that works a treat on the Nano as well.  All it needed was a little colour.

 

To the rescue – ccze.  (I did a little video for those who like video) I put this in my script by default – but if you don’t have it you’re missing a treat and the examples below will automatically add it if need be.

colours

Originally I thought that aliases would be great for this – but it turns out you can’t easily pass parameters to aliases which is a bit of a pain… for example if you wanted a colour version of “ls” – you might want to pass it the name of a folder or something.

And that’s where FUNCTIONS come in useful.  You can just create a function and paste it into a terminal for testing – OR you can store one in, say,  /etc/bash.bashrc for permanent use.

In these examples – I check FIRST for ccze and if not installed, install it THEN run the code – no apparent overhead.

CLEARLY if you make a function you must ensure it uses a name that isn’t likely to be in use elsewhere… so I just prefix with an underscore then alias the real name. The backslashes are used to prevent recursion.

Just run this one on the command line then try out “dir” for example.  Less stress on the fingers, easier on the eye)…

A modification to dir

_dir() { [ ! -x “$(command -v ccze)” ] && sudo apt-get install ccze;  \dir “$@” | ccze -A;}; alias dir=’_dir’

This is the dir command with some colour added.  if you have a subfolder called fred you can still use parameters, for example  type “dir fred”

A trivial example – but hey – experiment and if you like what you come up with you can do what I’ve done and make these a permanent part of your setup.

Try this which vastly improves ifconfig

_ifconfig() { [ ! -x “$(command -v ccze)” ] && sudo apt-get install ccze; \ifconfig “$@” | ccze -A; }; alias ifconfig=’_ifconfig’

Below, I’ve merged update and upgrade as I always seem to end up using them together anyway.

“apt-get upgrade” etc have to be done differently because they are not one word so here is command called “upgr” to do an update and upgrade while saving 40 keystrokes – in colour –  thanks Antonio and Google for help)

upgr() { [ ! -x “$(command -v ccze)” ] && sudo apt-get install ccze; echo “*** Updating”; sudo apt-get update | ccze -A; echo “*** Upgrading”; sudo apt-get upgrade | ccze -A; }

I’m sure you can think of lots of others…

Even MORE pretty colours

It turns out that “the majority” of “modern” terminals can actually handle 256 colours – Putty on my PC certainly can so this link will be of use to some.

If you want to have a play, the last example given controls both foreground and background colours

echo -en ‘\033[38;5;196;48;5;53m Hello\r\n’

Take the above, replace that 196 with another foreground colour 0-255 and that 53 and replace that with another background colour 0-255.

12 thoughts on “More Pretty Colours

  1. For OSX users I’ve modified command a bit as OSX has no apt-get:

    _ifconfig() { [ ! -x “$(command -v ccze)” ] && brew install ccze; \ifconfig | ccze -A; }; alias ifconfig=’_ifconfig’

  2. Just so everyone knows, I’m not poo-pooing the idea.

    For those that are wondering why the first line doesn’t immediately arrive. Piping commands from one command to another , that buffering will occur. Not sure how large the buffer is (for some reason I’m thinking 1K or 4K characters). But this will hold on to some of your data until the buffer fills. When the command that is being buffered ends, then the last of the data should flow as the pipe closes.

      1. Reboot? This is Linux (Unix) we don’t reboot the system for minor changes … 😉

        I think you just source the file in the existing shell and you’ll be okay.

        Also I’d consider moving these out of the functions and just into the main part of the .bashrc file (with no local). That will spee up the commands a bit:

        local bold=$(tput bold) # make colors bold/bright
        local red=”$bold$(tput setaf 1)” # bright red text
        local green=$(tput setaf 2) # dim green text
        local fawn=$(tput setaf 3); beige=”$fawn” # dark yellow text
        local yellow=”$bold$fawn” # bright yellow text
        local darkblue=$(tput setaf 4) # dim blue text
        local blue=”$bold$darkblue” # bright blue text
        local purple=$(tput setaf 5); magenta=”$purple” # magenta text
        local pink=”$bold$purple” # bright magenta text
        local darkcyan=$(tput setaf 6) # dim cyan text
        local cyan=”$bold$darkcyan” # bright cyan text
        local gray=$(tput setaf 7) # dim white text
        local darkgray=”$bold”$(tput setaf 0) # bold black = dark gray text
        local white=”$bold$gray” # bright white text

  3. not very “practical” (requires 2 terminals…), but it works and you can have FULL output colored with ccze…
    create a special file, a named pipe:

    mkpipe mypipe

    then redirect STDOUT to it

    exec > mypipe

    then open an other terminal, putty or whatever

    cat < mypipe | ccze -A

    every command run now on 1st terminal, will have its output on the 2nd one, colored…

    you can restore everything to default by just closing both terminals and opening new ones… there’s a way to do without it, google it 😉

    1. you can use even a standard file as destination
      on 1st terminal:

      exec > newfile

      on 2nd terminal:

      tail -f newfile | ccze -A

      and you’ll have all the output saved to that file, too, and you can color it as many times you want 😀

    2. and i just found that you can redirect a full block of code to something else, instead of every single command in there:

      #…part of script without redirection…
      {
      #…part of script with redirection…
      } > stdout.txt 2> stderr.txt
      #…residue of script without redirection…

      or, to just color it:

      #…part of script without redirection…
      {
      #…part of script with redirection…
      } | ccze -A
      #…residue of script without redirection…

  4. i do updates and cleaning with a 1 liner alias:

    alias aggiorna=’sudo apt-get -y update | ccze -A && sudo apt-get -y dist-upgrade | ccze -A && sudo apt-get -y autoremove | ccze -A && sudo apt-get -y autoclean’

    those 2 update() and upgrade() can just be aliases themselves, too

    oh, and you CAN define an alias which is called as a command… example:

    alias ls=’ls –color=auto’

    so anytime i use ls, i’m actually running that alias… you can revert temporarely to standard behaviour without unaliasing an alias, preceding the command with \, so

    \ls

    will give the stardard output of ls, ignoring the alias

Comments are closed.