prompt: remove task count, add command duration, change memory+swap ui

This commit is contained in:
Martin Sekera 2021-03-26 02:21:41 +01:00
parent a54fd956bf
commit 6868e223dc
2 changed files with 64 additions and 33 deletions

View file

@ -1,5 +1,7 @@
#! /bin/zsh
zmodload zsh/datetime
OVER_PROMPT_CFG="/etc/over/env/prompt.cfg"
if [[ -a "$OVER_PROMPT_CFG" ]]; then
@ -14,13 +16,15 @@ function __over_prompt_set_title {
function __over_prompt_preexec {
__over_prompt_set_title "${USER}: ${PWD} -> ${1}"
OVER_PROMPT_CMD_START=${EPOCHSECONDS}
}
function __over_prompt_precmd {
__over_prompt_set_title "${USER}: ${PWD}"
python3 /usr/share/over-env/prompt.py "${COLUMNS}" "${PWD}" "${OVER_PROMPT_LAST_REFRESH}" "${OVER_PROMPT_SYSLOAD_WARN:=1.25}" "${OVER_PROMPT_SYSLOAD_YELL:=3.0}" "${OVER_PROMPT_FREESPACE_WARN:=0.15}" "${OVER_PROMPT_FREESPACE_YELL:=0.05}"
python3 /usr/share/over-env/prompt.py "${COLUMNS}" "${PWD}" "${OVER_PROMPT_LAST_REFRESH}" "${OVER_PROMPT_SYSLOAD_WARN:=1.25}" "${OVER_PROMPT_SYSLOAD_YELL:=3.0}" "${OVER_PROMPT_FREESPACE_WARN:=0.15}" "${OVER_PROMPT_FREESPACE_YELL:=0.05}" "${OVER_PROMPT_CMD_START}"
PS1="$(print "%(?.%{\e[1;36m%}.%{\e[1;31m%}%?%{\e[0m%}:%{\e[1;31m%})%(\!.#.$)%{\e[0m%} ")"
OVER_PROMPT_LAST_REFRESH=$(date +%s)
OVER_PROMPT_LAST_REFRESH=${EPOCHSECONDS}
OVER_PROMPT_CMD_START=0
}
unset OVER_PROMPT_CFG

View file

@ -109,6 +109,39 @@ def space_string(free):
return "%.1f%s" if free > 1024 else "%d%s"
def timestamp_to_hms(t):
"""
Converts a duration in seconds to a [[?h ]?m ]?s representation.
>>> timestamp_to_hms(12345)
'3h 25m 45s'
>>> timestamp_to_hms(3600)
'1h 0m 0s'
>>> timestamp_to_hms(1234)
'20m 34s'
>>> timestamp_to_hms(12)
'12s'
"""
hh = t // 3600
mm = (t % 3600) // 60
ss = t % 60
out = []
if hh:
out.append("%dh" %(hh))
if mm or hh:
out.append("%dm" %(mm))
out.append("%ds" %(ss))
return " ".join(out)
class Sysload:
def __init__(self):
loadavg = open("/proc/loadavg").read().split()
@ -129,6 +162,7 @@ class Settings:
self.load_error = float(argv[5])
self.space_warn = float(argv[6])
self.space_error = float(argv[7])
self.cmd_start_time = int(argv[8]) if argv[8] else None
self.is_root = os.geteuid() == 0
self.is_daemon = os.geteuid() < 1000
except:
@ -512,13 +546,12 @@ class Padding(Part):
class StatsPart(Part):
"""
[ ?Δ1? clock | sysload tasks | m:memory ?s:swap? mountpoint_spaces...
[ [duration @] [Δx] clock | sysload memory[+swap] | mountpoint_spaces...
- duration is shown if > 5s
- delta is shown if the last prompt refresh was before midnight, and displays how many midnights elapsed since
- clock shows current time (in HH:MM)
- sysload is current loadavg divided by amount of cores (in 1)
- tasks is the total amount of tasks on the system
- memory shows current free memory (in octets)
- swap shows remaining swap space but only if any is actually used
- clock shows refresh time (HH:MM)
- sysload is current loadavg divided by amount of cores
- memory shows current free memory and swap, if available
- mountpoint_spaces list one free space per mountpoint
"""
def __init__(self, settings, sysload):
@ -527,11 +560,21 @@ class StatsPart(Part):
self.fragments.append("[ ")
self.fragments.append(style_bold())
now = datetime.datetime.now()
# last command duration
if settings.cmd_start_time:
duration = (now.timestamp() - settings.cmd_start_time)
if duration > 5:
self.fragments.append("%s " %(timestamp_to_hms(duration)))
self.fragments.append("@ ")
# delta
if settings.last_refresh_time:
then = datetime.datetime.fromtimestamp(settings.last_refresh_time).replace(hour=0, minute=0, second=0, microsecond=0)
now = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
midnights = (now - then).days
last_cmd_midnight = datetime.datetime.fromtimestamp(settings.last_refresh_time).replace(hour=0, minute=0, second=0, microsecond=0)
last_midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
midnights = (last_midnight - last_cmd_midnight).days
if midnights:
self.fragments.append(style_color(COLOR_CLOCK_DELTA))
@ -545,9 +588,6 @@ class StatsPart(Part):
self.fragments.append("%.1f " %(sysload.load1))
self.fragments.append(style_reset())
# tasks
self.fragments.append("%d " %(sysload.tasks_total))
if sysload.load1 >= settings.load_error:
self.fragments.append(" | ")
self.fragments.append(style_color(COLOR_ERROR))
@ -562,39 +602,26 @@ class StatsPart(Part):
swap_total = 0
swap_free = 0
# MemAvailable is not available on <linux-3.14 and we'll bitch about that
old_linux = True
with open("/proc/meminfo") as f:
for line in f:
if line.startswith("MemTotal"):
mem_total = int(line.split()[1]) * 1024
elif line.startswith("MemAvailable"):
mem_free = int(line.split()[1]) * 1024
old_linux = False
elif line.startswith("SwapTotal"):
swap_total = int(line.split()[1]) * 1024
elif line.startswith("SwapFree"):
swap_free = int(line.split()[1]) * 1024
if old_linux:
self.fragments.append(style_color(COLOR_ERROR))
self.fragments.append(style_bold())
self.fragments.append("⚠ <linux-3.14 ⚠")
else:
self.fragments.append(style_color(COLOR_MEMSWAP))
self.fragments.append("m")
self.fragments.append(style_bold())
self.fragments.append(style_color(settings.get_space_color(mem_free, mem_total)))
mem_free_si = si_number(mem_free)
self.fragments.append(space_string(mem_free) %(mem_free_si[0], mem_free_si[1]))
self.fragments.append(style_bold())
self.fragments.append(style_color(settings.get_space_color(mem_free, mem_total)))
mem_free_si = si_number(mem_free)
self.fragments.append(space_string(mem_free) %(mem_free_si[0], mem_free_si[1]))
self.fragments.append(style_reset())
if swap_total - swap_free > 2**20:
self.fragments.append(style_color(COLOR_MEMSWAP))
self.fragments.append(" s")
self.fragments.append(style_bold())
if swap_total:
self.fragments.append("+")
self.fragments.append(style_color(settings.get_space_color(swap_free, swap_total)))
swap_free_si = si_number(swap_free)
self.fragments.append(space_string(swap_free) %(swap_free_si[0], swap_free_si[1]))