This commit is contained in:
Johann Dreo 2017-05-17 11:13:25 +02:00
commit ac7a2c3859
2 changed files with 14 additions and 72 deletions

View file

@ -286,3 +286,10 @@ alternative one.
See the ninja theme for how to extend an existing theme with more regexps and a different configuration.
See the gcc theme for an example of how to use the localization of existing softwares to build translated regexp.
### Buffering
Note that when you use colout within real time streams (like `tail -f X | qrep Y | colout Y`) of commands,
you may observe that the lines are printed by large chunks and not one by one, in real time.
This is not due to colout but to the buffering behavior of your shell.
To fix that, use `stdbuf`, for example: `tail -f X | stdbuf -o0 grep Y | colout Y`.

View file

@ -17,6 +17,7 @@ import signal
import string
import hashlib
import functools
import argparse
# set the SIGPIPE handler to kill the program instead of
# ending in a write error when a broken pipe occurs
@ -215,6 +216,7 @@ def load_themes( themes_dir):
global context
logging.debug("search for themes in: %s" % themes_dir)
os.chdir( themes_dir )
sys.path.append( themes_dir )
# load available themes
for f in glob.iglob("colout_*.py"):
@ -754,64 +756,7 @@ def colorgen(stream, pattern, color="red", style="normal", on_groups=False):
# Command line tools #
######################
def __args_dirty__(argv, usage=""):
"""
Roughly extract options from the command line arguments.
To be used only when argparse is not available.
Returns a tuple of (pattern,color,style,on_stderr).
>>> colout.__args_dirty__(["colout","pattern"],"usage")
('pattern', 'red', 'normal', False)
>>> colout.__args_dirty__(["colout","pattern","colors","styles"],"usage")
('pattern', 'colors', 'styles', False)
>>> colout.__args_dirty__(["colout","pattern","colors","styles","True"],"usage")
('pattern', 'colors', 'styles', True)
"""
# Use a dirty argument picker
# Check for bad usage or an help flag
if len(argv) < 2 \
or len(argv) > 10 \
or argv[1] == "--help" \
or argv[1] == "-h":
print(usage+"\n")
print("Usage:", argv[0], "<pattern> <color(s)> [<style(s)>] [<print on stderr?>] [<iterate over groups?>]")
print("\tAvailable colors:", " ".join(context["colors"]))
print("\tAvailable styles:", " ".join(context["styles"]))
print("Example:", argv[0], "'^(def)\s+(\w*).*$' blue,magenta italic,bold < colout.py")
sys.exit(1)
assert(len(argv) >= 2)
# Get mandatory arguments
pattern = argv[1]
# default values for optional args
color = "red"
style = "normal"
on_stderr = False
if len(argv) >= 3:
color = argv[2]
if len(argv) >= 4:
style = argv[3]
if len(argv) == 5:
on_groups = bool(argv[4])
if len(argv) == 6:
as_colormap = bool(argv[5])
if len(argv) == 7:
as_theme = bool(argv[6])
if len(argv) == 8:
as_source = bool(argv[7])
if len(argv) == 9:
as_all = bool(argv[8])
if len(argv) == 10:
scale = bool(argv[9])
return pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, scale
def __args_parse__(argv, usage=""):
def _args_parse(argv, usage=""):
"""
Parse command line arguments with the argparse library.
Returns a tuple of (pattern,color,style,on_stderr).
@ -922,19 +867,9 @@ if __name__ == "__main__":
#####################
# Arguments parsing #
#####################
try:
import argparse
# if argparse is not installed
except ImportError:
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale \
= __args_dirty__(sys.argv, usage)
# if argparse is available
else:
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale, \
debug, resources, palettes_dirs, themes_dirs, default_colormap \
= __args_parse__(sys.argv, usage)
pattern, color, style, on_groups, as_colormap, as_theme, as_source, as_all, myscale, \
debug, resources, palettes_dirs, themes_dirs, default_colormap \
= _args_parse(sys.argv, usage)
if debug:
lvl = logging.DEBUG
@ -1088,6 +1023,6 @@ if __name__ == "__main__":
for var in context:
print(var,context[var])
print(traceback.format_exc())
logging.error("unknown color: %s" % e )
logging.error("unknown color: %s (maybe you forgot to install python3-pygments?)" % e )
sys.exit( error_codes["UnknownColor"] )