#!/usr/bin/env python3

import os, sys, subprocess, optparse, re, linecache
from subprocess import Popen, PIPE
from optparse import OptionParser
from getpass import getuser
from time import ctime, sleep

labels = ['OS', "User", "Hostname", "Kernel", "Uptime", "WM", "Shell", "Terminal", "Packages", "CPU", "RAM", "Disk"]

class tcolors:

	for arg in sys.argv:
		if arg == "-r":
			table = [
			'\x1b[0;31m', # Red
			'\x1b[0;33m', # Yellow
			'\x1b[0;32m', # Green
			'\x1b[0;34m', # Blue
			'\x1b[0;35m', # Purple
			'\x1b[0;31m', # Red
			'\x1b[0;33m', # Yellow
			'\x1b[0;32m', # Green
			'\x1b[0;34m', # Blue
			'\x1b[0;35m', # Purple
			'\x1b[0;31m', # Red
			'\x1b[0;33m', # Yellow
			'\x1b[0;32m', # Green
			'\x1b[0;34m', # Blue
			'\x1b[0;35m', # Purple
			'\x1b[0;31m', # Red
			'\x1b[0;33m', # Yellow
			'\x1b[0;32m', # Green
			'\x1b[0;34m', # Blue
			'\x1b[0;35m', # Purple
			'\x1b[0m' # Clear
			]
		else:
			table = [None] * len(labels)
			for x in range(0, len(labels)):
				table[x] = '\x1b[0m' # Clear


def display(label, string, color):
	print(color + "█ " + label + ": " + tcolors.table[len(tcolors.table) - 1] + string)

# Info #

def distro():
	if os.path.exists('/etc/pacman.conf'):
		return 'Arch Linux'
	else:
		sys.exit(1)

def user():
	return os.getenv('USER')

def hostname():
	return Popen(['uname', '-n'], stdout=PIPE).communicate()[0].decode('Utf-8').rstrip('\n')

def kernel():
	return Popen(['uname', '-r'], stdout=PIPE).communicate()[0].decode('Utf-8').rstrip('\n')

def uptime():
	fuptime = int(open('/proc/uptime').read().split('.')[0])
	day = int(fuptime / 86400)
	fuptime = fuptime % 86400
	hour = int(fuptime / 3600)
	fuptime = fuptime % 3600
	minute = int(fuptime / 60)
	uptime = ''
	if day == 1:
		uptime += '%d day, ' % day
	if day > 1:
		uptime += '%d days, ' % day
	uptime += '%d:%02d' % (hour, minute)
	return uptime

def wm():
	return "i3-gaps"

def shell():
	return os.getenv('SHELL')

def terminal():
	return os.getenv('TERM')

def packages():
	p1 = Popen(['pacman', '-Q'], stdout=PIPE).communicate()[0].decode("Utf-8")
	return str(len(p1.rstrip('\n').split('\n')))

def cpu():
	file = open('/proc/cpuinfo').readlines()
	return re.sub('  +', ' ', file[4].replace('model name\t: ', '').rstrip('\n'))

def ram():
	raminfo = Popen(['free', '-m'], stdout=PIPE).communicate()[0].decode('Utf-8').split('\n')
	ram = ''.join(filter(re.compile('M').search, raminfo)).split()
	#ramvalue = round(int(ram[1]) / 1024) # Convert Mb to Gb
	return str(ram[1]) + "MB"

def disk():
	p1 = Popen(['df', '-Tlh', '--total', '-t', 'ext4', '-t', 'ext3', '-t', 'ext2', '-t', 'reiserfs', '-t', 'jfs', '-t', 'ntfs', '-t', 'fat32', '-t', 'btrfs', '-t', 'fuseblk', '-t', 'xfs'], stdout=PIPE).communicate()[0].decode("Utf-8")
	total = p1.splitlines()[-1]
	used = total.split()[3]
	size = total.split()[2]
	usedpercent = float(total.split()[5][:-1])
	return str(used) + "/" + str(size)

info = [distro(), user(), hostname(), kernel(), uptime(), wm(), shell(), terminal(), packages(), cpu(), ram(), disk()]

for x in range(0, len(labels)):
	display(labels[x], info[x], tcolors.table[x])