From 1c13e8d3180e44ed82efa34d5d27fb8ad5c6affd Mon Sep 17 00:00:00 2001 From: Josef Gajdusek Date: Thu, 3 Jul 2014 18:07:30 +0200 Subject: [PATCH] Add shell module Add new "shell" module which can execute arbitrary shell command and display its output. --- i3pystatus/shell.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 i3pystatus/shell.py diff --git a/i3pystatus/shell.py b/i3pystatus/shell.py new file mode 100644 index 0000000..e97a3cb --- /dev/null +++ b/i3pystatus/shell.py @@ -0,0 +1,36 @@ +from i3pystatus import IntervalModule +from subprocess import check_output, CalledProcessError + +class Shell(IntervalModule): + """ + Shows output of shell command + """ + + color = "#FFFFFF" + error_color = "#FF0000" + + settings = ( + ("command", "command to be executed"), + ("color", "standard color"), + ("error_color", "color to use when non zero exit code is returned") + ) + + required = ("command",) + + def run(self): + try: + out = check_output(self.command, shell=True) + color = self.color + except CalledProcessError as e: + out = e.output + color = self.error_color + + out = out.decode("UTF-8").replace("\n", " ") + + if out[-1] == " ": + out = out[:-1] + + self.output = { + "full_text": out, + "color": color + }