diff --git a/diskusage.py b/diskusage.py new file mode 100644 index 0000000..719154b --- /dev/null +++ b/diskusage.py @@ -0,0 +1,47 @@ +import os + +barWidth = 50 + +class bcolors: + EMPTY = '\033[34m' #BLUE + MIDWAY = '\033[33m' #YELLOW + FULL = '\033[31m' #RED + RESET = '\033[0m' #RESET COLOR + UNUSED = '\033[90m' #UNUSED + +# Function to generate usage bar based on percentage of storage in use +def generateBar(percentage): + no_used = int((percentage/100)*barWidth) + bar = '[' + if no_used != 0 and no_used != barWidth: + bar = bar + bcolors.EMPTY + elif no_used == barWidth: + bar = bar + bcolors.FULL + bar = bar + '='*no_used + bar = bar + bcolors.UNUSED + '='*(barWidth - no_used) + return bar + bcolors.RESET + ']' + +mountpoints = ['/', '/dev'] +cmd = 'df -h ' + +# Generate command to be executed +for mount in mountpoints: + cmd = cmd + mount + ' ' + +du = os.popen(cmd).read() # Execute command on system + +# Get percentages from each mounting point in order +percinfo = du.split()[11::6] +percs = [] +for perc in percinfo: + percs.append(int(perc.split('%')[0])) + +# Get full information from df and split it for fancy output +full = du.split('\n') +full = full[:-1] # Get rid of trailing empty string + +# Output disk usage along with bar to indicate usage +print(full[0]) +for i in range(1, len(full)): + print(full[i]) + print(generateBar(percs[i-1]))