ponysay-tool can get the print string for a pony and the ponies dimensions

This commit is contained in:
Mattias Andrée 2012-10-30 02:17:00 +01:00
parent 0a79a833d4
commit e73c8518d1
3 changed files with 65 additions and 14 deletions

5
.gitignore vendored
View file

@ -42,3 +42,8 @@
/ponysay.pgs
/ponysay.vrs
## Python cache files
__pycache__/

View file

@ -13,9 +13,7 @@ http://sam.zoy.org/wtfpl/COPYING for more details.
'''
import os
import shutil
import sys
import random
from subprocess import Popen, PIPE
from ponysay import *
@ -107,7 +105,7 @@ class PonysayTool():
meta = data[1:][:sep]
image = data[sep + 1:]
class PhonyArgsParser:
class PhonyArgParser:
def __init__(self):
self.argcount = 3
self.message = ponyfile
@ -115,8 +113,54 @@ class PonysayTool():
def __getitem__(self, key):
return [ponyfile] if key == '-f' else None
data = {}
comment = []
for line in data:
if ': ' in line:
key = line.replace('\t', ' ')
key = key[:key.find(': ')]
key = key.strip(' ')
if key == key.upper():
value = line.replace('\t', ' ')
value = keyvalue[key.find(': ') + 2:]
data[key] = value.strip(' ')
else:
comment.append(line)
cut = 0
while (len(comment) > cut) and (len(comment[cut]) == 0):
cut += 1
comment = [''] + comment[cut:]
stdout = sys.stdout
class StringInputStream:
def __init__(self):
self.buf = ''
class Buffer:
def __init__(self, parent):
self.parent = parent
def write(self, data):
self.parent.buf += data.decode('utf8', 'replace')
def flush(self):
pass
self.buffer = Buffer(self)
def flush(self):
pass
def isatty(self):
return True
sys.stdout = StringInputStream()
ponysay = Ponysay()
ponysay.run(PhonyArgsParser())
ponysay.run(PhonyArgParser())
printpony = sys.stdout.buf[:-1].split('\n')
sys.stdout = stdout
ponyheight = len(printpony)
ponywidth = Backend.len(max(printpony, key = Backend.len))
print('\n'.join(printpony))
print(str(ponyheight) + '×' + str(ponywidth))

View file

@ -1658,7 +1658,7 @@ class Backend():
c = line[i]
i += 1
if c == '\033':
colour = self.__getcolour(line, i - 1)
colour = Backend.getcolour(line, i - 1)
i += len(colour) - 1
buf += colour
elif c == '\t':
@ -1695,7 +1695,7 @@ class Backend():
c = line[i]
i += 1
if c == '\033':
colour = self.__getcolour(line, i - 1)
colour = Backend.getcolour(line, i - 1)
i += len(colour) - 1
self.output += colour
else:
@ -1779,7 +1779,7 @@ class Backend():
balloonLines = balloon
balloonLine = 0
balloonIndent = indent
indent += self.__len(balloonLines[0])
indent += Backend.len(balloonLines[0])
balloonLines[0] = None
dollar = None
else:
@ -1790,7 +1790,7 @@ class Backend():
i += 1
dollar += c
elif c == '\033':
colour = self.__getcolour(self.pony, i - 1)
colour = Backend.getcolour(self.pony, i - 1)
for b in colour:
self.output += b + colourstack.feed(b);
i += len(colour) - 1
@ -1806,7 +1806,7 @@ class Backend():
else:
if (balloonLines is not None) and (balloonLines[balloonLine] is not None) and (balloonIndent == indent):
data = balloonLines[balloonLine]
datalen = self.__len(data)
datalen = Backend.len(data)
skip += datalen
nonskip += datalen
data = data.replace('$', '$$')
@ -1840,7 +1840,8 @@ class Backend():
@param offset:int The offset at where to start reading, a escape must begin here
@return :str The escape sequence
'''
def __getcolour(self, input, offset):
@staticmethod
def getcolour(input, offset):
(i, n) = (offset, len(input))
rc = input[i]
i += 1
@ -1878,12 +1879,13 @@ class Backend():
@param input:str The input buffer
@return :int The number of visible characters
'''
def __len(self, input):
@staticmethod
def len(input):
(rc, i, n) = (0, 0, len(input))
while i < n:
c = input[i]
if c == '\033':
i += len(self.__getcolour(input, i))
i += len(Backend.getcolour(input, i))
else:
i += 1
if not UCS.isCombining(c):
@ -1912,7 +1914,7 @@ class Backend():
msg = msg.replace('\n', '\033[0m%s\n' % (self.ballooncolour)) + '\033[0m' + self.ballooncolour
return self.balloon.get(width, height, msg.split('\n'), self.__len);
return self.balloon.get(width, height, msg.split('\n'), Backend.len);
'''
@ -1952,7 +1954,7 @@ class Backend():
if i < n:
d = line[i]
i += 1
if d == '\033': # TODO this should use self.__getcolour()
if d == '\033': # TODO this should use Backend.getcolour()
## Invisible stuff
b[bi] = d
bi += 1