A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label colour. Show all posts
Showing posts with label colour. Show all posts

Easily add colours to bash scripts

RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
WHITE="$(tput setaf 7)"
RESET=$WHITE

echo "${RED}An error occurred${RESET}"
echo "${YELLOW}Warning: Be careful${RESET}"
echo "${GREEN}Everything is groovy${RESET}"

(source)

Old, naive way:

GREEN="\[\033[32m\]"
YELLOW="\[\033[33m\]"
RESET="\[\033[0m\]"

Colouring terminal output


Thanks to StackOverflow

UPDATE: You can actually do this yourself with pretty much any programming language that has an ANSI colours library.

Things lacking in Eclipse for Perl

Using EPIC
  • You can't fold code on 'if' blocks, or any arbitrary block (only on subs and perldocs).
  • Word wrap causes the line numbers to be wrong (a single wrapped line has more than one line number)
  • Syntax highlighting is not a perfect fit (perhaps this or these might help):
    • POD and heredoc are both marked as 'Comment2' and so can't be different colours
    • Even though there is a 'Markup' entry, it doesn't seem to be used for POD
    • The colour of dollar signs can't be changed (it's the same as the variable name in vim)
    • Neither variables nor special characters change colour within interpolated strings (as in vim)

Coloured log output

Tail the log and pipe it through this:

#!/usr/bin/perl

use warnings;
use strict;
use Term::ANSIColor qw(color);

while( <> ) {
chomp;
s/\t/ /g;
(/ERROR|failed/) && do {
print( color( 'red' ). $_ .color( 'reset' )."\n" );
next;
};
(/WARN/) && do {
print( color( 'magenta' ). $_ .color( 'reset' )."\n" );
next;
};
(/succeeded/) && do {
print( color( 'green' ). $_ .color( 'reset' )."\n" );
next;
};
(/INFO/) && do {
print( color( 'yellow' ). $_ .color( 'reset' )."\n" );
next;
};
(/DEBUG/) && do {
print( color( 'blue' ). $_ .color( 'reset' )."\n" );
next;
};
print $_."\n";
}

How to change the colours of directories and files

To change the colour of directories from dark blue to light blue (cyan):

    export LS_COLORS=$(echo $LS_COLORS|sed 's/di=01;34/di=01;36/')

See also

    man dircolors

and

    dircolors --print-database



...for a list of the colours.

vi syntax higlighting

Use

:set syn=perl
:set syn=tt2

and

:highlight Comment ctermfg=Green guifg=Green