mirror of
https://github.com/erkin/ponysay.git
synced 2024-11-26 06:07:59 +01:00
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:
parent
93c58dd5c3
commit
ab986563ec
1 changed files with 44 additions and 28 deletions
72
ponysay.py
72
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue