2012-08-18 13:15:12 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
'''
|
|
|
|
ponysay.py - POC of ponysay in python
|
2012-08-18 14:26:53 +02:00
|
|
|
Copyright (C) 2012 Elis "etu" Axelsson, Mattias "maandree" Andrée
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
License: WTFPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import random
|
2012-08-18 14:26:53 +02:00
|
|
|
from subprocess import Popen, PIPE
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
'''
|
|
|
|
The version of ponysay
|
|
|
|
'''
|
2012-08-18 14:29:57 +02:00
|
|
|
VERSION = "2.0-alpha"
|
2012-08-18 14:26:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
The directory where ponysay is installed, this is modified when building with make
|
|
|
|
'''
|
2012-08-18 14:29:57 +02:00
|
|
|
INSTALLDIR = '/usr'
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
'''
|
|
|
|
The directories where pony files are stored, ttyponies/ are used if the terminal is Linux VT (also known as TTY)
|
|
|
|
'''
|
|
|
|
ponydirs = []
|
2012-08-18 14:29:57 +02:00
|
|
|
if os.environ['TERM'] == 'linux': _ponydirs = [INSTALLDIR + '/share/ponysay/ttyponies/', os.environ['HOME'] + '/.local/share/ponysay/ttyponies/']
|
|
|
|
else: _ponydirs = [INSTALLDIR + '/share/ponysay/ponies/', os.environ['HOME'] + '/.local/share/ponysay/ponies/' ]
|
2012-08-18 14:26:53 +02:00
|
|
|
for ponydir in _ponydirs:
|
|
|
|
if os.path.isdir(ponydir):
|
|
|
|
ponydirs.append(ponydir)
|
|
|
|
|
2012-08-18 13:15:12 +02:00
|
|
|
parser = argparse.ArgumentParser(description = 'Ponysay, like cowsay with ponies')
|
|
|
|
|
2012-08-18 14:29:57 +02:00
|
|
|
parser.add_argument('-v', '--version', action = 'version', version = '%s %s' % ("ponysay", VERSION))
|
2012-08-18 13:15:12 +02:00
|
|
|
parser.add_argument('-l', '--list', action = 'store_true', dest = 'list', help = 'list pony files')
|
|
|
|
parser.add_argument('-f', '--pony', action = 'append', dest = 'pony', help = 'select a pony (either a file name or a pony name)')
|
|
|
|
parser.add_argument('message', nargs = '?', help = 'message to ponysay')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
class ponysay():
|
|
|
|
def __init__(self, args):
|
2012-08-18 14:26:53 +02:00
|
|
|
if args.list: self.list()
|
|
|
|
else: self.print_pony(args)
|
2012-08-18 13:15:12 +02:00
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
Lists the available ponies
|
|
|
|
'''
|
|
|
|
def list(self):
|
|
|
|
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(" ")
|
|
|
|
termsize = [int(item) for item in termsize]
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
for ponydir in ponydirs: # Loop ponydirs
|
2012-08-18 14:26:53 +02:00
|
|
|
print('\033[1mponyfiles located in ' + ponydir + '\033[21m')
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
ponies = os.listdir(ponydir)
|
2012-08-18 14:26:53 +02:00
|
|
|
ponies = [item[:-5] for item in ponies] # remove .pony from file name
|
2012-08-18 13:15:12 +02:00
|
|
|
ponies.sort()
|
2012-08-18 14:26:53 +02:00
|
|
|
|
2012-08-18 13:15:12 +02:00
|
|
|
width = len(max(ponies, key = len)) + 2 # Get the longest ponyfilename lenght + 2 spaces
|
|
|
|
|
|
|
|
x = 0
|
|
|
|
for pony in ponies:
|
2012-08-18 14:26:53 +02:00
|
|
|
print(pony + (" " * (width - len(pony))), end="") # Print ponyfilename
|
2012-08-18 14:29:57 +02:00
|
|
|
x += width
|
2012-08-18 13:15:12 +02:00
|
|
|
if x > (termsize[1] - width): # If too wide, make new line
|
2012-08-18 14:26:53 +02:00
|
|
|
print();
|
2012-08-18 13:15:12 +02:00
|
|
|
x = 0
|
2012-08-18 14:26:53 +02:00
|
|
|
|
|
|
|
print("\n");
|
2012-08-18 13:15:12 +02:00
|
|
|
|
|
|
|
def print_pony(self, args):
|
|
|
|
if args.message == None:
|
|
|
|
msg = sys.stdin.read().strip()
|
|
|
|
else:
|
|
|
|
msg = args.message
|
|
|
|
|
|
|
|
|
|
|
|
if args.pony == None:
|
|
|
|
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
|
|
|
|
|
|
|
|
else:
|
|
|
|
for ponydir in ponydirs:
|
|
|
|
if os.path.isfile(ponydir + args.pony[0]):
|
|
|
|
pony = ponydir + args.pony[0]
|
|
|
|
|
|
|
|
os.system('cowsay -f ' + pony + ' "' + msg + '"')
|
|
|
|
|
|
|
|
|
2012-08-18 14:26:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
Start the program from ponysay.__init__ if this is the executed file
|
|
|
|
'''
|
2012-08-18 13:15:12 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
ponysay(args)
|