diff --git a/perl/df-highlight.pl b/perl/df-highlight.pl new file mode 100644 index 0000000..5dc6a60 --- /dev/null +++ b/perl/df-highlight.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use Term::ANSIColor; +use Time::HiRes qw(usleep nanosleep); +use Time::localtime; +use Getopt::Long; + +# "User modifiable" data +my $MY_COL=3; +my $DFCMD="/usr/bin/df"; +my $TPUT="/usr/bin/tput"; +my $DFARGS="-h"; +my $CLEAR_SCREEN=`/usr/bin/clear`; + +# Static data +#GetOptions("=s" => \( my $INTERVAL=300 ),"header|h=i" => \( my $HEADER_REPEAT = 24 ),) or die "command line arguments error"; +my $CLS = grep( /^-c/, @ARGV ) ? 1 : 0; +my $RBG = grep( /^-p/, @ARGV ) ? 1 : 0; +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;} + +# 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"); + } +} + +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);