From a8f64b32296a38d2d650684ffcd08cd95298d2fa Mon Sep 17 00:00:00 2001 From: Sheppy Date: Fri, 24 Feb 2017 23:03:35 +0100 Subject: [PATCH] various fixes and migration to python --- herbstluftwm/autostart | 2 +- herbstluftwm/hl_pracct_deamon.py | 7 +-- herbstluftwm/hl_utils.py | 9 +++- herbstluftwm/panel.sh | 19 ++++---- herbstluftwm/panel_content.py | 76 ++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 16 deletions(-) diff --git a/herbstluftwm/autostart b/herbstluftwm/autostart index 943af64..c276f23 100755 --- a/herbstluftwm/autostart +++ b/herbstluftwm/autostart @@ -14,7 +14,7 @@ xsetroot -solid '#5A8E3A' hc keyunbind --all pkill dunst pkill xautolock -pkill -s 15 hl_pracct_deamon #send SIGTERM for correct termination during write +pkill -s 15 hl_pracct #send SIGTERM for correct termination during write #variables Mod=Mod4 # Use the super key as the main modifier diff --git a/herbstluftwm/hl_pracct_deamon.py b/herbstluftwm/hl_pracct_deamon.py index 45262dd..6c36aae 100755 --- a/herbstluftwm/hl_pracct_deamon.py +++ b/herbstluftwm/hl_pracct_deamon.py @@ -2,13 +2,14 @@ import os import sys import time -from hl_utils import error, is_cip, shexec, color_remove +from hl_utils import error, is_cip, shexec, color_remove, hlpath #Druckerguthaben if is_cip(): while(True): + path = hlpath("pracct.log") out = color_remove(shexec("pr_acct").split("\n")[0]).split(' ')[-1] - with open("pracct.log",'w') as f : + with open(path,'w+') as f: f.write(out) - time.sleep(60) + time.sleep(30) sys.exit() diff --git a/herbstluftwm/hl_utils.py b/herbstluftwm/hl_utils.py index d495e5d..b9781db 100755 --- a/herbstluftwm/hl_utils.py +++ b/herbstluftwm/hl_utils.py @@ -5,6 +5,9 @@ import subprocess import shlex import re +def hlpath(addition=""): + return os.path.join(os.path.expanduser("~"),".config/herbstluftwm/"+addition) + def color_remove(s): '''removes colorcodes from inputstring''' return re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]').sub('',s) @@ -14,7 +17,11 @@ def shexec(s): def is_cip(): u = os.uname() - return "cip" in u.release or "faui" in u.name or "ircbox" in u.name + return "cip" in u.release or "faui" in u.nodename or "ircbox" in u.nodename + +def is_laptop(): + u = os.uname() + return "laptop" in u.nodename or "atlantismedion" in u.nodename def error(s): with open("herbstlog",'a') as f: diff --git a/herbstluftwm/panel.sh b/herbstluftwm/panel.sh index dc3bbb3..723ee4a 100755 --- a/herbstluftwm/panel.sh +++ b/herbstluftwm/panel.sh @@ -118,21 +118,18 @@ hc pad $monitor $panel_height echo -n "^bg()^fg() ${windowtitle//^/^^}" # small adjustments - #powersupply - pwr="NO BATTERY" - if [ $HOSTNAME == atlantislaptop ] || [$HOSTNAME == atlantismedion ] ; then - pwr="$(acpi -b | sed -r 's/Battery [0-9]+: //')" - fi - pracct="" - if [ $HOSTNAME~="faui*" ]; then - pracct="$(pr_acct | head -n 1 | sed -r 's/.*: //' | sed -r 's/\x1b\[[0-9]+m//' | sed -r 's/\x1b\[[0-9]+m//')" - fi + powersupply + #pwr="NO BATTERY" + #if [ $HOSTNAME == atlantislaptop ] || [$HOSTNAME == atlantismedion ] ; then + # pwr="$(acpi -b | sed -r 's/Battery [0-9]+: //')" + #fi - right="^fg(#32CD32) Druckerguthaben: $pracct Euro^bg() ^fg(#ff0000) $pwr $separator^bg() $date $separator" + right="$($HOME/.config/herbstluftwm/panel_content.py) $date" + #right="^fg(#32CD32) Druckerguthaben: -256 Euro^bg() ^fg(#ff0000) $separator^bg() $date $separator" right_text_only=$(echo -n "$right" | sed 's.\^[^(]*([^)]*)..g') # get width of right aligned text.. and add some space.. width=$($textwidth "$font" "$right_text_only ") - echo -n "^pa($(($panel_width - $width)))$right" + echo -n "^pa($(($panel_width - $width - 20)))$right" echo ### Data handling ### diff --git a/herbstluftwm/panel_content.py b/herbstluftwm/panel_content.py index 3442ca4..8e37ef3 100755 --- a/herbstluftwm/panel_content.py +++ b/herbstluftwm/panel_content.py @@ -1 +1,77 @@ +#!/usr/bin/python3 + import hl_utils + +final = "" +date = "" +sep = " | " + +RED = 0xff0000 +GREEN = 0x32CD32 +GREY = 0x909090 +WHITE = 0xefefef +DEFAULT_FG = 0x476243 +COLOR_BORDER = 5.0 + +def color_panel(s,hex_code,seper=True): + if type(hex_code)==int: + hex_code = hex(hex_code) + hex_code = hex_code.lstrip('0x') + if seper: + sep=color_panel('|',DEFAULT_FG,False) + else: + sep = "" + return "^fg(#" + hex_code + ") " + s + "^bg()"+sep + +def get_color(nr,start,end): + if end == start or nr > end: + return hex(GREEN) + else: + r,g,b = 0,0,0 + interval = 256 + 256 + custom_interval = abs(start-end) + div = float(interval)/float(custom_interval) + if div >= interval/3: + error("Interval for coloring too small, using default") + return WHITE + nr = nr*div + if custom_interval > interval: + custom_interval = interval + if nr > 256: + g = 0xFF + r = int(abs(nr - (256+256))) #counts down reverse to nr + b = 0 + elif nr > 0: + g = int(nr - 256) + r = 0xFF + b = 0 + else: + error("Negative interval value???") + r = r << 16 + g = g << 8 + tmp_col = r + g + b + if tmp_col > 0xFFFF00: + error("color value too high") + return hex(tmp_col) + + +def guthaben(): + if hl_utils.is_cip(): + tmp = -1 + with open(hl_utils.hlpath("pracct.log")) as f: + tmp = float(f.read()); + guthaben = "Druckerguthaben: " + str(tmp) + " Euro" + col = get_color(tmp,0,COLOR_BORDER) + guthaben = color_panel(guthaben,col) + return guthaben; + +def battery(): + if hl_utils.is_laptop(): + try: + return color_panel(hl_utils.shexec("acpi -b | sed -r 's/Battery [0-9]+: //"),RED) + except(ValueError): + return color_panel("acpi or sed not in path",RED) + else: + return "" + +print(guthaben(),battery())