Skip to content

Instantly share code, notes, and snippets.

@vain
Created April 9, 2012 08:18
Show Gist options
  • Save vain/2342271 to your computer and use it in GitHub Desktop.
Save vain/2342271 to your computer and use it in GitHub Desktop.
Ugly perl colored ls (no separators)
#!/usr/bin/perl -w
use strict;
my $cols = int(`tput cols`);
my $COL_BOLD = "\e[1m";
my $COL_RESET = "\e[0m";
my $COL_BLACK = "\e[30m";
my $COL_RED = "\e[31m";
my $COL_GREEN = "\e[32m";
my $COL_BROWN = "\e[33m";
my $COL_BLUE = "\e[34m";
my $COL_MAGENTA = "\e[35m";
my $COL_CYAN = "\e[36m";
my $COL_WHITE = "\e[37m";
my $COL_LINE;
if (int(`tput colors`) >= 256)
{
$COL_LINE = "\e[38;5;235m";
}
else
{
$COL_LINE = $COL_BOLD . $COL_BLACK;
}
my $total = "";
while (<>)
{
chomp($_);
my @toks;
if ($_ =~ m/^total .+[^:]$/)
{
$total = $_;
next;
}
elsif ($_ =~ m/^(c|b)(...)(...)(...)( +[^ ]+)( +[^ ]+)( +[^ ]+)( +[^,]+, +[^ ]+)( +[^ ]+ +[^ ]+ +[^ ]+)(.*)/
|| $_ =~ m/^(.)(...)(...)(...)( +[^ ]+)( +[^ ]+)( +[^ ]+)( +[^ ]+)( +[^ ]+ +[^ ]+ +[^ ]+)(.*)/)
{
@toks = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
}
if (!defined($toks[0]))
{
print $_ . "\n";
next;
}
$toks[0] =~ s/[^-]/$COL_BLUE$&$COL_RESET/g;
$toks[1] =~ s/[^-]/$COL_BROWN$&$COL_RESET/g;
$toks[2] =~ s/[^-]/$COL_CYAN$&$COL_RESET/g;
$toks[3] =~ s/[^-]/$COL_GREEN$&$COL_RESET/g;
for (my $i = 0; $i <= 3; $i++)
{
$toks[$i] =~ s/-/$COL_LINE─$COL_RESET/g;
}
if ($toks[8] =~ m/:/)
{
$toks[8] = $COL_BLUE . $toks[8];
}
else
{
$toks[8] = $COL_LINE . $toks[8];
}
print
$toks[0] .
$toks[1] .
$toks[2] .
$toks[3] .
$COL_RED . $toks[4] .
$COL_BOLD . $COL_BROWN . $toks[5] . $COL_RESET .
$COL_BROWN . $toks[6] .
$COL_BOLD . $COL_GREEN . $toks[7] . $COL_RESET .
$toks[8] . $COL_RESET .
$COL_LINE . " │" . $COL_RESET .
$toks[9] . $COL_RESET . "\n";
}
if ($total ne "")
{
print $COL_BOLD . $COL_BLACK . $total . $COL_RESET . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment