balloon styles: -b and -B options are implemented

This commit is contained in:
Mattias Andrée 2012-08-21 17:26:21 +02:00
parent b66b3b5bfc
commit 66cbb352eb
6 changed files with 116 additions and 41 deletions

View file

@ -5,7 +5,7 @@ _ponysay()
local cur prev words cword local cur prev words cword
_init_completion -n = || return _init_completion -n = || return
options='--version --help --list --altlist --pony --wrap --quote' options='--version --help --list --altlist --pony --wrap --quote --balloonlist --balloon'
COMPREPLY=( $( compgen -W "$options" -- "$cur" ) ) COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
if [ $prev = "-f" ] || [ $prev = "--pony" ]; then if [ $prev = "-f" ] || [ $prev = "--pony" ]; then
@ -16,6 +16,10 @@ _ponysay()
quoters=$('/usr/bin/ponysay' --quoters) quoters=$('/usr/bin/ponysay' --quoters)
COMPREPLY=( $( compgen -W "$quoters" -- "$cur" ) ) COMPREPLY=( $( compgen -W "$quoters" -- "$cur" ) )
elif [ $prev = "-b" ] || [ $prev = "--balloon" ]; then
balloons=$('/usr/bin/ponysay' --balloonlist)
COMPREPLY=( $( compgen -W "$balloons" -- "$cur" ) )
elif [ $prev = "-W" ] || [ $prev = "--wrap" ]; then elif [ $prev = "-W" ] || [ $prev = "--wrap" ]; then
cols=$(( `stty size | cut -d ' ' -f 2` - 10 )) cols=$(( `stty size | cut -d ' ' -f 2` - 10 ))
COMPREPLY=( $cols $(( $cols / 2 )) 100 60 ) COMPREPLY=( $cols $(( $cols / 2 )) 100 60 )

View file

@ -5,17 +5,20 @@
set -g ponies ('/usr/bin/ponysay' --onelist) set -g ponies ('/usr/bin/ponysay' --onelist)
set -g quoters ('/usr/bin/ponysay' --quoters) set -g quoters ('/usr/bin/ponysay' --quoters)
set -g balloons ('/usr/bin/ponysay' --balloonlist)
complete --command ponysay --short-option h --long-option help --description 'help of ponysay' complete --command ponysay --short-option h --long-option help --description 'help of ponysay'
complete --command ponysay --short-option v --long-option version --description 'version of ponysay' complete --command ponysay --short-option v --long-option version --description 'version of ponysay'
complete --command ponysay --short-option l --long-option list --description 'list pony files' complete --command ponysay --short-option l --long-option list --description 'list pony names'
complete --command ponysay --short-option L --long-option altlist --description 'list pony files with alternatives' complete --command ponysay --short-option L --long-option altlist --description 'list pony names with alternatives'
complete --command ponysay --short-option B --long-option balloonlist --description 'list balloon style names'
complete --command ponysay --short-option f --long-option pony --arguments "$ponies" --description 'pony' complete --command ponysay --short-option f --long-option pony --arguments "$ponies" --description 'pony'
complete --command ponysay --short-option q --long-option quote --arguments "$quoters" --no-files --description 'pony' complete --command ponysay --short-option q --long-option quote --arguments "$quoters" --no-files --description 'pony'
complete --command ponysay --short-option b --long-option balloon --arguments "$balloons" --description 'balloon style'
complete --command ponysay --short-option W --long-option wrap --arguments 'Integer' --description 'specify the column when the message should be wrapped' complete --command ponysay --short-option W --long-option wrap --arguments 'Integer' --description 'specify the column when the message should be wrapped'
complete --command ponysay --arguments 'MESSAGE' complete --command ponysay --arguments 'MESSAGE'
set -e ponies set -e ponies
set -e quoters set -e quoters
set -e balloons

View file

@ -2,10 +2,12 @@
_shortopts=( _shortopts=(
'-v[Show version and exit]' '-v[Show version and exit]'
'-h[Show this help and exit]' '-h[Show this help and exit]'
'-l[list ponyfiles]' '-l[list pony names]'
'-L[list ponyfiles with alternatives]' '-L[list pony names with alternatives]'
'-f[Select a pony (either a filename or a ponyname]: :_path_files -W '/usr/share/ponysay/ponies' -g "*(\:r)"' '-B[list balloon style names]'
'-f[Select a pony (either a file name or a pony name]: :_path_files -W '/usr/share/ponysay/ponies' -g "*(\:r)"'
'-q[Select ponies for MLP:FiM quotes]' '-q[Select ponies for MLP:FiM quotes]'
'-b[Selecy a balloon style]'
'-W[The screen column where the message should be wrapped]' '-W[The screen column where the message should be wrapped]'
) )
_arguments -s : \ _arguments -s : \

2
configure vendored
View file

@ -6,7 +6,7 @@ INFODESC=\''My Little Ponies for your terminal'\'
SHELL='bash' SHELL='bash'
completions='completion/bash-completion.sh completion/fish-completion.fish completion/zsh-completion.zsh' completions='completion/bash-completion.sh completion/fish-completion.fish completion/zsh-completion.zsh'
shareDirs='ponies ttyponies quotes' shareDirs='ponies ttyponies quotes balloons'
manFiles='manuals/manpage.6 manuals/manpage.es.6' manFiles='manuals/manpage.6 manuals/manpage.es.6'
licenseFiles='COPYING' licenseFiles='COPYING'

View file

@ -159,8 +159,7 @@ supersedes file names.
Specify the balloon style that should used, this can either be a file name or a Specify the balloon style that should used, this can either be a file name or a
balloon name printed by @command{ponysay -B}. This option can be used multiple balloon name printed by @command{ponysay -B}. This option can be used multiple
times to specify a set of styles from which one will be selected randomly. If no times to specify a set of styles from which one will be selected randomly. If no
pony is specified one will be selected randomly. Any existing balloon name balloon style is specified a fallback style will be used.
supersedes file names.
@item -q [PONY...] @item -q [PONY...]
@itemx --quote [PONY...] @itemx --quote [PONY...]

129
ponysay
View file

@ -52,6 +52,7 @@ class Ponysay():
elif args.opts['-v'] is not None: self.version() elif args.opts['-v'] is not None: self.version()
elif args.opts['-l'] is not None: self.list() elif args.opts['-l'] is not None: self.list()
elif args.opts['-L'] is not None: self.linklist() elif args.opts['-L'] is not None: self.linklist()
elif args.opts['-B'] is not None: self.balloonlist()
elif args.opts['-q'] is not None: self.quote(args) elif args.opts['-q'] is not None: self.quote(args)
else: self.print_pony(args) else: self.print_pony(args)
@ -61,7 +62,7 @@ class Ponysay():
## ##
''' '''
Returns one file with full path, names is filter for names, also accepts filepaths. Returns one file with full path, names is filter for names, also accepts filepaths
''' '''
def __getponypath(self, names = None): def __getponypath(self, names = None):
ponies = {} ponies = {}
@ -160,10 +161,13 @@ class Ponysay():
quoters = self.__quoters() quoters = self.__quoters()
for ponydir in ponydirs: # Loop ponydirs for ponydir in ponydirs: # Loop ponydirs
print('\033[1mponyfiles located in ' + ponydir + '\033[21m') print('\033[1mponies located in ' + ponydir + '\033[21m')
ponies = os.listdir(ponydir) _ponies = os.listdir(ponydir)
ponies = [item[:-5] for item in ponies] # remove .pony from file name ponies = []
for pony in _ponies:
if (len(pony) > 5) and (pony[-5:] == '.pony'):
ponies.append(pony[:-5])
ponies.sort() ponies.sort()
width = len(max(ponies, key = len)) + 2 # Get the longest ponyfilename lenght + 2 spaces width = len(max(ponies, key = len)) + 2 # Get the longest ponyfilename lenght + 2 spaces
@ -176,7 +180,7 @@ class Ponysay():
if x > (termsize[1] - width): # If too wide, make new line if x > (termsize[1] - width): # If too wide, make new line
print() print()
x = 0 x = 0
print('\n'); print('\n');
@ -188,12 +192,15 @@ class Ponysay():
quoters = self.__quoters() quoters = self.__quoters()
for ponydir in ponydirs: # Loop ponydirs for ponydir in ponydirs: # Loop ponydirs
print('\033[1mponyfiles located in ' + ponydir + '\033[21m') print('\033[1mponies located in ' + ponydir + '\033[21m')
files = os.listdir(ponydir) _ponies = os.listdir(ponydir)
files = [item[:-5] for item in files] # remove .pony from file name ponies = []
files.sort() for pony in _ponies:
pairs = [(item, os.readlink(ponydir + item + '.pony') if os.path.islink(ponydir + item + '.pony') else '') for item in files] if (len(pony) > 5) and (pony[-5:] == '.pony'):
ponies.append(pony[:-5])
ponies.sort()
pairs = [(pony, os.readlink(ponydir + pony + '.pony') if os.path.islink(ponydir + pony + '.pony') else '') for pony in ponies]
ponymap = {} ponymap = {}
for pair in pairs: for pair in pairs:
@ -239,7 +246,7 @@ class Ponysay():
for pony in ponies: for pony in ponies:
spacing = ' ' * (width - widths[index]) spacing = ' ' * (width - widths[index])
index += 1 index += 1
print(pony + spacing, end='') # Print ponyfilename print(pony + spacing, end='') # Print pony file name
x += width x += width
if x > (termsize[1] - width): # If too wide, make new line if x > (termsize[1] - width): # If too wide, make new line
print() print()
@ -268,10 +275,13 @@ class Ponysay():
''' '''
def onelist(self): def onelist(self):
last = '' last = ''
ponies = [] _ponies = []
for ponydir in ponydirs: # Loop ponydirs for ponydir in ponydirs: # Loop ponydirs
ponies += os.listdir(ponydir) _ponies += os.listdir(ponydir)
ponies = [item[:-5] for item in ponies] # remove .pony from file name ponies = []
for pony in _ponies:
if (len(pony) > 5) and (pony[-5:] == '.pony'):
ponies.append(pony[:-5])
ponies.sort() ponies.sort()
for pony in ponies: for pony in ponies:
if not pony == last: if not pony == last:
@ -285,7 +295,42 @@ class Ponysay():
## ##
''' '''
Returns one file with full path, names is filter for names, also accepts filepaths Prints a list of all balloons
'''
def balloonlist(self):
termsize = self.__gettermsize()
balloonset = set()
for balloondir in balloondirs:
for balloon in os.listdir(balloondir):
if isthink and (len(balloon) > 6) and (balloon[-6:] == '.think'):
balloon = balloon[:-6]
elif (not isthink) and (len(balloon) > 4) and (balloon[-4:] == '.say'):
balloon = balloon[:-4]
else:
continue
if balloon not in balloonset:
balloonset.add(balloon)
balloons = list(balloonset)
balloons.sort()
width = len(max(balloons, key = len)) + 2
x = 0
for balloon in balloons:
spacing = ' ' * (width - len(balloon))
print(balloon + spacing, end='')
x += width
if x > (termsize[1] - width):
print()
x = 0
print();
'''
Returns one file with full path, names is filter for style names, also accepts filepaths
''' '''
def __getballoonpath(self, names): def __getballoonpath(self, names):
if names is None: if names is None:
@ -293,8 +338,14 @@ class Ponysay():
balloons = {} balloons = {}
for balloondir in balloondirs: for balloondir in balloondirs:
for balloonfile in os.listdir(balloondir): for balloon in os.listdir(balloondir):
balloon = balloonfile[:-5] balloonfile = balloon
if isthink and (len(balloon) > 6) and (balloon[-6:] == '.think'):
balloon = balloon[:-6]
elif (not isthink) and (len(balloon) > 4) and (balloon[-4:] == '.say'):
balloon = balloon[:-4]
else:
continue
if balloon not in balloons: if balloon not in balloons:
balloons[balloon] = balloondir + balloonfile balloons[balloon] = balloondir + balloonfile
@ -390,7 +441,9 @@ class Ponysay():
widthtruncation = self.__gettermsize()[1] if env_width not in ('yes', 'y', '1') else None widthtruncation = self.__gettermsize()[1] if env_width not in ('yes', 'y', '1') else None
messagewrap = int(args.opts['-W'][0]) if args.opts['-W'] is not None else None messagewrap = int(args.opts['-W'][0]) if args.opts['-W'] is not None else None
backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap if messagewrap is not None else 40, width = widthtruncation, balloon = self.__getballoon(None)) balloon = self.__getballoon(self.__getballoonpath(args.opts['-b']))
backend = Backend(message = msg, ponyfile = pony, wrapcolumn = messagewrap if messagewrap is not None else 40, width = widthtruncation, balloon = balloon)
backend.parse() backend.parse()
output = backend.output output = backend.output
if (len(output) > 0) and (output[-1] == '\n'): if (len(output) > 0) and (output[-1] == '\n'):
@ -1269,7 +1322,7 @@ class ColourStack():
part = self.seq[i] part = self.seq[i]
p = 0 if part == '' else int(part) p = 0 if part == '' else int(part)
i += 1 i += 1
if p == '': self.stack[0][1:] = [None, None, [False] * 9] if p == 0: self.stack[0][1:] = [None, None, [False] * 9]
elif (1 <= p) and (p <= 9): self.stack[0][3][p - 1] = True elif (1 <= p) and (p <= 9): self.stack[0][3][p - 1] = True
elif (21 <= p) and (p <= 29): self.stack[0][3][p - 21] = False elif (21 <= p) and (p <= 29): self.stack[0][3][p - 21] = False
elif p == 39: self.stack[0][1] = None elif p == 39: self.stack[0][1] = None
@ -1313,7 +1366,8 @@ linuxvt = os.environ['TERM'] == 'linux'
''' '''
Whether the script is executed as ponythink Whether the script is executed as ponythink
''' '''
isthink = (len(__file__) >= 8) and (__file__[-8:] == 'think.py') isthink = (len(__file__) >= 5) and (__file__[-5:] == 'think')
isthink = ((len(__file__) >= 8) and (__file__[-8:] == 'think.py')) or isthink
''' '''
@ -1354,15 +1408,26 @@ for quotedir in _quotedirs:
quotedirs.append(quotedir) quotedirs.append(quotedir)
'''
The directories where balloon style files are stored
'''
balloondirs = []
_balloondirs = [HOME + '/.local/share/ponysay/balloons/', INSTALLDIR + '/share/ponysay/balloons/']
for balloondir in _balloondirs:
if os.path.isdir(balloondir):
balloondirs.append(balloondir)
usage_saythink = '\033[34;1m(ponysay | ponythink)\033[21;39m' usage_saythink = '\033[34;1m(ponysay | ponythink)\033[21;39m'
usage_wrap = '--wrap \033[4mCOLUMN\033[24m' usage_wrap = '--wrap \033[4mCOLUMN\033[24m'
usage_listhelp = '(--list | ---altlist | --version | --help)' usage_balloon = '--balloon \033[4mSTYLE\033[24m'
usage_listhelp = '(--list | --altlist | --balloonlist | --version | --help)'
usage_file = '[--pony \033[4mPONY\033[24m]... ([--] \033[4mmessage\033[24m | <<<\033[4mmessage\033[24m)' usage_file = '[--pony \033[4mPONY\033[24m]... ([--] \033[4mmessage\033[24m | <<<\033[4mmessage\033[24m)'
usage_quote = '--quote [\033[4mPONY\033[24m...]' usage_quote = '--quote [\033[4mPONY\033[24m...]'
usage = '%s %s\n%s [%s] %s\n%s [%s] %s' % (usage_saythink, usage_listhelp, usage = '%s %s\n%s [%s] [%s] %s\n%s [%s] [%s] %s' % (usage_saythink, usage_listhelp,
usage_saythink, usage_wrap, usage_file, usage_saythink, usage_wrap, usage_balloon, usage_file,
usage_saythink, usage_wrap, usage_quote) usage_saythink, usage_wrap, usage_balloon, usage_quote)
usage = usage.replace('\033[', '\0') usage = usage.replace('\033[', '\0')
for sym in ('[', ']', '(', ')', '|', '...'): for sym in ('[', ']', '(', ')', '|', '...'):
@ -1384,13 +1449,15 @@ run `man ponysay`. Ponysay has so much more to offer than described here.''')
opts.add_argumentless(['--quoters']) opts.add_argumentless(['--quoters'])
opts.add_argumentless(['--onelist']) opts.add_argumentless(['--onelist'])
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 files.') opts.add_argumentless(['-l', '--list'], help = 'List pony names.')
opts.add_argumentless(['-L', '--altlist'], help = 'List pony files with alternatives.') opts.add_argumentless(['-L', '--altlist'], help = 'List pony names with alternatives.')
opts.add_argumented( ['-W', '--wrap'], arg = 'COLUMN', help = 'Specify the column when the message should be wrapped.') opts.add_argumentless(['-B', '--balloonlist'], help = 'list balloon styles.')
opts.add_argumented( ['-f', '--pony'], arg = 'PONY', help = 'Select a pony.\nEither a file name or a pony name.') opts.add_argumented( ['-W', '--wrap'], arg = 'COLUMN', help = 'Specify the column when the message should be wrapped.')
opts.add_variadic( ['-q', '--quote'], arg = 'PONY', help = 'Select a ponies which will quote themself.') opts.add_argumented( ['-b', '--bubble', '--balloon'], arg = 'STYLE', help = 'Select a balloon style.')
opts.add_argumented( ['-f', '--file', '--pony'], arg = 'PONY', help = 'Select a pony.\nEither a file name or a pony name.')
opts.add_variadic( ['-q', '--quote'], arg = 'PONY', help = 'Select a ponies which will quote themself.')
opts.parse() opts.parse()