html/df.pl


   1 use Irssi;
   2 use Irssi::TextUI;
   3 use strict;
   4 
   5 use vars qw($VERSION %IRSSI);
   6 
   7 $VERSION="0.1.0";
   8 %IRSSI = (
   9 	authors=> 'Jochem Meyers',
  10 	contact=> 'jochem.meyers@gmail.com',
  11 	name=> 'df',
  12 	description=> 'Adds an item which displays the current disk usage.',
  13 	license=> 'GPL v2 or later',
  14 	url=> 'http://kaede.kicks-ass.net/irssi.html',
  15 );
  16 
  17 #########
  18 # INFO
  19 ###
  20 #
  21 #  Type this to add the item:
  22 #
  23 #    /statusbar window add df
  24 #
  25 #  See
  26 #  
  27 #    /help statusbar
  28 #
  29 #  for more help on how to custimize your statusbar.
  30 #
  31 #  If you want to change the way the item looks, browse down to where it reads
  32 #
  33 #  $output .= ' [' . $device . ': A: ' . $avail{$device} . ' U%%: ' . $use{$device} . ']';
  34 #
  35 #  and add or remove any of the following:
  36 #  $size{$device} is the total size of the drive
  37 #  $used{$device} is the total amount of used space
  38 #  $avail{$device} is the amount of available space
  39 #  $use{$device} is the percentage of space used
  40 #  $mount{$device} is the mount point
  41 #
  42 #  Next version, if I ever get around to making one, will have an easier system of changing the 
  43 #  way the statusbar item looks.
  44 #
  45 #  There's a command defined, /dfupdate, which will instantly update the statusbar item. If you
  46 #  want this information printed in the statuswindow, use /exec df -h in any window :).
  47 #
  48 ############
  49 # OPTIONS
  50 ######
  51 #
  52 #  The irssi command /set can be used to change these settings (more to follow):
  53 #  * df_refresh_time (default: 60)
  54 #      The number of seconds between updates.
  55 #
  56 ###
  57 #########
  58 # TODO
  59 ###
  60 #
  61 #  - Add format support so the display is more easily customizable.
  62 #  - Add a list of devices to display.
  63 #  - Add a setting that'll let user define the switches to pass to df?
  64 #
  65 #########
  66 
  67 #definte variables
  68 my $output;
  69 my ($df_refresh_tag);
  70 my $sbitem;
  71 my (%size, %used, %avail, %use, %mount);
  72 
  73 #get information about the harddrives
  74 sub getDiskInfo()
  75 {
  76 	my @list;
  77 	my $skip_line_one = 1;
  78 
  79 	open(FID, "/bin/df -h|");
  80 	while (<FID>)
  81 	{
  82 		if ($skip_line_one > 0)
  83 		{
  84 			$skip_line_one--;
  85 			next;
  86 		}
  87 		my $line = $_;
  88 		$line =~ s/[\s:]/ /g;
  89 		@list = split(" ", $line);
  90 		$list[0] =~ s/\/dev\///g;
  91 		$size{$list[0]} = $list[1];
  92 		$used{$list[0]} = $list[2];
  93 		$avail{$list[0]} = $list[3];
  94 		$use{$list[0]} = $list[4];
  95 		$mount{$list[0]} = $list[5];
  96 		$skip_line_one--;
  97 		if ($skip_line_one < -100) {
  98 			Irssi::print("More than 100 drives, this can't be.");
  99 			return;
 100 		}
 101 	}
 102 	close(FID);
 103 }
 104 
 105 #called by irssi to get the statusbar item
 106 sub sb_df()
 107 {
 108 	my ($item, $get_size_only) = @_;
 109 	$item->default_handler($get_size_only, "{sb $sbitem}", undef, 1);
 110 }
 111 
 112 sub test()
 113 {
 114 	refresh_df();
 115 }
 116 #refresh the statusbar item
 117 sub refresh_df()
 118 {
 119 	getDiskInfo();
 120 	$output = "";
 121 	$sbitem = "";
 122 	my @devices = keys(%size);
 123 	my $device;
 124 	foreach $device (@devices)
 125 	{
 126 		$output .= ' [' . $device . ': A: ' . $avail{$device} . ' U%%: ' . $use{$device} . ']';
 127 	}
 128 	$sbitem = 'DF' . $output;
 129 	Irssi::statusbar_items_redraw('df');
 130 	if ($df_refresh_tag)
 131 	{
 132 		Irssi::timeout_remove($df_refresh_tag)
 133 	}
 134 	my $time = Irssi::settings_get_int('df_refresh_time');
 135 	$df_refresh_tag = Irssi::timeout_add($time*1000, 'refresh_df', undef);
 136 }
 137 
 138 #register the statusbar item
 139 Irssi::statusbar_item_register('df', undef, 'sb_df');
 140 
 141 #add settings
 142 Irssi::settings_add_int('misc', 'df_refresh_time', 60);
 143 
 144 Irssi::command_bind('dfupdate','test');
 145 
 146 #run refresh_df() once so sbitem has a value
 147 refresh_df();
 148 
 149 ################
 150 ###
 151 # Changelog
 152 # Version 0.1.0
 153 #  - initial release
 154 #
 155 ###
 156 ################