From 829dfaced39ad2194f1735775b10bc02ba89ce48 Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Mon, 14 May 2018 15:12:06 -0400 Subject: [PATCH] motd --- files/.config/zsh/custom/200-motd.zsh | 7 +- files/.local/bin/motd | 143 ++++++++++++++++++++++++++ files/.profile | 3 + 3 files changed, 147 insertions(+), 6 deletions(-) create mode 100755 files/.local/bin/motd diff --git a/files/.config/zsh/custom/200-motd.zsh b/files/.config/zsh/custom/200-motd.zsh index abda585..fbcecbe 100644 --- a/files/.config/zsh/custom/200-motd.zsh +++ b/files/.config/zsh/custom/200-motd.zsh @@ -1,9 +1,4 @@ if [ "$IS_ROOT" = false ]; then - cat /etc/issue - - # Check tmux - if tmux info &>/dev/null; then - printf "\n%s\n\n" "$(tmux ls)" - fi + motd fi diff --git a/files/.local/bin/motd b/files/.local/bin/motd new file mode 100755 index 0000000..e2ec967 --- /dev/null +++ b/files/.local/bin/motd @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 + +import math +import psutil +import shutil +import sys +import os +import re + +from subprocess import check_call, DEVNULL, CalledProcessError + +try: + from pyfiglet import figlet_format +except ImportError: + def figlet_format(string, *args, **kwargs): + return string + '\n' + +term_r, term_c = os.popen('stty size', 'r').read().split() + +class Termutils: + BLACK = '\33[30m' + RED = '\33[31m' + GREEN = '\33[32m' + YELLOW = '\33[33m' + BLUE = '\33[34m' + MAGENTA = '\33[35m' + CYAN = '\33[36m' + WHITE = '\33[37m' + GREY = '\33[30;1m' + DISABLE = '\033[0m' + + PADDING = ' ' + + @staticmethod + def print_padded(*arg, **kwarg): + print(Termutils.PADDING, end='') + print(*arg, **kwarg) + + @staticmethod + def print_progress_bar(percent, label='', length=60): + percent_color = Termutils.GREEN + bar = '[' + for i in range(0, length): + if i >= math.ceil(percent * length): + bar += Termutils.GREY + elif i == 0: + bar += Termutils.GREEN + elif i == math.ceil(length * 0.6): + bar += Termutils.YELLOW + percent_color = Termutils.YELLOW + elif i == math.ceil(length * 0.85): + bar += Termutils.RED + percent_color = Termutils.RED + + bar += '=' + bar += '{}]{}{:7.2%}{} {}'.format( + Termutils.DISABLE, + percent_color, + percent, + Termutils.DISABLE, + label + ) + Termutils.print_padded(bar) + +def to_gb(bytes_count): + return bytes_count/1024/1024/1024 + +def print_hostname(): + print( + Termutils.CYAN + + figlet_format(os.uname()[1], 'slant') + + Termutils.DISABLE + + '\n' + + os.popen('uptime').read().strip() + + '\n' + ) + +def print_tmux(): + try: + check_call(['tmux', 'info'], stdout=DEVNULL, stderr=DEVNULL) + print('tmux:') + tmux_session = os.popen('tmux display-message -p "#S"', 'r').read().strip() + with os.popen('tmux list-sessions', 'r') as tmux_ls: + for line in tmux_ls: + line = line.strip() + if line.startswith(tmux_session) and 'TMUX' in os.environ: + print(Termutils.GREEN, end='') + Termutils.print_padded( + line + + Termutils.DISABLE) + print('') + except CalledProcessError: + pass + +def print_services(): + if 'MOTD_SERVICES' in os.environ: + print('Services:') + for service in set(os.environ['MOTD_SERVICES'].split()): + Termutils.print_padded('{:<15.15}'.format(service), end='') + try: + check_call(['systemctl', 'is-active', service], stdout=DEVNULL, stderr=DEVNULL) + print('{} active{}'.format(Termutils.GREEN, Termutils.DISABLE)) + except CalledProcessError: + print('{} inactive{}'.format(Termutils.RED, Termutils.DISABLE)) + print('') + + +def print_mem(): + print('Memory:') + memory = psutil.virtual_memory() + Termutils.print_padded('{:.2f}G used {:6.2f}G free {:6.2f}G total'.format( + to_gb(memory.used), + to_gb(memory.free), + to_gb(memory.total) + )) + Termutils.print_progress_bar(memory.used / memory.total) + print('') + + +def print_fs(): + print('Filesystems:') + with open('/etc/mtab') as mtab: + for line in mtab: + line = line.strip() + if line.startswith('/'): + mount_point = line.split()[1] + usage = shutil.disk_usage(mount_point) + Termutils.print_padded('{:<15.15} {:7.2f}G used {:7.2f}G free {:7.2f}G total'.format( + mount_point, + to_gb(usage.used), + to_gb(usage.free), + to_gb(usage.total) + )) + Termutils.print_progress_bar( + percent=(usage.used / usage.total) + ) + print('') + +print_hostname() +print_tmux() +print_services() +print_mem() +print_fs() diff --git a/files/.profile b/files/.profile index f190efd..24e4fc4 100644 --- a/files/.profile +++ b/files/.profile @@ -8,6 +8,9 @@ export VISUAL=vim export EDITOR=vim export PAGER=less +# motd +export MOTD_SERVICES="docker $MOTD_SERVICES" + # firefox export MOZ_USE_XINPUT2=1