This repository has been archived by the owner on Mar 11, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 40cfb9c
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__pycache__ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
===== | ||
Kinky | ||
===== | ||
|
||
Simple toolkit for custom conky-cli-like statusbar scripts, written in Python 3. | ||
|
||
Usage | ||
===== | ||
|
||
Create a file called `my_kinky.py`: | ||
|
||
.. code-block:: python | ||
import os | ||
import datetime | ||
import time | ||
from kinky import Item, StatusBar | ||
class Clock(Item): | ||
def run(self): | ||
while self.running: | ||
self.text = datetime.datetime.now().strftime('%H:%M') | ||
time.sleep(60) # update interval: a minute | ||
class Volume(Item): | ||
def run(self): | ||
while self.running: | ||
# somehow get system volume and save it as a string in self.text | ||
# self.text = "34%" | ||
time.sleep(.3) # update interval: 0.3 seconds | ||
statusbar = StatusBar() | ||
statusbar.items = [Volume(),Clock()] | ||
statusbar.between = '; ' | ||
statusbar.run() | ||
Every time an item writes to its text attribute, the statusbar will write a new | ||
line. Running this from the command line gives us:: | ||
|
||
$ python3 my_kinky.py | ||
|
||
34%; 20:15 | ||
34%; 20:16 | ||
34%; 20:17 | ||
|
||
... because the Volume doesn't change, but the Clock triggers an update every minute. | ||
|
||
This is intended for usage with dzen2:: | ||
|
||
python -u mystatusbar.py | dzen2 # -u disables stdout buffering | ||
|
||
:: | ||
|
||
License | ||
======= | ||
|
||
Kinky is released under the public domain. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import threading | ||
import logging | ||
from subprocess import Popen, PIPE | ||
|
||
class StatusBar(object): | ||
before = ' ' | ||
between = ' | ' | ||
after = ' ' | ||
items = None | ||
|
||
def __init__(self): | ||
self.items = [] | ||
|
||
def run(self): | ||
for item in self.items: | ||
logging.debug('Spawning: ' + item.__class__.__name__) | ||
item.spawn(self) | ||
|
||
def draw(self): | ||
text = [item.text.replace('\n', '') for item in self.items if item.text is not None] | ||
text = self.between.join(text).strip() | ||
if text: | ||
print(self.before + text + self.after) | ||
|
||
class Item(threading.Thread): | ||
thread = None | ||
bar = None | ||
running = None | ||
_text = None | ||
|
||
@property | ||
def text(self): | ||
return self._text | ||
|
||
@text.setter | ||
def text(self, value): | ||
self._text = value | ||
self.bar.draw() | ||
|
||
def spawn(self, bar): | ||
self.bar = bar | ||
self.running = True | ||
self.start() | ||
|
||
def kill(self): | ||
self.running = False | ||
|
||
def run(self): | ||
raise NotImplementedError | ||
|
||
|
||
# UTILS | ||
def shell(command): | ||
'''Executes a command on the shell and returns its output''' | ||
return Popen(command, shell=True, stdout=PIPE).stdout.read().decode() | ||
|
||
def is_running(process): | ||
'''Determines if a process with the given process name is running''' | ||
return shell('ps -A | grep -c ' + process).strip() != '0' |