aboutsummaryrefslogtreecommitdiff
path: root/ponysay
blob: 65a0339e5c79c19ce6a992a84dab9e4df9eb5a57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3

import os, sys, random
from os.path import dirname, realpath, exists
import ponysay
import argparse, textwrap

if __name__ == '__main__':
	termwidth = 80
	try:
		termwidth = os.get_terminal_size()[0]
	except:
		pass

	parser = argparse.ArgumentParser(description='Cowsay with ponies')
	parser.add_argument('-p', '--pony', type=str, default='random', help='The name of the pony to be used. Use "-p list" to list all ponies, "-p random" (default) to use a random pony.')
	parser.add_argument('-q', '--quote', action='store_true', help='Use a random quote of the pony being displayed as text')
	parser.add_argument('-c', '--center', action='store_true', help='Use a random quote of the pony being displayed as text')
	parser.add_argument('-C', '--center-text', action='store_true', help='Center the text in the bubble')
	parser.add_argument('-w', '--width', type=int, default=termwidth, help='Terminal width. Use 0 for unlimited width. Default: autodetect')
	parser.add_argument('-b', '--balloon', type=str, default='cowsay', help='Balloon style to use. Use "-b list" to list available styles.')
	parser.add_argument('text', type=str, nargs='*', help='The text to be placed in the speech bubble')
	args = parser.parse_args()
  
	think = sys.argv[0].endswith('think')
	if args.pony == "list":
		print('\n'.join(sorted(ponysay.list_ponies() if not args.quote else ponysay.list_ponies_with_quotes())))
		sys.exit()
	if args.balloon == 'list':
		print('\n'.join([ s.replace('.think', '') for s in ponysay.balloonstyles.keys() if s.endswith('.think') == think ]))
		sys.exit()
	pony = args.pony
	if pony == "random":
		pony = random.choice(ponysay.list_ponies() if not args.quote else ponysay.list_ponies_with_quotes())
	text = ' '.join(args.text) or None
	if text == '-':
		text = '\n'.join(sys.stdin.readlines())
	if args.quote:
		text = ponysay.random_quote(pony)

	balloonstyle = None
	if think:
		balloonstyle = ponysay.balloonstyles[args.balloon+'.think']
	else:
		balloonstyle = ponysay.balloonstyles[args.balloon]

	print(ponysay.render_pony(pony, text,
							  balloonstyle=balloonstyle,
							  width=args.width or sys.maxint,
							  center=args.center,
							  centertext=args.center_text))