Skip to content

Instantly share code, notes, and snippets.

@ketankr9
Created December 24, 2019 14:28
Show Gist options
  • Save ketankr9/8023ea7321d5c53155d44cb0ba91fb10 to your computer and use it in GitHub Desktop.
Save ketankr9/8023ea7321d5c53155d44cb0ba91fb10 to your computer and use it in GitHub Desktop.
output the value of any scripy in ubuntu unity panel
#!/bin/bash
echo $(($(cat /proc/net/dev | awk '{print $2}' | tail -n1) + $(cat /proc/net/dev | awk '{print $2}' | tail -n2 | head -n1)))
#!/usr/bin/env python3
import gi
gi.require_version('AppIndicator3', '0.1')
gi.require_version('Gtk', '3.0')
import threading
import subprocess
from gi.repository import Gtk
from gi.repository import AppIndicator3 as appindicator
from gi.repository import GLib
class PanelIndicator:
def __init__(self, script_file, update_interval):
self.ind = appindicator.Indicator.new(
"Test",
"go-down-symbolic",
appindicator.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status (appindicator.IndicatorStatus.ACTIVE)
self.menu = Gtk.Menu()
#
item = Gtk.MenuItem()
item.set_label("Exit")
item.connect("activate", self.quit)
self.menu.append(item)
#
self.menu.show_all()
self.ind.set_menu(self.menu)
self.script_file = script_file
self.update_interval = update_interval
self.data = 0
self.main()
def set_value(self, price):
self.ind.set_label(price, "")
self.ind.set_status(appindicator.IndicatorStatus.ATTENTION)
def get_new_value(self):
"""runs periodically"""
pass
def update_value(self):
val = self.get_new_value()
self.set_value(val)
return True
def f1(self):
""" runs once"""
GLib.timeout_add_seconds(self.update_interval, self.update_value)
def main(self):
threading.Thread(target=self.f1).start()
Gtk.main()
def quit(self, widget):
Gtk.main_quit()
class MyIndicator(PanelIndicator):
def get_new_value(self):
def script_runner():
out, err = subprocess.Popen(["bash", self.script_file], stdout=subprocess.PIPE).communicate()
out = out.strip().decode('utf-8')
return out
old_val = self.data
self.data = int(script_runner())
val = (self.data*1.0 - old_val)/10**3
val /= self.update_interval
if val/10**3 >= 1.0:
val /= 10**3
val = round(val, 1)
val = str(val) + " MB"
else:
val = round(val, 1)
val = str(val) + " KB"
return val
if __name__ == '__main__':
indicator = MyIndicator("/home/user/bin/netusage.sh", 1.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment