Skip to content

Instantly share code, notes, and snippets.

@duffyjp
Last active December 5, 2022 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duffyjp/5c1f4d25f8e1d3e21bf25c140ee8df9e to your computer and use it in GitHub Desktop.
Save duffyjp/5c1f4d25f8e1d3e21bf25c140ee8df9e to your computer and use it in GitHub Desktop.
Shows interesting information in your terminal
#!/bin/bash
#
# Displays the following in your terminal:
#
# ╔────────────╦──────────╦─────────────╦─────────────╦───────╗
# ║ CPU: 68.7% ║ MEM: 53% ║ DISK: 788GB ║ profile.git ║ arm64 ║
# ╚────────────╩──────────╩─────────────╩─────────────╩───────╝
#
# Usage: * To display the above in new terminals, save this script to a file
# and add a call to the top of your ~/.profile
# * I like to prepend it to pwd with an alias in ~/.bash_profile
# alias pwd='~/scripts/profile.sh && \pwd'
#
# Author: Jacob Duffy https://github.com/duffyjp
# License: 2022 https://opensource.org/licenses/MIT
case $(uname) in
Darwin )
PAGE_SIZE=$(vm_stat | grep -E "page size" | grep -Eo '[0-9]+')
MEM_FREE=$(vm_stat | grep -E "free:|inactive:" | grep -Eo '[0-9]+' | paste -sd+ - | bc | sed "s/$/*$PAGE_SIZE/" | bc)
MEM_TOTAL=$(sysctl -n hw.memsize)
BATT=$(pmset -g batt | grep "discharging" | grep -Eo "\d+%")
ROSETTA=$(sysctl -n sysctl.proc_translated)
;;
Linux )
MEM_FREE=$(grep -E "MemAvail" /proc/meminfo | grep -Eo '[0-9]+')
MEM_TOTAL=$(grep -E "MemTotal" /proc/meminfo | grep -Eo '[0-9]+')
BATT=''
;;
esac
CPU_PERCENT="CPU: $(ps -A -o %cpu | grep -E "[0-9]+" | paste -sd+ - | bc)% ║"
MEM_PERCENT=" MEM: $(( (MEM_TOTAL - MEM_FREE) * 100 / MEM_TOTAL ))% ║"
DISK_FREE=" DISK: $(df -m "$HOME" | tail -1 | grep -Eo " [0-9]+" | head -n3 | tail -1 | sed 's/$/\/1024/' | bc)GB ║"
ARCH=" $(uname -m)"
[ "$(which git)" ] && GIT_REPO=$(git config --get remote.origin.url | grep -Eo "[^\/]*$" | sed 's/\///')
[ -n "$BATT" ] && BATT=" BATT: $BATT ║"
[ -n "$GIT_REPO" ] && GIT_REPO=" $GIT_REPO ║"
STR="${CPU_PERCENT}${MEM_PERCENT}${DISK_FREE}${BATT}${GIT_REPO}${ARCH}"
TOP=$(echo "$STR" | sed 's/[^║]/─/g' | tr ║ ╦)
MID=$(echo "$TOP" | tr ╦ ╩)
MSG="$1 $(printf '%*s' $((${#STR} - ${#1} - 1)))"
if [[ -n "$1" ]]; then BOT=$(echo "$STR" | sed 's/./─/g'); else BOT=$(echo "$TOP" | tr ╦ ╩); fi;
# Color code ARCH if non-native (after determining lengths)
[ "$ROSETTA" = 1 ] && ARCH="\033[0;31m${ARCH}\033[0m"
STR="${CPU_PERCENT}${MEM_PERCENT}${DISK_FREE}${BATT}${GIT_REPO}${ARCH}"
echo "╔─${TOP}─╗"
echo -e "║ ${STR} ║"
[[ -n "$1" ]] && echo "╠─${MID}─╣"
[[ -n "$1" ]] && echo "║ ${MSG} ║"
echo "╚─${BOT}─╝"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment