Rss

Improving feedback in the console

Seems that managers are not like used to be, so if you miss a more classical motivation you can turn on sudo insults.

The only thing you need is edit the /etc/sudoers file (using visudo!) and add the following line:

Defaults  insults

So any time we introduce a wrong password to sudo command you will get a motivational message:


david@david-sony:~$ sudo ls
[sudo] password for david:
No soap, honkie-lips.
[sudo] password for david:
Your mind just hasn't been the same since the electro-shock, has it?
[sudo] password for david:
BOB says:  You seem to have forgotten your passwd, enter another!
sudo: 3 incorrect password attempts

It is good, but I wanted it better ;-) Why don’t have this any time I type a wrong command?
I was able to get this list of insults from the sudo source code:

Just what do you think you're doing Dave?
It can only be attributed to human error.
That's something I cannot allow to happen.
My mind is going. I can feel it.
Sorry about this, I know it's a bit silly.
Take a stress pill and think things over.
This mission is too important for me to allow you to jeopardize it.
I feel much better now.
Wrong!  You cheating scum!
And you call yourself a Rocket Scientist!
No soap, honkie-lips.
Where did you learn to type?
Are you on drugs?
My pet ferret can type better than you!
You type like i drive.
Do you think like you type?
Your mind just hasn't been the same since the electro-shock, has it?
Maybe if you used more than just two fingers...
BOB says:  You seem to have forgotten your passwd, enter another!
stty: unknown mode: doofus
I can't hear you -- I'm using the scrambler.
The more you drive -- the dumber you get.
Listen, broccoli brains, I don't have time to listen to this trash.
Listen, burrito brains, I don't have time to listen to this trash.
I've seen penguins that can type better than that.
Have you considered trying to match wits with a rutabaga?
You speak an infinite deal of nothing
You silly, twisted boy you.
He has fallen in the water!
We'll all be murdered in our beds!
You can't come in. Our tiger has got flu
I don't wish to know that.
What, what, what, what, what, what, what, what, what, what?
You can't get the wood, you know.
You'll starve!
... and it used to be so popular...
Pauses for audience applause, not a sausage
Hold it up to the light --- not a brain in sight!
Have a gorilla...
There must be cure for it!
There's a lot of it about, you know.
You do that again and see what happens...
Ying Tong Iddle I Po
Harm can come to a young lad like that!
And with that remarks folks, the case of the Crown vs yourself was proven.
Speak English you fool --- there are no subtitles in this scene.
You gotta go owwwww!
I have been called worse.
It's only your word against mine.
I think ... err ... I think ... I think I'll go home

And I copied that list in a file called /etc/insults.txt, after that I edited the file /usr/lib/command-not-found and add the following function:

def get_insult() :
    try:
        with open("/etc/insults.txt") as f:
            insults = f.readlines()

        from random import choice
        return choice(insults)
    except:
        return "I don't have words to describe you"

And adding one more line:

print(_("%s: command not found") % args[0], file=sys.stderr)
print(get_insult())

It is done. Now if type badly I will get good advices:

david@david-sony:~/Downloads$ ls-l
ls-l: command not found
Ying Tong Iddle I Po

david@david-sony:~/Downloads$ ls-l
ls-l: command not found
We'll all be murdered in our beds!

david@david-sony:~/Downloads$ ls-l
ls-l: command not found
You silly, twisted boy you.

All this is Ubuntu specific, in Debian you should install the command-not-found package and change the file /usr/bin/command-not-found.

Extra ball

sudo apt-get install cowsay

And finally changing again /usr/lib/command-not-found:

print(_("%s: command not found") % args[0], file=sys.stderr)
from subprocess import call
call(["cowsay", get_insult()])

It looks like much more better:

david@david-sony:~$ wrongcommand
wrongcommand: command not found
 ______________________________________
/ Listen, burrito brains, I don't have \
\ time to listen to this trash.        /
 --------------------------------------
           ^__^
           (oo)_______
           (__)       )/
              ||----w |
              ||     ||

Much better apache log with some colors

When I have to debug some web application I often stared at the Apache log and say WTF!!
This is what the log might looks like:

log_01

The problem comes because every time I go into the web I get hundreds of unrelated log lines, a huge mess!

So one day I thought, could I improve that? My first idea was to add some color using grep like this:

tail -f /var/log/apache2/error.log | grep -E "ERROR|" --color

log_02

That is nicer because now I can see clearly errors in red.
Could that be better? Maybe yellow warnings?
The answer is yes, grep output can be concatenated to another grep and different colors can be used in every grep. Like this little script:

#!/bin/bash
shopt -s expand_aliases

alias grey-grep="GREP_COLOR='1;30' grep -E --color=always --line-buffered"
alias red-grep="GREP_COLOR='1;31' grep -E --color=always --line-buffered"
alias green-grep="GREP_COLOR='1;32' grep -E --color=always --line-buffered"
alias yellow-grep="GREP_COLOR='1;33' grep -E --color=always --line-buffered"
alias cyan-grep="GREP_COLOR='1;36' grep -E --color=always --line-buffered"

tail -1000f /var/log/apache2/error.log | grey-grep ".*PerformanceLogger.*|$" | cyan-grep "INFO|$" | yellow-grep "WARN|$" | red-grep "[ERROR].*|[FATAL].*|$" | green-grep "***|$"

So the output looks like:

log_03

Which is really cool, errors and warnings are clearly highlighted and all the performance parts I don’t care about are in a dark gray that allows me to focus on what is going on.

Of course, real programmers don’t need colours, this is just for fun ;-)

References

 __________
< Comment! >
 ----------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||