Skip to content

Instantly share code, notes, and snippets.

@elijahmanor
Last active February 18, 2022 22:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save elijahmanor/5a27a34c4c26d91452cd5784b1f893f7 to your computer and use it in GitHub Desktop.
Tmux Status Right
package main
import (
"fmt"
"strings"
"os/exec"
"strconv"
)
const white = "#f8f8f2"
const gray = "#44475a"
const dark_gray = "#282a36"
const light_purple = "#bd93f9"
const dark_purple = "#6272a4"
const cyan = "#8be9fd"
const green = "#50fa7b"
const orange = "#ffb86c"
const red = "#ff5555"
const pink = "#ff79c6"
const yellow = "#f1fa8c"
func run(command string) string {
out, _ := exec.Command("sh", "-c", command).Output()
return strings.TrimSpace(string(out))
}
func main() {
cpu(pink, dark_purple)
battery(orange, dark_purple)
node_npm_version(yellow, dark_purple)
mrwatson(green, dark_purple)
datetime(light_purple, dark_purple)
fmt.Printf(" ")
}
func cpu(bg string, fg string) {
// Logic borrowed from https://github.com/dracula/tmux
value, _ := strconv.Atoi(run("ps -A -o %cpu | awk -F. '{s+=$1} END {print s}'"))
scores, _ := strconv.Atoi(run("sysctl -n hw.logicalcpu"))
usage := value / scores
fmt.Printf("#[fg=%s]#[fg=%s]#[bg=%s]  %02d%% #[bg=%s]", bg, fg, bg, usage, bg)
}
func battery(bg string, fg string) {
status := run("pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d ' '")
batt := run("pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%'")
percentage := fmt.Sprintf("%03s", batt);
chargingMap := [10]string{"", "", "", "", "", "", "", "", "", ""}
chargedMap := [10]string{"", "", "", "", "", "", "", "", "", ""}
icon := ""
if status == "charging" {
if percentage == "100%" {
icon = ""
} else {
icon = chargingMap[percentage[0]]
}
} else {
if percentage == "100%" {
icon = ""
} else {
icon = chargedMap[percentage[0]]
}
}
fmt.Printf("#[fg=%s]#[fg=%s]#[bg=%s] %s %s #[bg=%s]", bg, fg, bg, icon, percentage, bg)
}
func node_npm_version(bg string, fg string) {
node_version := run("node --version | sed -e 's/v//g'")
npm_version := run("npm --version")
fmt.Printf("#[fg=%s]#[fg=%s]#[bg=%s]  %s  %s #[bg=%s]", bg, fg, bg, node_version, npm_version, bg)
}
func mrwatson(bg string, fg string) {
status := ""
if run("watson status") == "No project started." {
status = ""
}
total := run("watson report -dcG | grep 'Total:' | sed 's/Total: //'")
fmt.Printf("#[fg=%s]#[fg=%s]#[bg=%s] %s %s #[bg=%s]", bg, fg, bg, status, total, bg)
}
func datetime(bg string, fg string) {
date := run("date +'%h-%d %I:%M %p'")
fmt.Printf("#[fg=%s]#[fg=%s]#[bg=%s]  %s #[bg=%s]", bg, fg, bg, date, bg)
}
#!/usr/bin/env node
const { promisify } = require('util');
const exec = promisify(require('child_process').exec);
const run = async command => {
const { stdout } = await exec(command);
return stdout.toString().trim();
};
const colors = {
white:'#f8f8f2',
gray:'#44475a',
dark_gray:'#282a36',
light_purple:'#bd93f9',
dark_purple:'#6272a4',
cyan:'#8be9fd',
green:'#50fa7b',
orange:'#ffb86c',
red:'#ff5555',
pink:'#ff79c6',
yellow:'#f1fa8c'
};
async function cpu(bg, fg=colors.dark_gray) {
// Logic borrowed from https://github.com/dracula/tmux
const value = parseFloat(await run("ps -A -o %cpu | awk -F. '{s+=$1} END {print s}'"));
const scores = parseFloat(await run("sysctl -n hw.logicalcpu"));
const usage = parseInt(value / scores).toString().padStart(2, "0");
return `#[fg=${bg}]#[fg=${fg}]#[bg=${bg}]  ${usage}% #[bg=${bg}]`;
}
async function battery(bg, fg=colors.dark_gray) {
const status = await run("pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d ' '");
const batt = await run("pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%'");
const percentage = batt.padStart(3, "0");
const chargingMap = ["", "", "", "", "", "", "", "", "", ""];
const chargedMap = ["", "", "", "", "", "", "", "", "", ""];
let icon = "";
if (status === "charging") {
icon = percentage === "100%" ? "" : chargingMap[percentage[0]];
} else {
icon = percentage === "100%" ? "" : chargedMap[percentage[0]];
}
return `#[fg=${bg}]#[fg=${fg}]#[bg=${bg}] ${icon} ${percentage} #[bg=${bg}]`;
}
async function node_npm_version(bg, fg=colors.dark_gray) {
const node_version = await run("node --version | sed -e 's/v//g'");
const npm_version = await run("npm --version");
return `#[fg=${bg}]#[fg=${fg}]#[bg=${bg}]  ${node_version}${npm_version} #[bg=${bg}]`;
}
async function mrwatson(bg, fg=colors.dark_gray) {
let status = await run("watson status");
status = status === "No project started." ? "" : "";
const total = await run("watson report -dcG | grep 'Total:' | sed 's/Total: //'");
return `#[fg=${bg}]#[fg=${fg}]#[bg=${bg}] ${status} ${total} #[bg=${bg}]`;
}
async function datetime(bg, fg=colors.dark_gray) {
const date = await run("date +'%h-%d %I:%M %p'");
return `#[fg=${bg}]#[fg=${fg}]#[bg=${bg}]  ${date}`;
}
async function main() {
let statusRight = "";
statusRight += await cpu(colors.pink);
statusRight += await battery(colors.orange);
statusRight += await node_npm_version(colors.yellow);
statusRight += await mrwatson(colors.green);
statusRight += await datetime(colors.light_purple);
console.log(statusRight);
}
main();
#!/bin/bash
white='#f8f8f2'
gray='#44475a'
dark_gray='#282a36'
light_purple='#bd93f9'
dark_purple='#6272a4'
cyan='#8be9fd'
green='#50fa7b'
orange='#ffb86c'
red='#ff5555'
pink='#ff79c6'
yellow='#f1fa8c'
function cpu() {
# Logic borrowed from https://github.com/dracula/tmux
local cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}')
local cpucores=$(sysctl -n hw.logicalcpu)
local cpuusage=$((cpuvalue / cpucores))
printf "#[fg=$1]#[fg=$2]#[bg=$1]  %02d%% #[bg=$1]" "${cpuusage}"
}
function battery() {
local status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ")
local batt=$(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%')
local percentage=$(printf "%03s" $batt)
local chargingMap=("" "" "" "" "" "" "" "" "" "")
local chargedMap=("" "" "" "" "" "" "" "" "" "")
local icon=""
if [[ $status == "charging" ]]; then
if [[ $percentage == "100%" ]]; then
icon=""
else
icon=${chargingMap[${percentage:0:1}]}
fi
else
if [[ $percentage == "100%" ]]; then
icon=""
else
icon=${chargedMap[${percentage:0:1}]}
fi
fi
printf "#[fg=$1]#[fg=$2]#[bg=$1] %s %s #[bg=$1]" "${icon}" "${percentage}"
}
function mrwatson() {
local status=""
if [[ "$(watson status)" == "No project started." ]]; then
status=""
fi
local total=$(watson report -dcG | grep 'Total:' | sed 's/Total: //')
printf "#[fg=$1]#[fg=$2]#[bg=$1] %s %s #[bg=$1]" "${status}" "${total}"
}
function node_npm_version() {
local node_version=$(node --version | sed -e 's/v//g')
local npm_version=$(npm --version)
printf "#[fg=$1]#[fg=$2]#[bg=$1]  %s  %s #[bg=$1]" "${node_version}" "${npm_version}"
}
function datetime() {
printf "#[fg=$1]#[fg=$2]#[bg=$1]  %s " "$(date +'%h-%d %I:%M %p')"
}
function main() {
cpu "$pink" "$dark_gray"
battery "$orange" "$dark_gray"
node_npm_version "$yellow" "$dark_gray"
mrwatson "$green" "$dark_gray"
datetime "$light_purple" "$dark_gray"
printf " "
}
main
@elijahmanor
Copy link
Author

Screen Shot 2022-02-18 at 4 24 50 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment