mirror of
https://github.com/erkin/ponysay.git
synced 2024-11-23 04:47:59 +01:00
merge conflict resolved
This commit is contained in:
commit
0925635f44
1 changed files with 33 additions and 25 deletions
58
ponysay.py
58
ponysay.py
|
@ -18,7 +18,7 @@ from subprocess import Popen, PIPE
|
||||||
'''
|
'''
|
||||||
The version of ponysay
|
The version of ponysay
|
||||||
'''
|
'''
|
||||||
VERSION = "2.0-alpha"
|
VERSION = '2.0-alpha'
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -55,9 +55,12 @@ for quotedir in _quotedirs:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
Argument parsing
|
||||||
|
'''
|
||||||
parser = argparse.ArgumentParser(description = 'Ponysay, like cowsay with ponies')
|
parser = argparse.ArgumentParser(description = 'Ponysay, like cowsay with ponies')
|
||||||
|
|
||||||
parser.add_argument('-v', '--version', action = 'version', version = '%s %s' % ("ponysay", VERSION))
|
parser.add_argument('-v', '--version', action = 'version', version = '%s %s' % ('ponysay', VERSION))
|
||||||
parser.add_argument('-l', '--list', action = 'store_true', dest = 'list', help = 'list pony files')
|
parser.add_argument('-l', '--list', action = 'store_true', dest = 'list', help = 'list pony files')
|
||||||
parser.add_argument('-L', '--altlist', action = 'store_true', dest = 'linklist', help = 'list pony files with alternatives')
|
parser.add_argument('-L', '--altlist', action = 'store_true', dest = 'linklist', help = 'list pony files with alternatives')
|
||||||
parser.add_argument( '--quoters', action = 'store_true', dest = 'quoters', help = 'list ponies with quotes (visible in -l and -L)') # for shell completions
|
parser.add_argument( '--quoters', action = 'store_true', dest = 'quoters', help = 'list ponies with quotes (visible in -l and -L)') # for shell completions
|
||||||
|
@ -104,6 +107,21 @@ class ponysay():
|
||||||
|
|
||||||
return ponies
|
return ponies
|
||||||
|
|
||||||
|
'''
|
||||||
|
Returns one .pony-file with full path, names is filter for names, also accepts filepaths
|
||||||
|
'''
|
||||||
|
def __getponypath(self, names = None):
|
||||||
|
ponies = {}
|
||||||
|
|
||||||
|
for name in names:
|
||||||
|
if os.path.isfile(name):
|
||||||
|
return name
|
||||||
|
|
||||||
|
for ponydir in ponydirs:
|
||||||
|
for ponyfile in os.listdir(ponydir):
|
||||||
|
ponies[ponyfile[:-5]] = ponydir + ponyfile
|
||||||
|
|
||||||
|
return ponies[names[random.randrange(0, len(names))]]
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Returns a list with all (pony, quote file) pairs
|
Returns a list with all (pony, quote file) pairs
|
||||||
|
@ -130,7 +148,7 @@ class ponysay():
|
||||||
Lists the available ponies
|
Lists the available ponies
|
||||||
'''
|
'''
|
||||||
def list(self):
|
def list(self):
|
||||||
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(" ")
|
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(' ')
|
||||||
termsize = [int(item) for item in termsize]
|
termsize = [int(item) for item in termsize]
|
||||||
|
|
||||||
quoters = self.__quoters()
|
quoters = self.__quoters()
|
||||||
|
@ -147,20 +165,20 @@ class ponysay():
|
||||||
x = 0
|
x = 0
|
||||||
for pony in ponies:
|
for pony in ponies:
|
||||||
spacing = ' ' * (width - len(pony))
|
spacing = ' ' * (width - len(pony))
|
||||||
print(('\033[1m' + pony + '\033[21m' if (pony in quoters) else pony) + spacing, end="") # Print ponyfilename
|
print(('\033[1m' + pony + '\033[21m' if (pony in quoters) else pony) + spacing, end='') # Print ponyfilename
|
||||||
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()
|
||||||
x = 0
|
x = 0
|
||||||
|
|
||||||
print("\n");
|
print('\n');
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Lists the available ponies with alternatives inside brackets
|
Lists the available ponies with alternatives inside brackets
|
||||||
'''
|
'''
|
||||||
def linklist(self):
|
def linklist(self):
|
||||||
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(" ")
|
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(' ')
|
||||||
termsize = [int(item) for item in termsize]
|
termsize = [int(item) for item in termsize]
|
||||||
|
|
||||||
quoters = self.__quoters()
|
quoters = self.__quoters()
|
||||||
|
@ -171,11 +189,11 @@ class ponysay():
|
||||||
files = os.listdir(ponydir)
|
files = os.listdir(ponydir)
|
||||||
files = [item[:-5] for item in files] # remove .pony from file name
|
files = [item[:-5] for item in files] # remove .pony from file name
|
||||||
files.sort()
|
files.sort()
|
||||||
pairs = [(item, os.readlink(ponydir + item + ".pony") if os.path.islink(ponydir + item + ".pony") else '') for item in files]
|
pairs = [(item, os.readlink(ponydir + item + '.pony') if os.path.islink(ponydir + item + '.pony') else '') for item in files]
|
||||||
|
|
||||||
ponymap = {}
|
ponymap = {}
|
||||||
for pair in pairs:
|
for pair in pairs:
|
||||||
if pair[1] == "":
|
if pair[1] == '':
|
||||||
if pair[0] not in ponymap:
|
if pair[0] not in ponymap:
|
||||||
ponymap[pair[0]] = []
|
ponymap[pair[0]] = []
|
||||||
else:
|
else:
|
||||||
|
@ -196,16 +214,16 @@ class ponysay():
|
||||||
syms = ponymap[pony]
|
syms = ponymap[pony]
|
||||||
if len(syms) > 0:
|
if len(syms) > 0:
|
||||||
w += 2 + len(syms)
|
w += 2 + len(syms)
|
||||||
item += " ("
|
item += ' ('
|
||||||
first = True
|
first = True
|
||||||
for sym in syms:
|
for sym in syms:
|
||||||
w += len(sym)
|
w += len(sym)
|
||||||
if not first:
|
if not first:
|
||||||
item += " "
|
item += ' '
|
||||||
else:
|
else:
|
||||||
first = False
|
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.append(item)
|
ponies.append(item)
|
||||||
widths.append(w)
|
widths.append(w)
|
||||||
if width < w:
|
if width < w:
|
||||||
|
@ -217,13 +235,13 @@ 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 ponyfilename
|
||||||
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()
|
||||||
x = 0
|
x = 0
|
||||||
|
|
||||||
print("\n");
|
print('\n');
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
@ -270,19 +288,9 @@ class ponysay():
|
||||||
else:
|
else:
|
||||||
msg = args.message
|
msg = args.message
|
||||||
|
|
||||||
if args.pony == None:
|
pony = self.__getponypath(args.pony)
|
||||||
ponies = [] # Make array with direct paths to all ponies
|
|
||||||
for ponydir in ponydirs:
|
|
||||||
for ponyfile in os.listdir(ponydir):
|
|
||||||
ponies.append(ponydir + ponyfile)
|
|
||||||
|
|
||||||
pony = ponies[random.randrange(0, len(ponies) - 1)] # Select random pony
|
os.system('cowsay -f ' + pony + ' \'' + msg + '\'')
|
||||||
else:
|
|
||||||
for ponydir in ponydirs:
|
|
||||||
if os.path.isfile(ponydir + args.pony[0]):
|
|
||||||
pony = ponydir + args.pony[0]
|
|
||||||
|
|
||||||
os.system('cowsay -f ' + pony + ' "' + msg + '"')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue