Make uptime tmux segment support psutil
psutil is used if available, and the function falls back to reading /proc/uptime if not. According to the psutil docs this should work across more platforms.
This commit is contained in:
parent
25c5a6d978
commit
4a9ade11b5
|
@ -73,16 +73,20 @@ def external_ip(query_url='http://ipv4.icanhazip.com/'):
|
||||||
|
|
||||||
|
|
||||||
def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'):
|
def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'):
|
||||||
# TODO: make this work with operating systems without /proc/uptime
|
|
||||||
try:
|
try:
|
||||||
with open('/proc/uptime', 'r') as f:
|
import psutil
|
||||||
seconds = int(float(f.readline().split()[0]))
|
from datetime import datetime
|
||||||
minutes, seconds = divmod(seconds, 60)
|
seconds = (datetime.now() - datetime.fromtimestamp(psutil.BOOT_TIME)).seconds
|
||||||
hours, minutes = divmod(minutes, 60)
|
except ImportError:
|
||||||
days, hours = divmod(hours, 24)
|
try:
|
||||||
return format.format(days=int(days), hours=hours, minutes=minutes)
|
with open('/proc/uptime', 'r') as f:
|
||||||
except IOError:
|
seconds = int(float(f.readline().split()[0]))
|
||||||
pass
|
except IOError:
|
||||||
|
return None
|
||||||
|
minutes, seconds = divmod(seconds, 60)
|
||||||
|
hours, minutes = divmod(minutes, 60)
|
||||||
|
days, hours = divmod(hours, 24)
|
||||||
|
return format.format(days=int(days), hours=hours, minutes=minutes)
|
||||||
|
|
||||||
|
|
||||||
@memoize(1800, persistent=True)
|
@memoize(1800, persistent=True)
|
||||||
|
|
Loading…
Reference in New Issue