add src colorifier
This commit is contained in:
1
bin/src2ans
Symbolic link
1
bin/src2ans
Symbolic link
@@ -0,0 +1 @@
|
||||
../reporting/src2ans
|
||||
109
reporting/src2ans
Executable file
109
reporting/src2ans
Executable file
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
#
|
||||
# Copyright 2016 Ville Rantanen
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Lesser General Public License as published
|
||||
# by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
'''Color by ${X} notation.'''
|
||||
|
||||
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
||||
__version__ = "0.1"
|
||||
|
||||
import sys,os,argparse,re
|
||||
from argparse import ArgumentParser
|
||||
class bc:
|
||||
K="\033[1;30m"
|
||||
R="\033[1;31m"
|
||||
G="\033[1;32m"
|
||||
B="\033[1;34m"
|
||||
Y="\033[1;33m"
|
||||
M="\033[1;35m"
|
||||
C="\033[1;36m"
|
||||
W="\033[1;37m"
|
||||
k="\033[0;30m"
|
||||
r="\033[0;31m"
|
||||
g="\033[0;32m"
|
||||
b="\033[0;34m"
|
||||
y="\033[0;33m"
|
||||
m="\033[0;35m"
|
||||
c="\033[0;36m"
|
||||
w="\033[0;37m"
|
||||
S = '\033[1m'
|
||||
U = '\033[4m'
|
||||
Z = '\033[0m'
|
||||
CLR = '\033[2J'
|
||||
CLREND = '\033[K'
|
||||
|
||||
color_keys="KRGBYMCWkrgbymcwSUZ"
|
||||
color_list=[K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,U,Z]
|
||||
|
||||
def pos(self,y,x):
|
||||
return "\033["+str(y)+";"+str(x)+"H"
|
||||
|
||||
def posprint(self, y,x,s):
|
||||
sys.stdout.write( self.pos(y,x) + str(s) )
|
||||
|
||||
def clear(self):
|
||||
sys.stdout.write( self.CLR+self.pos(0,0) )
|
||||
def clear_to_end(self):
|
||||
sys.stdout.write( self.CLREND )
|
||||
|
||||
def color_string(self,s):
|
||||
for i,c in enumerate(self.color_keys):
|
||||
s=s.replace("${"+c+"}",self.color_list[i])
|
||||
return s
|
||||
def nocolor_string(self,s):
|
||||
for i,c in enumerate(self.color_keys):
|
||||
s=s.replace("${"+c+"}","")
|
||||
return s
|
||||
|
||||
|
||||
def setup_options():
|
||||
''' Create command line options '''
|
||||
usage='''
|
||||
Color notation renderer in ANSI codes
|
||||
Special syntaxes:
|
||||
* Colors: insert string ${C}, where C is one of KRGBYMCWkrgbymcwSUZ
|
||||
|
||||
'''
|
||||
|
||||
parser=ArgumentParser(description=usage,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog=__author__)
|
||||
|
||||
parser.add_argument("-v","--version",action="version",version=__version__)
|
||||
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
|
||||
help="Disable color.")
|
||||
parser.add_argument("filename",type=str,
|
||||
help="File to show")
|
||||
opts=parser.parse_args()
|
||||
return opts
|
||||
|
||||
bc=bc()
|
||||
opts=setup_options()
|
||||
f=open(opts.filename,'r')
|
||||
for row in f:
|
||||
if not row:
|
||||
continue
|
||||
row=row.decode('utf-8').rstrip("\n\r ")
|
||||
if opts.color:
|
||||
colored=bc.color_string(row)
|
||||
else:
|
||||
colored=bc.nocolor_string(row)
|
||||
sys.stdout.write(colored.encode('utf-8'))
|
||||
sys.stdout.write("\n")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user