Skip to content

Instantly share code, notes, and snippets.

@edast
Created May 12, 2011 11:17
Show Gist options
  • Save edast/968337 to your computer and use it in GitHub Desktop.
Save edast/968337 to your computer and use it in GitHub Desktop.
package colprint;
use strict;
use warnings;
use Switch;
{
# Get terminal sizes lines and cols
my $lines = `tput lines`;
my $columns = `tput cols`;
my $column = '0';
# Show our formatters (ok, fail, star and line)
sub pshow {
my ($what, $cols, $color) = @_;
if ($cols) { $column = $cols; }
$color ||= 'default';
switch($color) {
case ('blue') { $color = '[34m' }
case ('green') { $color = '[32m' }
case ('red') { $color = '[31m' }
else { $color = '[m' }
}
switch($what){
case('ok') {
printf "\e[%dG\e[32m * \e[m", $column;
printf "\e[%dG\e[34m[ \e[32mok \e[34m] \e[m\n", $columns-6;
$column = 0;
}
case('fail') {
printf "\e[%dG\e[31m * \e[m", $column;
printf "\e[%dG\e[34m[\e[31mfail\e[34m] \e[m\n", $columns-6;
$column = 0;
}
case('star') {
printf "\e[%dG\e$color * \e[m", $column;
}
case('line') {
printf "\e[%dG ", $column if ($column > 0);
printf "\e$color-\e[m" x ($columns - ($column * 2)) . "\n";
$column = 0;
}
else{ print "\n";}
}
};
# Print a given string. Optional indentation in columns.
sub pbegin {
my ($msg, $cols, $color) = @_;
$msg ||= ' ';
$color ||= 'default';
if ($cols) { $column = $cols; }
pshow('star', $column, $color);
print $msg; # we need it without \n for the checkers
return;
};
sub pinfo {
my ($msg, $cols, $color) = @_;
$msg ||= ' ';
$color ||= 'default';
if ($cols) { $column = $cols; }
pshow('star', $column, $color);
local $\ = "\n"; print $msg;
$column = 0;
return;
};
}
1;
#!/usr/bin/perl
use FindBin qw($Bin);
use lib "$Bin";
use colprint;
# Lets see how this works
colprint::pbegin('Testing some results...');
colprint::pshow('ok');
colprint::pbegin('This is a error result...');
colprint::pshow('fail');
colprint::pbegin('Testing indentation 3...', 3);
colprint::pshow('ok');
colprint::pbegin('Testing indentation 6...', 6);
colprint::pshow('ok');
colprint::pbegin('Returning from indentation a level...', 3);
colprint::pshow('ok');
colprint::pbegin('Indenting again...', 6);
colprint::pshow('ok');
colprint::pbegin('Back one level again...', 3);
colprint::pshow('ok');
colprint::pbegin('Now some color lines...');
colprint::pshow('ok');
colprint::pshow('line');
colprint::pshow('line', 4, 'blue');
colprint::pshow('line', 8, 'green');
colprint::pshow('line', 12, 'red');
colprint::pinfo('A default star');
colprint::pinfo('A blue star, indented 3', 3, 'blue');
colprint::pinfo('A green star, indented 6', 6, 'green');
colprint::pinfo('A red star', 0, 'red');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment