mirror of
https://github.com/erkin/ponysay.git
synced 2024-11-25 13:57:59 +01:00
colour options are almost fully functional, inplace balloons are problematic
This commit is contained in:
parent
6b49c94128
commit
dc38ab4e6d
5 changed files with 104 additions and 22 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,6 +1,7 @@
|
||||||
## Private workspace directory
|
## Private workspace directory
|
||||||
|
|
||||||
/_/
|
/_/
|
||||||
|
/*.pony
|
||||||
|
|
||||||
|
|
||||||
## Backup files
|
## Backup files
|
||||||
|
|
|
@ -15,6 +15,8 @@ Version 2.8
|
||||||
|
|
||||||
Support for printing just the pony, using the flag -o.
|
Support for printing just the pony, using the flag -o.
|
||||||
|
|
||||||
|
Colouring option flags are added.
|
||||||
|
|
||||||
|
|
||||||
Version 2.7
|
Version 2.7
|
||||||
|
|
||||||
|
|
|
@ -92,6 +92,12 @@ the first list are the MLP:FiM and the second one are non-MLP:FiM.
|
||||||
.TP
|
.TP
|
||||||
.B \-o, \-\-pony\-only, \-\-ponyonly
|
.B \-o, \-\-pony\-only, \-\-ponyonly
|
||||||
Just print the pony, nothing else like the speech balloon.
|
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
|
.SH ENVIRONMENT
|
||||||
.TP
|
.TP
|
||||||
.B PONYSAY_BOTTOM
|
.B PONYSAY_BOTTOM
|
||||||
|
|
|
@ -322,6 +322,45 @@ compatibilies.
|
||||||
Use Linux VT's compatbilies with KMS utilisation, despite your terminal's actual
|
Use Linux VT's compatbilies with KMS utilisation, despite your terminal's actual
|
||||||
compatibilies.
|
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
|
@end table
|
||||||
|
|
||||||
@opindex @var{message}
|
@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}.
|
Support for terminal capabilities emulation with the flags @option{-X}, @option{-V} and @option{-K}.
|
||||||
@item
|
@item
|
||||||
Support for printing just the pony, using the flag @option{-o}.
|
Support for printing just the pony, using the flag @option{-o}.
|
||||||
|
@item
|
||||||
|
Colouring option flags are added.
|
||||||
@end itemize
|
@end itemize
|
||||||
|
|
||||||
|
|
||||||
|
|
76
ponysay.py
76
ponysay.py
|
@ -20,7 +20,6 @@ Authors of ponysay.py:
|
||||||
Sven-Hendrik "svenstaro" Haase: Major contributor of the first implementation
|
Sven-Hendrik "svenstaro" Haase: Major contributor of the first implementation
|
||||||
Jan Alexander "heftig" Steffens: Major contributor of the first implementation
|
Jan Alexander "heftig" Steffens: Major contributor of the first implementation
|
||||||
Kyah "L-four" Rindlisbacher: Patched the first implementation
|
Kyah "L-four" Rindlisbacher: Patched the first implementation
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
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.list(); self.__extraponies(); self.list()
|
||||||
elif args.opts['+A'] is not None: self.linklist(); self.__extraponies(); self.linklist()
|
elif args.opts['+A'] is not None: self.linklist(); self.__extraponies(); self.linklist()
|
||||||
else:
|
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
|
## Other extra features
|
||||||
self.__extraponies(args)
|
self.__extraponies(args)
|
||||||
self.__bestpony(args)
|
self.__bestpony(args)
|
||||||
|
@ -656,6 +665,8 @@ class Ponysay():
|
||||||
msg = ''.join(sys.stdin.readlines()).rstrip()
|
msg = ''.join(sys.stdin.readlines()).rstrip()
|
||||||
else:
|
else:
|
||||||
msg = args.message
|
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)
|
## This algorithm should give some result as cowsay's (according to tests)
|
||||||
if args.opts['-c'] is not None:
|
if args.opts['-c'] is not None:
|
||||||
|
@ -705,8 +716,24 @@ class Ponysay():
|
||||||
## Get balloon object
|
## Get balloon object
|
||||||
balloon = self.__getballoon(self.__getballoonpath(args.opts['-b'])) if args.opts['-o'] is None else None
|
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
|
## 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()
|
backend.parse()
|
||||||
output = backend.output
|
output = backend.output
|
||||||
if output.endswith('\n'):
|
if output.endswith('\n'):
|
||||||
|
@ -1215,17 +1242,21 @@ class Backend():
|
||||||
'''
|
'''
|
||||||
Constructor
|
Constructor
|
||||||
Takes message [string], ponyfile [filename string], wrapcolumn [None or an int],
|
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.message = message
|
||||||
self.ponyfile = ponyfile
|
self.ponyfile = ponyfile
|
||||||
self.wrapcolumn = wrapcolumn
|
self.wrapcolumn = wrapcolumn
|
||||||
self.width = width
|
self.width = width
|
||||||
self.balloon = balloon
|
self.balloon = balloon
|
||||||
|
self.hyphen = hyphen
|
||||||
|
self.ballooncolour = ballooncolour
|
||||||
|
|
||||||
if self.balloon is not None:
|
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:
|
else:
|
||||||
self.link = {}
|
self.link = {}
|
||||||
|
|
||||||
|
@ -1363,6 +1394,7 @@ class Backend():
|
||||||
w = int(props)
|
w = int(props)
|
||||||
balloon = self.__getballoon(w, h, indent)
|
balloon = self.__getballoon(w, h, indent)
|
||||||
balloon = balloon.split('\n')
|
balloon = balloon.split('\n')
|
||||||
|
balloon = [AUTO_PUSH + self.ballooncolour + item + AUTO_POP for item in balloon]
|
||||||
for b in balloon[0]:
|
for b in balloon[0]:
|
||||||
self.output += b + colourstack.feed(b)
|
self.output += b + colourstack.feed(b)
|
||||||
if lineindex == 0:
|
if lineindex == 0:
|
||||||
|
@ -1495,22 +1527,15 @@ class Backend():
|
||||||
if wrap is not None:
|
if wrap is not None:
|
||||||
msg = self.__wrapMessage(msg, wrap)
|
msg = self.__wrapMessage(msg, wrap)
|
||||||
|
|
||||||
if '\033' in msg:
|
AUTO_PUSH = '\033[01010~'
|
||||||
AUTO_PUSH = '\033[01010~'
|
AUTO_POP = '\033[10101~'
|
||||||
AUTO_POP = '\033[10101~'
|
cstack = ColourStack(AUTO_PUSH, AUTO_POP)
|
||||||
cstack = ColourStack(AUTO_PUSH, AUTO_POP)
|
buf = ''
|
||||||
buf = ''
|
for c in msg.replace('\n', AUTO_PUSH + self.ballooncolour + '\n' + AUTO_POP) + self.ballooncolour:
|
||||||
for c in msg:
|
buf += c
|
||||||
if c == '\n':
|
buf += cstack.feed(c)
|
||||||
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
|
|
||||||
|
|
||||||
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
|
hyphen = m - 1
|
||||||
while b[hyphen] != '':
|
while b[hyphen] != '':
|
||||||
hyphen -= 1
|
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
|
||||||
x += 1
|
x += 1
|
||||||
m = map[mm + x]
|
m = map[mm + x]
|
||||||
|
@ -1635,7 +1660,7 @@ class Backend():
|
||||||
|
|
||||||
rc = '\n'.join(line.rstrip() for line in buf[:-1].split('\n'));
|
rc = '\n'.join(line.rstrip() for line in buf[:-1].split('\n'));
|
||||||
rc = rc.replace('', ''); # remove soft hyphens
|
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
|
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(['-V', '--tty-colours', '--ttycolours', '--vt-colours'])
|
||||||
opts.add_argumentless(['-K', '--kms-colours', '--kmscolours'])
|
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(['-h', '--help'], help = 'Print this help message.')
|
||||||
opts.add_argumentless(['-v', '--version'], help = 'Print the version of the program.')
|
opts.add_argumentless(['-v', '--version'], help = 'Print the version of the program.')
|
||||||
opts.add_argumentless(['-l', '--list'], help = 'List pony names.')
|
opts.add_argumentless(['-l', '--list'], help = 'List pony names.')
|
||||||
|
|
Loading…
Reference in a new issue