From dc38ab4e6d6302cf278df6e6adfcefe0afd841fe Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 12 Oct 2012 01:56:08 +0200 Subject: colour options are almost fully functional, inplace balloons are problematic --- .gitignore | 1 + CHANGELOG | 2 ++ manuals/manpage.0 | 6 ++++ manuals/ponysay.texinfo | 41 ++++++++++++++++++++++++++ ponysay.py | 76 +++++++++++++++++++++++++++++++++++-------------- 5 files changed, 104 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 0f3a1fc..cbbd875 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ ## Private workspace directory /_/ +/*.pony ## Backup files diff --git a/CHANGELOG b/CHANGELOG index 892416b..ab03820 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -15,6 +15,8 @@ Version 2.8 Support for printing just the pony, using the flag -o. + Colouring option flags are added. + Version 2.7 diff --git a/manuals/manpage.0 b/manuals/manpage.0 index 5162a50..9627f62 100644 --- a/manuals/manpage.0 +++ b/manuals/manpage.0 @@ -92,6 +92,12 @@ the first list are the MLP:FiM and the second one are non-MLP:FiM. .TP .B \-o, \-\-pony\-only, \-\-ponyonly Just print the pony, nothing else like the speech balloon. +.TP +.B \+c, \-\-colour [\fIansi-colour\fP] +Colour the balloon, including link and message. For more colouring features, se the \fIinfo\fP +manual. The argument, \fIansi-colour\fP, should be a ANSI colour sequence without leading CSI +and without a tailing \fIm\fP, for example \fI1;31\fP will make it in red and bold (or bright +depending on the terminal.) .SH ENVIRONMENT .TP .B PONYSAY_BOTTOM diff --git a/manuals/ponysay.texinfo b/manuals/ponysay.texinfo index 5df4fd2..a43a781 100644 --- a/manuals/ponysay.texinfo +++ b/manuals/ponysay.texinfo @@ -322,6 +322,45 @@ compatibilies. Use Linux VT's compatbilies with KMS utilisation, despite your terminal's actual compatibilies. +@item +c +@itemx --colour ANSI-COLOUR +@opindex @option{+c} +@opindex @option{--colour} +Colour the balloon, including link and message (the parts that are not individually +specified.) The argument, should be a ANSI colour sequence without leading CSI and +without a tailing ‘m’, for example @code{1;31} will make it in red and bold (or bright +depending on the terminal.) + +@item --colour-bubble +@itemx --colour-balloon ANSI-COLOUR +@opindex @option{--colour-bubble} +@opindex @option{--colour-balloon} +Just like @option{--colour}, but it only colours the balloon, without the message +or link. + +@item --colour-link ANSI-COLOUR +@opindex @option{--colour-link} +Just like @option{--colour}, but it only colours the balloon link. + +@item --colour-msg +@itemx --colour-message ANSI-COLOUR +@opindex @option{--colour-msg} +@opindex @option{--colour-message} +Just like @option{--colour}, but it only colours the message. + +@item --colour-pony ANSI-COLOUR +@opindex @option{--colour-pony} +Just like @option{--colour}, but it colours the pony. This colouring has no +effect ony regular pony files, as it has its own colouring. + +@item --colour-wrap +@itemx --colour-hyphen ANSI-COLOUR +@opindex @option{--colour-wrap} +@opindex @option{--colour-hyphen} +Just like @option{--colour}, but it colours hyphen added by the word wrapping. +By default this is red (@code{31}), if you want uncoloured use @code{0}, +without @code{0} or @code{39}, the default @code{31} is presistent. + @end table @opindex @var{message} @@ -1926,6 +1965,8 @@ The word wrapper colours the inserted hyphens in red. Support for terminal capabilities emulation with the flags @option{-X}, @option{-V} and @option{-K}. @item Support for printing just the pony, using the flag @option{-o}. +@item +Colouring option flags are added. @end itemize diff --git a/ponysay.py b/ponysay.py index 1649402..2bad694 100755 --- a/ponysay.py +++ b/ponysay.py @@ -20,7 +20,6 @@ Authors of ponysay.py: Sven-Hendrik "svenstaro" Haase: Major contributor of the first implementation Jan Alexander "heftig" Steffens: Major contributor of the first implementation Kyah "L-four" Rindlisbacher: Patched the first implementation - ''' import os @@ -99,6 +98,16 @@ class Ponysay(): elif args.opts['-A'] is not None: self.list(); self.__extraponies(); self.list() elif args.opts['+A'] is not None: self.linklist(); self.__extraponies(); self.linklist() else: + ## Colouring features + if args.opts['--colour-pony'] is not None: + mode += '\033[' + ';'.join(args.opts['--colour-pony']) + 'm' + else: + mode += '\033[0m' + if args.opts['+c'] is not None: + if args.opts['--colour-msg'] is None: args.opts['--colour-msg'] = args.opts['+c'] + if args.opts['--colour-link'] is None: args.opts['--colour-link'] = args.opts['+c'] + if args.opts['--colour-bubble'] is None: args.opts['--colour-bubble'] = args.opts['+c'] + ## Other extra features self.__extraponies(args) self.__bestpony(args) @@ -656,6 +665,8 @@ class Ponysay(): msg = ''.join(sys.stdin.readlines()).rstrip() else: msg = args.message + if args.opts['--colour-msg'] is not None: + msg = '\033[' + ';'.join(args.opts['--colour-msg']) + 'm' + msg ## This algorithm should give some result as cowsay's (according to tests) if args.opts['-c'] is not None: @@ -705,8 +716,24 @@ class Ponysay(): ## Get balloon object balloon = self.__getballoon(self.__getballoonpath(args.opts['-b'])) if args.opts['-o'] is None else None + ## Get hyphen style + hyphencolour = '' + if args.opts['--colour-wrap'] is not None: + hyphencolour = '\033[' + ';'.join(args.opts['--colour-wrap']) + 'm' + hyphen = '\033[31m' + hyphencolour + '-' + + ## Link and balloon colouring + linkcolour = '' + if args.opts['--colour-link'] is not None: + linkcolour = '\033[' + ';'.join(args.opts['--colour-link']) + 'm' + ballooncolour = '' + if args.opts['--colour-bubble'] is not None: + ballooncolour = '\033[' + ';'.join(args.opts['--colour-bubble']) + 'm' + + ## Run cowsay replacement - backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap if messagewrap is not None else 40, width = widthtruncation, balloon = balloon) + backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap if messagewrap is not None else 40, + width = widthtruncation, balloon = balloon, hyphen = hyphen, linkcolour = linkcolour, ballooncolour = ballooncolour) backend.parse() output = backend.output if output.endswith('\n'): @@ -1215,17 +1242,21 @@ class Backend(): ''' Constructor Takes message [string], ponyfile [filename string], wrapcolumn [None or an int], - width [None or an int] and balloon [None or Balloon object] + width [None or an int], balloon [None or Balloon object], hyphen [string], + linkcolour [string] and ballooncolour [string] ''' - def __init__(self, message, ponyfile, wrapcolumn, width, balloon): + def __init__(self, message, ponyfile, wrapcolumn, width, balloon, hyphen, linkcolour, ballooncolour): self.message = message self.ponyfile = ponyfile self.wrapcolumn = wrapcolumn self.width = width self.balloon = balloon + self.hyphen = hyphen + self.ballooncolour = ballooncolour if self.balloon is not None: - self.link = {'\\' : self.balloon.link, '/' : self.balloon.linkmirror} + self.link = {'\\' : linkcolour + self.balloon.link, + '/' : linkcolour + self.balloon.linkmirror} else: self.link = {} @@ -1363,6 +1394,7 @@ class Backend(): w = int(props) balloon = self.__getballoon(w, h, indent) balloon = balloon.split('\n') + balloon = [AUTO_PUSH + self.ballooncolour + item + AUTO_POP for item in balloon] for b in balloon[0]: self.output += b + colourstack.feed(b) if lineindex == 0: @@ -1495,22 +1527,15 @@ class Backend(): if wrap is not None: msg = self.__wrapMessage(msg, wrap) - if '\033' in msg: - AUTO_PUSH = '\033[01010~' - AUTO_POP = '\033[10101~' - cstack = ColourStack(AUTO_PUSH, AUTO_POP) - buf = '' - for c in msg: - if c == '\n': - for cc in ('%s\n%s' % (AUTO_PUSH, AUTO_POP)): - buf += cc - buf += cstack.feed(cc) - else: - buf += c - buf += cstack.feed(c) - msg = buf + AUTO_PUSH = '\033[01010~' + AUTO_POP = '\033[10101~' + cstack = ColourStack(AUTO_PUSH, AUTO_POP) + buf = '' + for c in msg.replace('\n', AUTO_PUSH + self.ballooncolour + '\n' + AUTO_POP) + self.ballooncolour: + buf += c + buf += cstack.feed(c) - return self.balloon.get(width, height, msg.split('\n'), self.__len) + return self.balloon.get(width, height, buf.split('\n'), self.__len) ''' @@ -1587,7 +1612,7 @@ class Backend(): hyphen = m - 1 while b[hyphen] != '­': hyphen -= 1 - while map[mm + x] > hyphen: ## Only looking backward, if foreward is required the word is probabily not hythenated correctly + while map[mm + x] > hyphen: ## Only looking backward, if foreward is required the word is probabily not hyphenated correctly x -= 1 x += 1 m = map[mm + x] @@ -1635,7 +1660,7 @@ class Backend(): rc = '\n'.join(line.rstrip() for line in buf[:-1].split('\n')); rc = rc.replace('­', ''); # remove soft hyphens - rc = rc.replace('\0', '%s%s%s' % (AUTO_PUSH, '\033[31m-', AUTO_POP)) # TODO make configurable + rc = rc.replace('\0', '%s%s%s' % (AUTO_PUSH, self.hyphen, AUTO_POP)) # TODO make configurable return rc @@ -2075,6 +2100,13 @@ opts.add_argumentless(['-X', '--256-colours', '--256colours', '--x-colours']) opts.add_argumentless(['-V', '--tty-colours', '--ttycolours', '--vt-colours']) opts.add_argumentless(['-K', '--kms-colours', '--kmscolours']) +opts.add_argumented(['+c', '--colour'], arg = 'COLOUR') +opts.add_argumented(['--colour-bubble', '--colour-balloon'], arg = 'COLOUR') +opts.add_argumented(['--colour-link'], arg = 'COLOUR') +opts.add_argumented(['--colour-msg', '--colour-message'], arg = 'COLOUR') +opts.add_argumented(['--colour-pony'], arg = 'COLOUR') +opts.add_argumented(['--colour-wrap', '--colour-hyphen'], arg = 'COLOUR') + opts.add_argumentless(['-h', '--help'], help = 'Print this help message.') opts.add_argumentless(['-v', '--version'], help = 'Print the version of the program.') opts.add_argumentless(['-l', '--list'], help = 'List pony names.') -- cgit