Skip to content

Commit

Permalink
Upload the readme (and screenshots) and the setup.py .
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtrooper committed Nov 20, 2019
1 parent 868a573 commit 533bb3a
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 5 deletions.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# POWERLINE SWISSARMYKNIFE

A [Powerline](https://powerline.readthedocs.io/en/master/) segment. This segment shows the execution of complex command line defined by user.

By [Miguel de Dios Matias](https://github.com/mdtrooper).

## Installation

### Using pip

```
pip install powerline-swissarmyknife
```

## Configuration

You can activate the Powerline Slotmachine segment by adding it to your segment configuration,
for example in `.config/powerline/themes/shell/default.json`:

```json
{
"function": "powerline_swissarmyknife.execute",
"priority": 30,
"args": {
"commandLine": "ps aux --no-headers | wc -l",
"postContent": "⚙️"
}
},
```

Show the number of processes running in your system.

![screenshot number processes](https://raw.githubusercontent.com/mdtrooper/powerline_swissarmyknife/master/powerline_swissarmyknife_number_processes.jpg "screenshot number processes")

### Arguments
* **commandLine (string)**: The command line to execute, it can be complex (with pipes) (remember python3 runs as /bin/sh, not bash).
* **line (string)**: The string to format the content of segment.
* Default: "{preContent}{output}{err}{postContent}"
* **preContent (string)**: The string to show before the result.
* Default: ""
* **postContent (string)**: The string to show after the result.
* Default: "🤖"
* **successCodes (list(int) or None)**: The values are success code return (normally 0), the background change to critical success.
* Default: None
* **failureCodes (list(int) or None)**: The values are fail code return, the background change to critical failture.
* Default: None

### Examples

Shows the upload and download rate and count of torrents download in Deluge.

```json
{
"function": "powerline_swissarmyknife.execute",
"priority": 30,
"args": {
"commandLine": "deluge-console status | awk '/Total upload:/{print $3$4} /Total download:/{print $3$4} /Downloading:/{print $2\"D\"}' | tr '\n' ' '",
"postContent": ""
}
},
```


![screenshot deluge](https://raw.githubusercontent.com/mdtrooper/powerline_dice/master/powerline_deluge.jpg "screenshot deluge")

Show a random pornstar from redtube api.

```json
{
"function": "powerline_swissarmyknife.execute",
"priority": 30,
"args": {
"commandLine": "if [ -f /tmp/list.redtube.json ]; then list=$(cat /tmp/list.redtube.json); else list=$(curl 'https://api.redtube.com/?data=redtube.Stars.getStarList&output=json'); echo $list > /tmp/list.redtube.json; fi; list_length=$(echo $list | jq '.stars | length'); rand=$(echo 'ibase=16;' $(openssl rand -hex 4 | tr '[a-z]' '[A-Z]') | bc); echo $list | jq \".stars[$(echo \\\"$rand % $list_length\\\" | bc)].star.star_name\" -r",
"postContent": " 🎥"
}
},
```

![screenshot pornstar](https://raw.githubusercontent.com/mdtrooper/powerline_dice/master/powerline_pornstar.jpg "screenshot pornstar")

## License

Licensed under [the GPL3 License](https://github.com/mdtrooper/powerline_slotmachine/blob/master/LICENSE).
Binary file added powerline_deluge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added powerline_pornstar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions powerline_swissarmyknife/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python3
#
# slotmachine_oneline.py
# Copyright (C) 2019 Miguel de Dios Matias
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .execute import execute
27 changes: 22 additions & 5 deletions powerline_swissarmyknife/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,36 @@

import subprocess

def execute(pl, commandLine='', line='{preContent}{output}{err}{postContent}', successCodes=None, failureCode=None, preContent='', postContent='🧰', *args, **kwargs):
def execute(pl, commandLine='', line='{preContent}{output}{err}{postContent}', preContent='', postContent='🤖', successCodes=None, failureCodes=None, *args, **kwargs):
"""
Return a segment with the info (as output and or error) from command line execution.
Args:
pl (object): The powerline logger.
commandLine (string): The command line to execute, it can be complex (with pipes).
line (string): The string to format the content of segment.
Default value: {preContent}{output}{err}{postContent}
preContent (string): The string to show before the result.
postContent (string): The string to show after the result.
Default value: 🤖
successCodes list(int) or None: The values are success code return (normally 0), the background
change to critical success.
failureCodes list(int) or None: The values are fail code return, the background
change to critical failture.
Returns:
segment (list(dict)): The formated line with output of execution command line as powerline segment.
"""
result = subprocess.run(commandLine, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

returncode = result.returncode
output = result.stdout.decode('utf8')
err = result.stderr.decode('utf8')
output = result.stdout.decode('utf8').strip()
err = result.stderr.decode('utf8').strip()

color = ['information:regular']
if isinstance(successCodes, list):
if returncode in successCodes:
color = ['critical:success']
elif isinstance(failureCode, list):
if returncode in failureCode:
elif isinstance(failureCodes, list):
if returncode in failureCodes:
color = ['critical:failure']

return [{
Expand Down
Binary file added powerline_swissarmyknife_number_processes.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="powerline_swissarmyknife",
version="1.0.0",
license="GPLv3+",
author="Miguel de Dios Matias",
author_email="[email protected]",
description="A Powerline segment.This segment shows the execution of complex command line defined by user.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/mdtrooper/powerline_swissarmyknife",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Operating System :: OS Independent",
"Environment :: Console"
],
python_requires='>=3.7',
platforms=['any']
)

0 comments on commit 533bb3a

Please sign in to comment.