118 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Perl
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			Perl
		
	
	
	
	
	
#!/usr/bin/perl
 | 
						|
 | 
						|
# Companion script:
 | 
						|
 | 
						|
# #!/bin/bash
 | 
						|
# cd
 | 
						|
#while true; do
 | 
						|
#  tput civis;
 | 
						|
#  clear; tput smul;
 | 
						|
#  printf "%+50s" "(every 5 mins)";
 | 
						|
#  tput cub 99; date; tput sgr0; echo;
 | 
						|
#  perl df-highlight.pl -c 3 -p;
 | 
						|
#  echo; uptime | cut -d, -f4- | awk '{$1=$1};1' # https://unix.stackexchange.com/a/205854
 | 
						|
#  tput cnorm; sleep 300;
 | 
						|
#done
 | 
						|
 | 
						|
use warnings;
 | 
						|
use strict;
 | 
						|
use Term::ANSIColor;
 | 
						|
use Time::HiRes qw(usleep nanosleep);
 | 
						|
use Time::localtime;
 | 
						|
 | 
						|
# "User modifiable" data
 | 
						|
my $DFCMD="/usr/bin/df";
 | 
						|
my $TPUT="/usr/bin/tput";
 | 
						|
my $DFARGS="-h";
 | 
						|
my $CLEAR_SCREEN=`/usr/bin/clear`;
 | 
						|
 | 
						|
# Arguments
 | 
						|
my ($CLS, $RBG, $ARGDEX, $MY_COL) = (0,0,0,3);
 | 
						|
for (@ARGV) {
 | 
						|
  if ($_ eq "-p") {$RBG=1;}
 | 
						|
  elsif ($_ eq "-c") {$MY_COL=$ARGV[$ARGDEX+1];}
 | 
						|
  elsif ($_ eq "--help" or $_ eq "-h" ) {get_help();}
 | 
						|
  $ARGDEX++;
 | 
						|
}
 | 
						|
 | 
						|
# Static data
 | 
						|
my $ROW_START=`$TPUT cub 120`; # If this doesn't work on your terminal, make the value more than 120 lol
 | 
						|
my $UP_ONE_ROW=`$TPUT cuu1`;
 | 
						|
my $CLEAR_TO_END=`$TPUT el`;
 | 
						|
my @HEADERS=qw(MT Filesystem Size Used Avail 'Use%' 'Mounted on');
 | 
						|
my @COLORS=qw(MT bright_red bright_yellow bright_green bright_cyan bright_blue bright_magenta);
 | 
						|
my @COLWIDTHS=qw(0 28 6 6 7 6 10);
 | 
						|
my $COLOR_OFFSET=9;
 | 
						|
 | 
						|
# Helper functions
 | 
						|
# https://stackoverflow.com/a/5047362
 | 
						|
sub clean {my $text = shift; $text=~s/\n//g; $text=~s/\r//g;return $text;}
 | 
						|
sub randcol {return int(rand(6)+1);}
 | 
						|
sub cachemath {my $lcache=(localtime->min*60)+localtime->sec;return $lcache;}
 | 
						|
sub get_help {
 | 
						|
  my ($underline,$undereset)=(`tput smul`,`tput sgr0`);
 | 
						|
  my @help_columns=qw(12);
 | 
						|
  printf("\n%s%s%s\n\n","$underline","DF Highlight Script Usage","$undereset");
 | 
						|
  printf("");
 | 
						|
  printf(" %-$help_columns[0]s %s\n","-h, --help","Displays this help and exits");
 | 
						|
  printf(" %-$help_columns[0]s %s\n","-c #","Highlights the specified column (default: 3)");
 | 
						|
  printf(" %-$help_columns[0]s %s\n","-p","Enables a pinball-esque color mode before highlighting the chosen column");
 | 
						|
  exit 0;
 | 
						|
}
 | 
						|
 | 
						|
# Runtime data
 | 
						|
my @linedata=();
 | 
						|
my $line="";
 | 
						|
my @dfdata=();
 | 
						|
my $dfcache=0;
 | 
						|
my $current_column=1;
 | 
						|
my @colored_cols=qw'0';
 | 
						|
my $rows=0;
 | 
						|
 | 
						|
sub dfboi {
 | 
						|
  my $colored_column = $_[0];
 | 
						|
  my $COLOR = $COLORS[$colored_column];
 | 
						|
  if ($dfcache == 0 or cachemath()-$dfcache > 60) {
 | 
						|
    # REFRESH DF DATA
 | 
						|
    @dfdata=(); # Clear the array to avoid duplicating data
 | 
						|
    open my $cmd, '-|', "$DFCMD $DFARGS";
 | 
						|
    while (my $line = <$cmd>) {
 | 
						|
      push(@dfdata,$line); # P O P U L A T E
 | 
						|
    }
 | 
						|
    close $cmd;
 | 
						|
    $dfcache=cachemath();
 | 
						|
    $rows=scalar @dfdata;
 | 
						|
  }
 | 
						|
  for (@dfdata) {
 | 
						|
    $line=$_;
 | 
						|
    $current_column=1;
 | 
						|
    #             Filesystem            Size             Used             Avail            Use%        Mounted
 | 
						|
    if ($line =~ /([a-zA-Z0-9\/\-_]+)\s+([0-9GMKT\.]+)\s+([0-9GMKT\.]+)\s+([0-9GMKT\.]+)\s+([0-9%]+)\s+([\/a-z\-0-9A-Z]+)/ or $line =~ /^(Filesystem)\s+(Size)\s+(Used)\s+(Avail)\s+(Use%)\s+(Mounted on)/) {
 | 
						|
      @linedata=("MT", "$1", "$2", "$3", "$4", "$5", "$6");
 | 
						|
      while ($current_column <= 6) {
 | 
						|
        if ($current_column == $colored_column) {
 | 
						|
          $colored_cols[0]=$COLWIDTHS[$current_column]+$COLOR_OFFSET;
 | 
						|
          printf("%-$colored_cols[0]s",clean(colored("$linedata[$current_column]", $COLOR)));
 | 
						|
        } else {
 | 
						|
          printf("%-$COLWIDTHS[$current_column]s",clean("$linedata[$current_column]"));
 | 
						|
        }
 | 
						|
        $current_column=$current_column+1;
 | 
						|
      }
 | 
						|
    }
 | 
						|
    printf("\n");
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
if ($RBG) {
 | 
						|
  my $blinky=10;
 | 
						|
  my $undoline=0;
 | 
						|
  while ($blinky--) {
 | 
						|
    dfboi(randcol());
 | 
						|
    select()->flush();
 | 
						|
    usleep($blinky < 2 ? 600000 : $blinky < 4 ? 300000 : 150000);
 | 
						|
    $undoline=0;
 | 
						|
    while ($undoline < $rows) {printf("%s",$UP_ONE_ROW);$undoline++;}
 | 
						|
  }
 | 
						|
}
 | 
						|
dfboi($MY_COL);
 |