brewbreadbeard

Untitled

Sep 15th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf8 -*-
  3.  
  4. # <Settings>
  5. #I3_CONNECTION            = i3ipc.Connection()
  6. PRIMARY_SOUND_DEV        = "alsa_output.usb-Logitech_Logitech_USB_Headset-00.analog-stereo"
  7. SECONDARY_SOUND_DEV      = "alsa_output.pci-0000_00_1b.0.analog-stereo"
  8. WEB_BROWSER              = "firefox"
  9. FILE_MANAGER             = "dolphin"
  10. TERM                     = "termite"
  11. EDITOR                   = "termite --exec=vim"
  12. MUSIC                    = "mpd"
  13. # Colors in hex
  14. COLOR_UNDERLINE          = "%{F#C98ACF}"
  15. COLOR_ICON               = "%{F#C98ACF}"
  16. COLOR_ICON_ACTIVE        = "%{F#62AFEE}"
  17. COLOR_TEXT               = "%{F#dddddd}"
  18. COLOR_RED                = "%{F#FF0000}"
  19. COLOR_HIGHTLIGHT         = "%{F#FFFFFF}"
  20. COLOR_NORMAL             = "%{F#DDDDDD}"
  21. END_COLOR                = "%{F-}"
  22. # Align bar objects
  23. LEFT                     = "%{l}"
  24. CENTER                   = "%{c}"
  25. RIGHT                    = "%{r}"
  26. GAP                      = "%{O10}"
  27. SEP                      = " "
  28. UNDERLINE_INIT           = "%{+u}%{U#C98ACF}"
  29. UNDERLINE_END            = "%{U-}%{-u}"
  30. OVERLINE_INIT            = "%{+o}%{O#C98ACF}"
  31. OVERLINE_END             = "%{O-}%{-o}"
  32. # icons - AwesomeFont
  33. WORKSPACES               = ""
  34. TIME                     = ""
  35. DATE                     = ""
  36. VOLUME                   = ""
  37. HEADPHONES               = ""
  38. PLAY                     = ""
  39. PAUSE                    = ""
  40. NEXT                     = ""
  41. BACK                     = ""
  42. STOP                     = ""
  43. WALLPAPER                = ""
  44. BATTERY_CRITICAL         = ""   #   5%
  45. BATTERY_LOW              = ""   #  25%
  46. BATTERY_HALF             = ""   #  50%
  47. BATTERY_ALMOST           = ""   #  75%
  48. BATTERY_FULL             = ""   # 100%
  49. WIRED                    = ""
  50. A_WEB_BROWSER            = ""
  51. A_FILE_MANAGER           = ""
  52. A_EDITOR                 = ""
  53. A_TERM                   = ""
  54. #</Settings>
  55. #from subprocess import Popen,PIPE
  56. #from re import split
  57. #from i3ipc import Connection
  58. #from time import strftime,sleep
  59. import subprocess,re,i3ipc,time
  60.  
  61.  
  62. # Running bash command
  63. def run(arg):
  64.    # from subprocess import Popen,PIPE
  65.     return subprocess.Popen(arg,stdout = subprocess.PIPE,stderr = subprocess.PIPE,shell = True).communicate()[0].decode("utf-8").strip()
  66.  
  67. # Create clickable area:
  68. # (sh command, 1 - LMB; 2 - CMB > 4 ScrollUp | 5 ScrollDown; 3 - RMB
  69. # For example: action("Termite","term","%{F#FF0000}",3)
  70. def action(arg,btn,color,a=1):
  71.     return color + "%{A" + str(a) + ":" + arg + ":}" + btn + "%{A}" + END_COLOR
  72.  
  73. # taken from stackoverflow user: Mark Byers
  74. def natural_sort(l):
  75.     #from re import split
  76.     convert = lambda text: int(text) if text.isdigit() else text.lower()
  77.     alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
  78.     return sorted(l, key = alphanum_key)
  79.  
  80. # i3 workspaces
  81. def get_workspaces():
  82.     #from i3ipc import Connection
  83.     focused_workspace = ""; workspaces = i3ipc.Connection().get_workspaces(); workspace_names = []
  84.     for workspace in workspaces:
  85.       workspace_names.append(workspace.name)
  86.       if workspace.focused: focused_workspace = workspace.name
  87.  
  88.     # build the string to return
  89.     ws = "%{A4:i3-msg workspace prev:}%{A5:i3-msg workspace next:}" + COLOR_ICON + WORKSPACES + SEP + END_COLOR
  90.     for workspace_name in ( natural_sort(workspace_names)):
  91.       if workspace_name == focused_workspace:
  92.         ws += UNDERLINE_INIT + COLOR_HIGHTLIGHT + SEP + workspace_name + SEP + END_COLOR + UNDERLINE_END + SEP
  93.       else:
  94.         ws += "%{A:i3-msg workspace " + workspace_name + ":}" + SEP + COLOR_NORMAL + workspace_name + SEP + END_COLOR + "%{A} "
  95.     ws += "%{A}%{A}"
  96.  
  97.     return ws
  98.  
  99. # Output battery percents
  100. def get_battery():
  101.     icon    = ""; time    = ""
  102.     raw     = run("acpi")[11:-1]
  103.     time = raw[raw.find("%,")+4:raw.find("%,")+8]
  104.     if "until" in raw:
  105.       icon = COLOR_ICON + WIRED + " "
  106.       if time == "unti": time = ""
  107.     elif "Unknown" in raw:
  108.       icon = COLOR_ICON + WIRED
  109.       time = ""
  110.     elif "Full" in raw:
  111.       icon = COLOR_ICON + WIRED
  112.       time = ""
  113.     elif "remainin" in raw:
  114.       percent = int(raw[raw.find(" ")+1:raw.find("%")])
  115.       if   percent <= 5:   icon = COLOR_ICON + BATTERY_CRITICAL
  116.       elif percent <= 25:  icon = COLOR_ICON + BATTERY_LOW
  117.       elif percent <= 50:  icon = COLOR_ICON + BATTERY_HALF
  118.       elif percent <= 75:  icon = COLOR_ICON + BATTERY_ALMOST
  119.       elif percent <= 100: icon = COLOR_ICON + BATTERY_FULL
  120.       icon += " "
  121.       if icon == BATTERY_LOW + " ": return COLOR_RED + icon + time
  122.     return icon + str(time)
  123.  
  124.  
  125. def get_time():
  126.     #from time import strftime
  127.     hour = time.strftime("%I"); minute = time.strftime("%M")
  128.     day = time.strftime("%d"); month = time.strftime("%m")
  129.     year = time.strftime("%y")
  130.     separator = COLOR_ICON + "/"
  131.     __time =  COLOR_ICON + TIME + " " + COLOR_TEXT + hour + END_COLOR + COLOR_ICON + ":" + END_COLOR + COLOR_TEXT + minute
  132.     __date = COLOR_ICON + DATE + " " + COLOR_TEXT + day + separator + COLOR_TEXT + month + separator + COLOR_TEXT + year + END_COLOR
  133.     return __time + GAP + __date
  134.  
  135.  
  136. def get_volume(pdev,sdev):
  137.     raw = run("pactl list sinks"); icon = ""; action = "" ; nn = 0
  138.     if pdev in raw:
  139.       dev = pdev; icon = HEADPHONES; nn = 150
  140.       actions = "%{A1:kmix &:}%{A4:amixer -q -D sysdefault sset Headphone 10%+ unmute:}%{A5:amixer -q -D sysdefault sset Headphone 10%- unmute:}"
  141.     elif sdev in raw:
  142.       dev = sdev; icon = VOLUME; nn = 100
  143.       actions = "%{A1:kmix &:}%{A4:amixer -q sset Master 10%+ unmute:}%{A5:amixer -q sset Master 10%- unmute:}"
  144.     else:
  145.       return COLOR_ICON + VOLUME + COLOR_TEXT + "nosnd"
  146.  
  147.     find = raw.splitlines()[8]
  148.     sraw = raw.splitlines()
  149.     y = 0; x = 0; n = 0
  150.     for x in range (0,nn):
  151.       if dev in sraw[x]:
  152.         y = x
  153.         for y in range (x,nn):
  154.           if find in sraw[y]:
  155.             n = y
  156.             break
  157.         break
  158.     num = n+1
  159.     s = sraw[num:num+1][0]
  160.     value = s[s.find("/ ")+2:s.find("%")]
  161.     if "IDL" in value: #<<< FIX THIS SHIT
  162.       value = "muted"
  163.       return COLOR_ICON + actions + icon + "%{A}%{A}%{A} " + COLOR_TEXT + value
  164.     else:
  165.       return COLOR_ICON + actions +  icon + "%{A}%{A}%{A} " + COLOR_TEXT + value +"%"
  166.  
  167. def get_mpd_buttons():
  168.     back_btn = action('mpc prev',BACK,COLOR_ICON)
  169.     next_btn = action('mpc next',NEXT,COLOR_ICON)
  170.     raw = run("mpc status | tail -n 2 | head -n 1 | awk '{print $1;}' | tr -cd 'a-zA-Z'")
  171.     if raw == "playing":
  172.       return back_btn + GAP + action('mpc pause',PAUSE,COLOR_ICON) + GAP + action('mpc stop',STOP,COLOR_ICON) + GAP + next_btn
  173.     elif raw == "paused":
  174.       return back_btn + GAP + action('mpc play',PLAY,COLOR_ICON) + GAP + action('mpc stop',STOP,COLOR_ICON) + GAP + next_btn
  175.     else: return back_btn + GAP + action('mpc play',PLAY,COLOR_ICON) + GAP + next_btn
  176.  
  177. def get_mpd_song():
  178.     raw = run("mpc status | tail -n 2 | head -n 1 | awk '{print $1;}' | tr -cd 'a-zA-Z'")
  179.     song = run("mpc current")
  180.     time = run("mpc status | head -n 2 | tail -n 1 | awk '{{print $3}}'")
  181.     repeat_raw = run("mpc")
  182.     repeat_param = repeat_raw[repeat_raw.find("single:")+8:repeat_raw.find("single:")+11]
  183.     repeat_action = ""
  184.     if repeat_param == "on ":
  185.       repeat_action = "mpc single off"
  186.       time = time + " (repeat)"
  187.     elif repeat_param == "off":
  188.       repeat_action = "mpc single on"
  189.     if raw == "playing" or raw == "paused": return action("termite --exec=ncmpcpp",song,COLOR_TEXT) + GAP + action(repeat_action, time, COLOR_TEXT)
  190.     else: return ""
  191.  
  192. def set_randomwp():
  193.     return action("feh --randomize --bg-fill ~/.bg/*",WALLPAPER,COLOR_ICON)
  194.  
  195. def get_btn(app,icon):
  196.     bapp = "ps axf | grep \"" + app + "\" | grep -v grep"; raw = run(bapp)
  197.     if raw == "": return action(app + " &",icon,COLOR_ICON)
  198.     elif raw != "": return action(app + " &",icon,COLOR_ICON_ACTIVE)
  199.  
  200. def telegram_bicycle():
  201.     bapp = "pidof Telegram"; raw = run(bapp); app = "/home/quanttyo/Telegram/Telegram"; icon = "Tg"
  202.     if raw == "": return action(app + " &",icon,COLOR_ICON)
  203.     elif raw != "": return action("killall Telegram &",icon,COLOR_ICON_ACTIVE)
  204.  
  205. def get_layout():
  206.     value = run("xkblayout-state print %s")
  207.     return COLOR_TEXT + value
  208.  
  209. def get_status():
  210.     return LEFT + GAP + get_workspaces() + GAP + get_btn(TERM,A_TERM) + GAP + get_btn(WEB_BROWSER,A_WEB_BROWSER) + GAP + get_btn(FILE_MANAGER,A_FILE_MANAGER) + GAP + get_btn(EDITOR,A_EDITOR) + GAP + telegram_bicycle() + CENTER + get_mpd_song() + GAP + get_mpd_buttons() + RIGHT + get_layout() + GAP + set_randomwp() + GAP + get_volume(PRIMARY_SOUND_DEV,SECONDARY_SOUND_DEV) + GAP + get_time() + GAP + get_battery() + GAP
  211.  
  212. def main():
  213.   # print the status and sleep until interrupted
  214.     while True:
  215.         print(get_status())
  216.         time.sleep(.4)
  217.     begin
  218.  
  219. main()
Advertisement
Add Comment
Please, Sign In to add comment