Last active
November 29, 2022 19:28
-
-
Save dgulino/4750099 to your computer and use it in GitHub Desktop.
Console plotting in python. A single or multiple delimited numbers per newline.
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
#!/usr/bin/env python | |
from __future__ import division | |
from __future__ import print_function | |
#Copyright 2022 Drew Gulino | |
##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 2 of the License, or | |
## (at your option) 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, write to the Free Software | |
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
import sys,os,getopt,time | |
from getopt import GetoptError | |
class Tgraph: | |
def __init__(self,display_number,columns,symbol,threshold,maximum,timestamp,counter): | |
self.bold = os.popen('tput bold').read() | |
self.reset = os.popen('tput sgr0').read() | |
#self.dim = os.popen('tput setaf 0').read() | |
self.red = os.popen('tput setaf 1').read() | |
#self.green = os.popen('tput setaf 2').read() | |
#self.yellow = os.popen('tput setaf 3').read() | |
#self.blue = os.popen('tput setaf 4').read() | |
#self.magenta = os.popen('tput setaf 5').read() | |
self.maximum = maximum | |
self.display_number = display_number | |
self.columns = columns | |
self.symbol = symbol | |
self.default_symbol = symbol | |
self.threshold = threshold | |
self.timestamp = timestamp | |
self.counter = counter | |
self.oldnum = 0 | |
self.first_num = True | |
def __del__(self): | |
sys.stdout.write( self.reset ) | |
def graph(self,nums,max_num): | |
for num in nums: | |
if self.counter: | |
new_oldnum = num | |
num = num - self.oldnum | |
self.oldnum = new_oldnum | |
if self.first_num: | |
self.first_num = False | |
continue | |
sys.stdout.softspace = 0 | |
if num > float(self.maximum): | |
self.maximum = num | |
sys.stdout.write( self.bold ) | |
self.symbol = "+" | |
else: | |
sys.stdout.write( self.reset ) | |
self.symbol = self.default_symbol | |
if self.threshold > 0: | |
if num >= float(self.threshold): | |
sys.stdout.write( self.red ) | |
else: | |
sys.stdout.write( self.reset ) | |
if num > 0: | |
if self.timestamp: | |
sys.stdout.write(time.strftime("%H:%M:%S ")) | |
sys.stdout.flush() | |
scale = float((self.columns - 9) / self.maximum) | |
characters = int(num*scale) | |
else: | |
scale = float(self.columns / self.maximum) | |
characters = int(num*scale) | |
for iter in (range(0,characters)): | |
sys.stdout.write(self.symbol) | |
sys.stdout.flush() | |
if self.display_number: | |
sys.stdout.write(str(num)) | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
else: | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
else: | |
if self.timestamp: | |
sys.stdout.write(time.strftime("%H:%M:%S ")) | |
sys.stdout.flush() | |
if self.display_number: | |
sys.stdout.write(str(num)) | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
def usage(progname): | |
print("Usage: " + progname) | |
version() | |
print("[-h --help]") | |
print("[-v --version]") | |
print("[-n --no_number] Don't display number w/graph") | |
print("[-c --columns=] Display columns(default = 72)") | |
print("[-s --symbol=] Symbol to display(default = '*')") | |
print("[-t --threshold=] Will color lines over this value") | |
print("[-m --maximum=] Presets the scale for this maximum value(default = 0)") | |
print("[-p --timestamp] Print local hour:min:sec timestamp per line (default = False)") | |
print("[-r --counter] subtract previous entry to track always increasing values") | |
def version(): | |
print("version: 1.6") | |
def main(argv, stdout, environ): | |
#TODO: Auto detect number of columns in display | |
progname = argv[0] | |
symbol = "*" | |
columns = int(os.popen('tput cols').read()) - 8 | |
number = 0 | |
display_number = 1 | |
threshold = 0 | |
maximum = 0 | |
timestamp = False | |
delimiter = " " | |
counter = False | |
try: | |
arglist, args = getopt.getopt(argv[1:], "hvnc:s:t:m:d:pr", ["help", "version", "no_number","columns=", "symbol=", | |
"threshold=", "maximum=", "delimiter=", "timestamp", "counter="]) | |
except GetoptError: | |
print("Invalid Option!") | |
usage(progname) | |
return | |
# Parse command line arguments | |
for (field, val) in arglist: | |
if field in ("-h", "--help"): | |
usage(progname) | |
return | |
if field in ("-v", "--version"): | |
version() | |
return | |
if field in ("-n", "--number"): | |
display_number = 0 | |
if field in ("-c", "--columns"): | |
columns = int(val) | |
if field in ("-s", "--symbol"): | |
symbol = val | |
if field in ("-t", "--threshold"): | |
threshold = val | |
if field in ("-m", "--maximum"): | |
maximum = val | |
if field in ("-d", "--delimiter"): | |
delimiter = val | |
if field in ("-p", "--timestamp"): | |
timestamp = True | |
if field in ("-r", "--counter"): | |
counter = True | |
tgraph = Tgraph(display_number,columns,symbol,threshold,maximum,timestamp,counter) | |
while 1: | |
numbers = sys.stdin.readline().split(delimiter) | |
if len(numbers) < 1: break | |
try: | |
numbers = [float(number.strip()) for number in numbers] | |
except: | |
continue | |
tgraph.graph(numbers,float(tgraph.maximum)) | |
if len(numbers) > 1: | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
if __name__ == "__main__": | |
main(sys.argv, sys.stdout, os.environ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment