]> git.nbdom.net Git - nb.git/commitdiff
pg_count, color.pl
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 8 Jul 2015 09:42:41 +0000 (11:42 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 8 Jul 2015 09:42:41 +0000 (11:42 +0200)
bin/color.pl [new file with mode: 0755]
etc/profile.d/pg.sh [new file with mode: 0644]

diff --git a/bin/color.pl b/bin/color.pl
new file mode 100755 (executable)
index 0000000..58997e8
--- /dev/null
@@ -0,0 +1,195 @@
+#!/usr/bin/perl -w
+use strict qw(subs vars refs);
+###############################################################################
+#                             V E R S I O N S 
+###############################################################################
+my $VERSION = '0.0.2';
+# english only - NB 07.02.14
+# NB 23.11.01 
+# - creation !
+###############################################################################
+#                               G L O B A L E 
+###############################################################################
+my ($NAME) = $0 =~ m#([^/]+)$#;
+my %BG = (
+       'black'         => 40,
+       'red'                   => 41,
+       'green'         => 42,
+       'yellow'                => 43,
+       'blue'          => 44,
+       'magenta'       => 45,
+       'cyan'          => 46,
+       'white'         => 47,
+       'default'       => 49,
+);
+
+my %FG = (
+       'black'         => 30,
+       'red'                   => 31,
+       'green'         => 32,
+       'yellow'                => 33,
+       'blue'          => 34,
+       'magenta'       => 35,
+       'cyan'          => 36,
+       'white'         => 37,
+       'default'       => 39,
+);
+
+my %EFFECT = (
+       'none'                          => 0,
+       'bold'                          => 1,
+       'pale'                          => 2,
+       'evidence'                      => 3,
+       'underline'             => 4,
+       'blink'                         => 5,
+       'invert'                                => 7,
+       'cache'                         => 8,
+       'normal'                                => 22,
+       'no_evidence'           => 23,
+       'no_souligne'           => 24,
+       'no_underline'          => 24,
+       'no_clignotant' => 25,
+       'no_blink'                      => 25,
+       'no_inverse'            => 27,
+       'no_invert'                     => 27,
+);
+###############################################################################
+#                        V A L E U R  P A R  D E F A U T 
+###############################################################################
+my $VERBOSE = 0;
+my $DEBUG = 1;
+my $PRINT_CODE = 0;
+my $BG = '';
+my $EFFECT = '';
+my $FG = '';
+
+###############################################################################
+#                 R E C E P E T I O N  D E S  P A R A M E T R E S 
+###############################################################################
+use Getopt::Long qw(:config default no_ignore_case); my %Opt;
+&GetOptions(\%Opt
+       ,'help|h|?!'
+       ,'verbose|v+'
+       ,'version|V!'
+       ,'debug+'
+       ,'background|bg=s'
+       ,'forground|fg=s'
+       ,'effect|ef=s'
+       ,'print-num|n!'
+       ,'all|a!'
+) || exit -1;
+       &help() if $Opt{'help'};
+       &version() if $Opt{'version'};
+       $VERBOSE = $Opt{'verbose'} if defined($Opt{'verbose'});
+       $DEBUG = $Opt{'debug'} if defined($Opt{'debug'});
+       $PRINT_CODE = $Opt{'print-num'} if defined($Opt{'print-num'});
+       $BG = $Opt{'background'} if defined($Opt{'background'});
+       $EFFECT = $Opt{'effect'} if defined($Opt{'effect'});
+       $FG = $Opt{'forground'} if defined($Opt{'forground'});
+
+###############################################################################
+#                    P R O G R A M M E  P R I N C I P A L 
+###############################################################################
+my $input = join('',@ARGV) || 'My Text';
+
+if ($Opt{all}) {
+
+  my $length = length($input)+3;
+  #use Data::Dumper; die Dumper(\%BG);
+  #print map { $_ = printf "%-".($length+4)."s |\n", "$_ $BG{$_}" } keys %BG;
+  #exit 0;
+# NB 08.08.14   print map { "$BG{$_}\n"
+
+  #while (my ($name,$value) = each %FG) {
+  for my $name (keys %FG) {
+    my $value = $FG{$name};
+
+    printf "%15s", "$name ($value) | ";
+
+    print in_color($input,{
+      fg => $_,
+      bg => $BG,
+      effect => $EFFECT,
+      num => $PRINT_CODE,
+    }) . "\n";
+
+  }
+
+  exit 0;
+}
+
+print in_color($input,{
+  fg => $FG,
+  bg => $BG,
+  effect => $EFFECT,
+  num => $PRINT_CODE,
+}),"\n";
+
+exit 0;
+
+###############################################################################
+#                            F O N C T I O N S 
+###############################################################################
+sub in_color {
+#------------------------------------------------------------------------------
+# NB 18.11.01 
+#------------------------------------------------------------------------------
+my ($txt,$hparam) = @_;
+
+       $hparam->{bg} ||= 'default';
+       $hparam->{fg} ||= 'default';
+       $hparam->{effect} ||= 'normal';
+
+  if ($hparam->{num}) {
+
+    return 
+      '\e['
+      . $BG{$hparam->{bg}}
+      . ";",$FG{$hparam->{fg}}
+      . ";",$EFFECT{$hparam->{effect}}
+      . "m",$txt
+      . '\e[0m'
+    ;
+
+  } else {
+
+    return 
+      "\e["
+      . $BG{$hparam->{bg}}
+      . ";",$FG{$hparam->{fg}}
+      . ";",$EFFECT{$hparam->{effect}}
+      . "m",$txt
+      . "\e[0m"
+    ;
+  }
+}
+sub version {
+#------------------------------------------------------------------------------
+# Affiche la version
+#------------------------------------------------------------------------------
+       print "$NAME: version [$VERSION]\n";
+exit 0;
+}
+
+sub help {
+#------------------------------------------------------------------------------
+# Affiche l'aide
+#------------------------------------------------------------------------------
+#".join("\n",map("  $_",keys(%BG)))."
+print "Usage: $NAME [OPTION...] [TEXT]
+Transform text in ansi color
+
+-bg,    --background: ".join(", ",keys(%BG))."
+-fg,    --forground: ".join(", ",keys(%FG))."
+-ef,    --effect: ".join(", ",keys(%EFFECT))."
+
+-n,     --print-num
+-a,     --all        Show a sample of each type
+
+-h, -?, --help       This help
+-V,     --version    Version
+-v,     --verbose
+-debug, --debug
+";
+exit 0;
+}
diff --git a/etc/profile.d/pg.sh b/etc/profile.d/pg.sh
new file mode 100644 (file)
index 0000000..ad05504
--- /dev/null
@@ -0,0 +1,28 @@
+pg_count() { 
+    declare dbs pwd tables length
+
+    pwd=$PWD;
+    cd /tmp;
+
+    dbs=$@;
+    [ -z "$dbs" ] && dbs=`psql -l | perl -ane 'print $F[0]."\n" if $i++>2 and /^\s*\w/ and !/^\s*template\d/'`
+
+    for db in $dbs; do
+
+      echo "[ $db ]";
+
+      tables=$(sem-psql $db -Atc '\dt' | grep -v '^No relations found' | cut -d\| -f2);
+      length=`echo "$tables" | perl -pe 's/^(.*)$/length($1)/e' | sort -n -r | head -1`;
+
+      (
+        for tb in $tables; do
+          echo "SELECT '$tb',count(*) FROM $tb;";
+        done
+      ) | psql -At $db | awk -F'|' '{printf "%-'$length's %9d\n",$1,$2}';
+
+      echo;
+
+    done;
+
+    cd $pwd
+}