From ab986563ec338e4031047e9da0b9fe020e24a5dc Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 18 Aug 2012 14:26:53 +0200 Subject: 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 --- ponysay.py | 72 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 28 deletions(-) (limited to 'ponysay.py') diff --git a/ponysay.py b/ponysay.py index cd5b184..29c969e 100755 --- a/ponysay.py +++ b/ponysay.py @@ -3,25 +3,40 @@ ''' 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 ''' import argparse import os -import curses import sys import random +from subprocess import Popen, PIPE -version = 1.98 -if os.environ['TERM'] == 'linux': - ponydirs = ['/usr/share/ponysay/ttyponies/', os.environ['HOME'] + '/.local/share/ponysay/ttyponies/'] -else: - ponydirs = ['/usr/share/ponysay/ponies/', os.environ['HOME'] + '/.local/share/ponysay/ponies/'] +''' +The version of ponysay +''' +version = "2.0-alpha" + + +''' +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.add_argument('-v', '--version', action = 'version', version='%s %s' % (__file__, version)) @@ -34,39 +49,36 @@ args = parser.parse_args() class ponysay(): def __init__(self, args): - if args.list: - self.list() - else: - self.print_pony(args) + if args.list: self.list() + else: self.print_pony(args) - def list(self): # List ponies - screen = curses.initscr() - termsize = screen.getmaxyx() - - y = 0 + + ''' + 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] for ponydir in ponydirs: # Loop ponydirs - screen.addstr(y, 0, 'ponyfiles located in ' + ponydir, curses.A_BOLD) # Print header - y = y + 1 + print('\033[1mponyfiles located in ' + ponydir + '\033[21m') ponies = os.listdir(ponydir) + ponies = [item[:-5] for item in ponies] # remove .pony from file name ponies.sort() + width = len(max(ponies, key = len)) + 2 # Get the longest ponyfilename lenght + 2 spaces x = 0 for pony in ponies: - screen.addstr(y, x, pony) # Print ponyfilename - - x = x + width # Add width + print(pony + (" " * (width - len(pony))), end="") # Print ponyfilename + x = x + width if x > (termsize[1] - width): # If too wide, make new line + print(); 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): if args.message == None: msg = sys.stdin.read().strip() @@ -90,6 +102,10 @@ class ponysay(): os.system('cowsay -f ' + pony + ' "' + msg + '"') + + +''' +Start the program from ponysay.__init__ if this is the executed file +''' if __name__ == '__main__': ponysay(args) - -- cgit