From ab38f5686d6c0c2fa658743e4e0d18ae0f7af76e Mon Sep 17 00:00:00 2001 From: Vasiliy Horbachenko Date: Thu, 22 May 2014 17:03:09 +0300 Subject: added battery level, current keyboard layout --- modules/battery-level.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 modules/battery-level.py (limited to 'modules/battery-level.py') diff --git a/modules/battery-level.py b/modules/battery-level.py new file mode 100644 index 0000000..ed6d484 --- /dev/null +++ b/modules/battery-level.py @@ -0,0 +1,59 @@ +import subprocess +import time +import re +import math + +""" +Module for displaying information about battery. + +@author shadowprince +@version 1.0 +@license Eclipse Public License +""" + +CACHE_TIME = 30 # time to update battery +HIDE_WHEN_FULL = True # hide any information when battery is fully charged + +MODE = "bar" # for primitive-one-char bar, or "text" for text percentage ouput + +BLOCKS = [ "_", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", ] # block for bar +TEXT_FORMAT = "Battery: {}" # text with "text" mode. percentage with % replaces {} + +CHARGING_CHARACTER = "⚡" + +# None means - get it from i3 config +COLOR_DEGRADED = None +COLOR_BAD = None +COLOR_CHARGING = "#FCE94F" + +class Py3status: + def currentBattery(self, i3status_output_json, i3status_config): + response = {'name': 'current-battery-level'} + + acpi = subprocess.check_output(["acpi"]).decode('utf-8') + proc = int(re.search(r"(\d+)%", acpi).group(1)) + + charging = bool(re.match(r".*Charging.*", acpi)) + full = bool(re.match(r".*Unknown.*", acpi)) or bool(re.match(r".*Full.*", acpi)) + + if MODE == "bar": + character = BLOCKS[math.ceil(proc/100*(len(BLOCKS) - 1))] + else: + character = TEXT_FORMAT.format(str(proc) + "%") + + if proc < 30: + response['color'] = COLOR_DEGRADED if COLOR_DEGRADED else i3status_config["color_degraded"] + if proc < 10: + response['color'] = COLOR_BAD if COLOR_BAD else i3status_config["color_bad"] + + if full: + response['full_text'] = "" if HIDE_WHEN_FULL else BLOCKS[-1] + elif charging: + response['full_text'] = CHARGING_CHAR + response['color'] = COLOR_CHARGING + else: + response['full_text'] = character + + response['cached_until'] = time.time() + CACHE_TIME + + return (0, response) -- cgit v1.3 From cff1906f8d6eab1a01fba07326d3ab5175e52975 Mon Sep 17 00:00:00 2001 From: Vasiliy Horbachenko Date: Thu, 22 May 2014 17:18:24 +0300 Subject: small fix --- modules/battery-level.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'modules/battery-level.py') diff --git a/modules/battery-level.py b/modules/battery-level.py index ed6d484..37a25d6 100644 --- a/modules/battery-level.py +++ b/modules/battery-level.py @@ -5,10 +5,6 @@ import math """ Module for displaying information about battery. - -@author shadowprince -@version 1.0 -@license Eclipse Public License """ CACHE_TIME = 30 # time to update battery @@ -49,7 +45,7 @@ class Py3status: if full: response['full_text'] = "" if HIDE_WHEN_FULL else BLOCKS[-1] elif charging: - response['full_text'] = CHARGING_CHAR + response['full_text'] = CHARGING_CHARACTER response['color'] = COLOR_CHARGING else: response['full_text'] = character -- cgit v1.3 From 61db2ac48f633db4d1b7c3ee82fa8baf06f3e82b Mon Sep 17 00:00:00 2001 From: Vasiliy Horbachenko Date: Fri, 23 May 2014 22:44:43 +0300 Subject: coding header for battery level --- modules/battery-level.py | 1 + 1 file changed, 1 insertion(+) (limited to 'modules/battery-level.py') diff --git a/modules/battery-level.py b/modules/battery-level.py index 37a25d6..02b7e43 100644 --- a/modules/battery-level.py +++ b/modules/battery-level.py @@ -1,3 +1,4 @@ +# -*- coding: utf8 -*- import subprocess import time import re -- cgit v1.3 From fbac7ee6d51f82756e3cd17a31f7d212f5f6dd3c Mon Sep 17 00:00:00 2001 From: Vasiliy Horbachenko Date: Sat, 24 May 2014 00:07:52 +0300 Subject: mpd module --- modules/battery-level.py | 4 ++ modules/current-title.py | 5 +-- modules/mpd.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 modules/mpd.py (limited to 'modules/battery-level.py') diff --git a/modules/battery-level.py b/modules/battery-level.py index 02b7e43..fb3b769 100644 --- a/modules/battery-level.py +++ b/modules/battery-level.py @@ -6,6 +6,10 @@ import math """ Module for displaying information about battery. + +@author shadowprince +@version 1.0 +@license Eclipse Public License """ CACHE_TIME = 30 # time to update battery diff --git a/modules/current-title.py b/modules/current-title.py index e2f5aef..530908f 100644 --- a/modules/current-title.py +++ b/modules/current-title.py @@ -4,9 +4,8 @@ import time """ Py3status plugin - shows current window title. -DEPENDENCIES: - Depends on i3-py! - pip install i3-py +DEPENDENCIES: Depends on i3-py: + # pip install i3-py If payload from server contains wierd utf-8 (for example one window have something bad in title) - plugin will give empty output UNTIL this window will be closed. I can't fix or workaround that in PLUGIN, problem is in i3-py library. diff --git a/modules/mpd.py b/modules/mpd.py new file mode 100644 index 0000000..e94872c --- /dev/null +++ b/modules/mpd.py @@ -0,0 +1,112 @@ +from mpd import (MPDClient, CommandError) +from socket import error as SocketError +import time +import re + +""" +Mpd - module to show information from mpd to your's bar! Settings listed above. +DEPENDENCIES: python-mpd2 (NOT python2-mpd2): + # pip install python-mpd2 + +@author shadowprince +@version 1.0 +@license Eclipse Public License +""" + +# mpd settings +HOST = "localhost" +PORT = "6600" +PASSWORD = None + +# time to update +CACHED_TIME = 1 + +# position +POSITION = 1 + +# if text length will be greater - it'll shrink it +MAX_WIDTH = 120 + +# hide any indicator, if +HIDE_WHEN_PAUSED = False +HIDE_WHEN_STOPPED = True + +# state characters (or strings). Actual of them replaces {state} placeholder in STRFORMAT +STATE_CHARACTERS = { + "pause": "[pause]", + "play": "[play]", + "stop": "[stop]", +} + +""" format of result string +can contain: + {state} - current state from STATE_CHARACTERS + Track information: + {track}, {artist}, {title}, {time}, {album}, {pos} + In additional, information about next track also comes in, in analogue with current, but with next_ prefix, + like {next_title} +""" +STRFORMAT = "{state} №{pos}. {artist} - {title} [{time}] | {next_title}" + +class Py3status: + def __init__(self, *args, **kwargs): + self.text = "" + super(Py3status).__init__(*args, **kwargs) + + def currentTrack(self, i3status_output_json, i3status_config): + try: + c = MPDClient() + c.connect(host=HOST, port=PORT) + if PASSWORD: + c.password(PASSWORD) + + status = c.status() + song = int(status.get("song", 0)) + next_song = int(status.get("nextsong", 0)) + + if (status["state"] == "pause" and HIDE_WHEN_PAUSED) or (status["state"] == "stop" and HIDE_WHEN_STOPPED): + text = "" + else: + try: + song = c.playlistinfo()[song] + song["time"] = "{0:.2f}".format(int(song.get("time", 1)) / 60) + except IndexError: + song = {} + + try: + next_song = c.playlistinfo()[next_song] + except IndexError: + next_song = {} + + format_args = song + format_args["state"] = STATE_CHARACTERS.get(status.get("state", None)) + for k, v in next_song.items(): + format_args["next_{}".format(k)] = v + + text = STRFORMAT + for k, v in format_args.items(): + text = text.replace("{" + k + "}", v) + + for sub in re.findall(r"{\S+?}", text): + text = text.replace(sub, "") + except SocketError: + text = "Failed to connect to mpd!" + except CommandError: + text = "Failed to authenticate to mpd!" + c.disconnect() + + if len(text) > MAX_WIDTH: + text = text[-MAX_WIDTH-3:] + "..." + + if self.text != text: + transformed = True + self.text = text + else: + transformed = False + + return (POSITION, { + 'transformed': transformed, + 'full_text': self.text, + 'name': 'scratchpad-count', + 'cached_until': time.time() + CACHED_TIME, + }) -- cgit v1.3 From b4eb295800916915b6188114b4df8fadd0165676 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Mon, 26 May 2014 19:11:19 +0200 Subject: PEP8 and python2 fixes --- modules/battery-level.py | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'modules/battery-level.py') diff --git a/modules/battery-level.py b/modules/battery-level.py index fb3b769..4d94311 100644 --- a/modules/battery-level.py +++ b/modules/battery-level.py @@ -1,35 +1,42 @@ # -*- coding: utf8 -*- -import subprocess -import time -import re + +from __future__ import division # python2 compatibility +from time import time + import math +import re +import subprocess """ Module for displaying information about battery. +Requires: + - the 'acpi' command line + @author shadowprince -@version 1.0 @license Eclipse Public License """ -CACHE_TIME = 30 # time to update battery -HIDE_WHEN_FULL = True # hide any information when battery is fully charged +CACHE_TIME = 30 # time to update battery +HIDE_WHEN_FULL = False # hide any information when battery is fully charged -MODE = "bar" # for primitive-one-char bar, or "text" for text percentage ouput +MODE = "bar" # for primitive-one-char bar, or "text" for text percentage ouput -BLOCKS = [ "_", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", ] # block for bar -TEXT_FORMAT = "Battery: {}" # text with "text" mode. percentage with % replaces {} +BLOCKS = ["_", "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"] # block for bar +TEXT_FORMAT = "Battery: {}" # text with "text" mode. percentage with % replaces {} -CHARGING_CHARACTER = "⚡" +CHARGING_CHARACTER = "⚡" # None means - get it from i3 config -COLOR_DEGRADED = None COLOR_BAD = None COLOR_CHARGING = "#FCE94F" +COLOR_DEGRADED = None +COLOR_GOOD = None + class Py3status: - def currentBattery(self, i3status_output_json, i3status_config): - response = {'name': 'current-battery-level'} + def battery_level(self, i3status_output_json, i3status_config): + response = {'name': 'battery-level'} acpi = subprocess.check_output(["acpi"]).decode('utf-8') proc = int(re.search(r"(\d+)%", acpi).group(1)) @@ -38,23 +45,24 @@ class Py3status: full = bool(re.match(r".*Unknown.*", acpi)) or bool(re.match(r".*Full.*", acpi)) if MODE == "bar": - character = BLOCKS[math.ceil(proc/100*(len(BLOCKS) - 1))] + character = BLOCKS[int(math.ceil(proc/100*(len(BLOCKS) - 1)))] else: character = TEXT_FORMAT.format(str(proc) + "%") - + if proc < 30: - response['color'] = COLOR_DEGRADED if COLOR_DEGRADED else i3status_config["color_degraded"] + response['color'] = COLOR_DEGRADED if COLOR_DEGRADED else i3status_config['color_degraded'] if proc < 10: - response['color'] = COLOR_BAD if COLOR_BAD else i3status_config["color_bad"] + response['color'] = COLOR_BAD if COLOR_BAD else i3status_config['color_bad'] if full: + response['color'] = COLOR_GOOD if COLOR_GOOD else i3status_config['color_good'] response['full_text'] = "" if HIDE_WHEN_FULL else BLOCKS[-1] elif charging: - response['full_text'] = CHARGING_CHARACTER response['color'] = COLOR_CHARGING + response['full_text'] = CHARGING_CHARACTER else: response['full_text'] = character - response['cached_until'] = time.time() + CACHE_TIME + response['cached_until'] = time() + CACHE_TIME return (0, response) -- cgit v1.3 From 277b0008c2d779a98605737bc91ca75e6b364728 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Tue, 27 May 2014 12:31:44 +0200 Subject: normalize the CACHE_TIMEOUT variable name --- modules/battery-level.py | 4 ++-- modules/keyboard-layout.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'modules/battery-level.py') diff --git a/modules/battery-level.py b/modules/battery-level.py index 4d94311..6c30556 100644 --- a/modules/battery-level.py +++ b/modules/battery-level.py @@ -17,7 +17,7 @@ Requires: @license Eclipse Public License """ -CACHE_TIME = 30 # time to update battery +CACHE_TIMEOUT = 30 # time to update battery HIDE_WHEN_FULL = False # hide any information when battery is fully charged MODE = "bar" # for primitive-one-char bar, or "text" for text percentage ouput @@ -63,6 +63,6 @@ class Py3status: else: response['full_text'] = character - response['cached_until'] = time() + CACHE_TIME + response['cached_until'] = time() + CACHE_TIMEOUT return (0, response) diff --git a/modules/keyboard-layout.py b/modules/keyboard-layout.py index 6fd4019..97ef716 100644 --- a/modules/keyboard-layout.py +++ b/modules/keyboard-layout.py @@ -13,7 +13,7 @@ Requires: @license Eclipse Public License """ -CACHE_TIME = 10 # refresh time to update indicator +CACHE_TIMEOUT = 10 # refresh time to update indicator # colors of layouts, check your command's output to match keys LANG_COLORS = { @@ -57,7 +57,7 @@ class Py3status: response = { 'full_text': '', 'name': 'keyboard-layout', - 'cached_until': time() + CACHE_TIME + 'cached_until': time() + CACHE_TIMEOUT } lang = self.command().strip() -- cgit v1.3