no need for curses, the use of it also caused some problem in the terminal when the program exited + pony dirs that do no exist are ignored + -l does not print .pony and is fixed

This commit is contained in:
Mattias Andrée 2012-08-18 14:26:53 +02:00
parent 93c58dd5c3
commit ab986563ec

View file

@ -3,25 +3,40 @@
''' '''
ponysay.py - POC of ponysay in python ponysay.py - POC of ponysay in python
Copyright (C) 2012 Elis "etu" Axelsson Copyright (C) 2012 Elis "etu" Axelsson, Mattias "maandree" Andrée
License: WTFPL License: WTFPL
''' '''
import argparse import argparse
import os import os
import curses
import sys import sys
import random import random
from subprocess import Popen, PIPE
version = 1.98 '''
if os.environ['TERM'] == 'linux': The version of ponysay
ponydirs = ['/usr/share/ponysay/ttyponies/', os.environ['HOME'] + '/.local/share/ponysay/ttyponies/'] '''
else: version = "2.0-alpha"
ponydirs = ['/usr/share/ponysay/ponies/', os.environ['HOME'] + '/.local/share/ponysay/ponies/']
'''
The directory where ponysay is installed, this is modified when building with make
'''
installdir = '/usr'
'''
The directories where pony files are stored, ttyponies/ are used if the terminal is Linux VT (also known as TTY)
'''
ponydirs = []
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/' ]
for ponydir in _ponydirs:
if os.path.isdir(ponydir):
ponydirs.append(ponydir)
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' % (__file__, version)) parser.add_argument('-v', '--version', action = 'version', version='%s %s' % (__file__, version))
@ -34,39 +49,36 @@ args = parser.parse_args()
class ponysay(): class ponysay():
def __init__(self, args): def __init__(self, args):
if args.list: if args.list: self.list()
self.list() else: self.print_pony(args)
else:
self.print_pony(args)
def list(self): # List ponies
screen = curses.initscr() '''
termsize = screen.getmaxyx() Lists the available ponies
'''
y = 0 def list(self):
termsize = Popen(['stty', 'size'], stdout=PIPE).communicate()[0].decode('utf8', 'replace')[:-1].split(" ")
termsize = [int(item) for item in termsize]
for ponydir in ponydirs: # Loop ponydirs for ponydir in ponydirs: # Loop ponydirs
screen.addstr(y, 0, 'ponyfiles located in ' + ponydir, curses.A_BOLD) # Print header print('\033[1mponyfiles located in ' + ponydir + '\033[21m')
y = y + 1
ponies = os.listdir(ponydir) ponies = os.listdir(ponydir)
ponies = [item[:-5] for item in ponies] # remove .pony from file name
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
x = 0 x = 0
for pony in ponies: for pony in ponies:
screen.addstr(y, x, pony) # Print ponyfilename print(pony + (" " * (width - len(pony))), end="") # Print ponyfilename
x = x + width
x = x + width # Add width
if x > (termsize[1] - width): # If too wide, make new line if x > (termsize[1] - width): # If too wide, make new line
print();
x = 0 x = 0
y = y + 1
print("\n");
y = y + 2 # Increase y before the loop restart to make space for the next header
screen.addstr(y, 0, '') # Make newline at end of output
screen.refresh()
def print_pony(self, args): def print_pony(self, args):
if args.message == None: if args.message == None:
msg = sys.stdin.read().strip() msg = sys.stdin.read().strip()
@ -90,6 +102,10 @@ class ponysay():
os.system('cowsay -f ' + pony + ' "' + msg + '"') os.system('cowsay -f ' + pony + ' "' + msg + '"')
'''
Start the program from ponysay.__init__ if this is the executed file
'''
if __name__ == '__main__': if __name__ == '__main__':
ponysay(args) ponysay(args)