code refinement

This commit is contained in:
Mattias Andrée 2012-08-23 00:56:06 +02:00
parent dbb167d180
commit adb370438b

81
ponysay
View file

@ -25,7 +25,7 @@ from subprocess import Popen, PIPE
''' '''
The version of ponysay The version of ponysay
''' '''
VERSION = '2.3' VERSION = '2.4'
''' '''
@ -89,8 +89,7 @@ class Ponysay():
for ponydir in ponydirs: for ponydir in ponydirs:
if os.path.isfile(ponydir + 'best.pony') or os.path.islink(ponydir + 'best.pony'): if os.path.isfile(ponydir + 'best.pony') or os.path.islink(ponydir + 'best.pony'):
pony = os.path.realpath(ponydir + 'best.pony') # Canonical path pony = os.path.realpath(ponydir + 'best.pony') # Canonical path
if args.opts['-q'] is not None: args.opts['-q'] = [pony] args.opts['-f' if args.opts['-q'] is None else '-q'] = [pony]
else: args.opts['-f'] = [pony]
break break
@ -274,11 +273,29 @@ class Ponysay():
## Listing methods ## Listing methods
## ##
'''
Columnise a list and prints it
'''
def __columnise(self, ponies):
termwidth = termsize = self.__gettermsize()[1]
ponies.sort(key = lambda pony : pony[0])
widths = [UCS.dispLen(pony[0]) for pony in ponies]
width = max(widths) + 2 # longest pony file name + space between columns
x = termwidth - width
for j in range(0, len(ponies)):
print(ponies[j][1] + ' ' * (width - widths[j]), end='') # Print pony file name
x -= width
if x < 0: # If too wide, make new line
print()
x = termwidth - width
print('' if x == 0 else '\n');
''' '''
Lists the available ponies Lists the available ponies
''' '''
def list(self): def list(self):
termsize = self.__gettermsize()
quoters = self.__quoters() quoters = self.__quoters()
for ponydir in ponydirs: # Loop ponydirs for ponydir in ponydirs: # Loop ponydirs
@ -288,24 +305,12 @@ class Ponysay():
if (len(pony) > 5) and (pony[-5:] == '.pony'): if (len(pony) > 5) and (pony[-5:] == '.pony'):
ponies.append(pony[:-5]) ponies.append(pony[:-5])
self.__ucsise(ponies) self.__ucsise(ponies)
ponies.sort()
if len(ponies) == 0: if len(ponies) == 0:
continue continue
print('\033[1mponies located in ' + ponydir + '\033[21m') print('\033[1mponies located in ' + ponydir + '\033[21m')
width = UCS.dispLen(max(ponies, key = UCS.dispLen)) + 2 # Get the longest ponyfilename lenght + 2 spaces self.__columnise([(pony, '\033[1m' + pony + '\033[21m' if pony in quoters else pony) for pony in ponies])
x = 0
for pony in ponies:
spacing = ' ' * (width - UCS.dispLen(pony))
print(('\033[1m' + pony + '\033[21m' if (pony in quoters) else pony) + spacing, end='') # Print ponyfilename
x += width
if x > (termsize[1] - width): # If too wide, make new line
print()
x = 0
print('' if x == 0 else '\n');
''' '''
@ -327,7 +332,7 @@ class Ponysay():
print('\033[1mponies located in ' + ponydir + '\033[21m') print('\033[1mponies located in ' + ponydir + '\033[21m')
pseudolinkmap = {} pseudolinkmap = {}
self.__ucsise(ponies, pseudolinkmap) ##TODO self.__ucsise(ponies, pseudolinkmap)
pairs = [] pairs = []
for pony in ponies: for pony in ponies:
if pony in pseudolinkmap: if pony in pseudolinkmap:
@ -362,31 +367,15 @@ class Ponysay():
first = True first = True
for sym in syms: for sym in syms:
w += UCS.dispLen(sym) w += UCS.dispLen(sym)
if not first: if first: first = False
item += ' ' else: item += ' '
else:
first = False
item += '\033[1m' + sym + '\033[21m' if (sym in quoters) else sym item += '\033[1m' + sym + '\033[21m' if (sym in quoters) else sym
item += ')' item += ')'
ponies[item] = w ponies[(item.replace('\033[1m', '').replace('\033[21m', ''), item)] = w
if width < w: if width < w:
width = w width = w
width += 2; self.__columnise(list(ponies))
x = 0
index = 0
ponylist = list(ponies)
ponylist.sort(key = lambda pony : pony.split(' ')[0].replace('\033[1m', '').replace('\033[21m', ''))
for pony in ponylist:
spacing = ' ' * (width - ponies[pony])
index += 1
print(pony + spacing, end='') # Print pony file name
x += width
if x > termsize[1] - width: # If too wide, make new line
print()
x = 0
print('' if x == 0 else '\n');
''' '''
@ -448,21 +437,7 @@ class Ponysay():
if balloon not in balloonset: if balloon not in balloonset:
balloonset.add(balloon) balloonset.add(balloon)
balloons = list(balloonset) self.__columnise([(balloon, balloon) for balloon in list(balloonset)])
balloons.sort()
width = UCS.dispLen(max(balloons, key = UCS.dispLen)) + 2
x = 0
for balloon in balloons:
spacing = ' ' * (width - UCS.dispLen(balloon))
print(balloon + spacing, end='')
x += width
if x > (termsize[1] - width):
print()
x = 0
print();
''' '''