mirror of
https://github.com/erkin/ponysay.git
synced 2024-11-22 04:27:58 +01:00
ponysay-tool can get the print string for a pony and the ponies dimensions
This commit is contained in:
parent
0a79a833d4
commit
e73c8518d1
3 changed files with 65 additions and 14 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -42,3 +42,8 @@
|
||||||
/ponysay.pgs
|
/ponysay.pgs
|
||||||
/ponysay.vrs
|
/ponysay.vrs
|
||||||
|
|
||||||
|
|
||||||
|
## Python cache files
|
||||||
|
|
||||||
|
__pycache__/
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,7 @@ http://sam.zoy.org/wtfpl/COPYING for more details.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import sys
|
import sys
|
||||||
import random
|
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
|
|
||||||
from ponysay import *
|
from ponysay import *
|
||||||
|
@ -107,7 +105,7 @@ class PonysayTool():
|
||||||
meta = data[1:][:sep]
|
meta = data[1:][:sep]
|
||||||
image = data[sep + 1:]
|
image = data[sep + 1:]
|
||||||
|
|
||||||
class PhonyArgsParser:
|
class PhonyArgParser:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.argcount = 3
|
self.argcount = 3
|
||||||
self.message = ponyfile
|
self.message = ponyfile
|
||||||
|
@ -115,8 +113,54 @@ class PonysayTool():
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
return [ponyfile] if key == '-f' else None
|
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 = 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))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
22
ponysay.py
22
ponysay.py
|
@ -1658,7 +1658,7 @@ class Backend():
|
||||||
c = line[i]
|
c = line[i]
|
||||||
i += 1
|
i += 1
|
||||||
if c == '\033':
|
if c == '\033':
|
||||||
colour = self.__getcolour(line, i - 1)
|
colour = Backend.getcolour(line, i - 1)
|
||||||
i += len(colour) - 1
|
i += len(colour) - 1
|
||||||
buf += colour
|
buf += colour
|
||||||
elif c == '\t':
|
elif c == '\t':
|
||||||
|
@ -1695,7 +1695,7 @@ class Backend():
|
||||||
c = line[i]
|
c = line[i]
|
||||||
i += 1
|
i += 1
|
||||||
if c == '\033':
|
if c == '\033':
|
||||||
colour = self.__getcolour(line, i - 1)
|
colour = Backend.getcolour(line, i - 1)
|
||||||
i += len(colour) - 1
|
i += len(colour) - 1
|
||||||
self.output += colour
|
self.output += colour
|
||||||
else:
|
else:
|
||||||
|
@ -1779,7 +1779,7 @@ class Backend():
|
||||||
balloonLines = balloon
|
balloonLines = balloon
|
||||||
balloonLine = 0
|
balloonLine = 0
|
||||||
balloonIndent = indent
|
balloonIndent = indent
|
||||||
indent += self.__len(balloonLines[0])
|
indent += Backend.len(balloonLines[0])
|
||||||
balloonLines[0] = None
|
balloonLines[0] = None
|
||||||
dollar = None
|
dollar = None
|
||||||
else:
|
else:
|
||||||
|
@ -1790,7 +1790,7 @@ class Backend():
|
||||||
i += 1
|
i += 1
|
||||||
dollar += c
|
dollar += c
|
||||||
elif c == '\033':
|
elif c == '\033':
|
||||||
colour = self.__getcolour(self.pony, i - 1)
|
colour = Backend.getcolour(self.pony, i - 1)
|
||||||
for b in colour:
|
for b in colour:
|
||||||
self.output += b + colourstack.feed(b);
|
self.output += b + colourstack.feed(b);
|
||||||
i += len(colour) - 1
|
i += len(colour) - 1
|
||||||
|
@ -1806,7 +1806,7 @@ class Backend():
|
||||||
else:
|
else:
|
||||||
if (balloonLines is not None) and (balloonLines[balloonLine] is not None) and (balloonIndent == indent):
|
if (balloonLines is not None) and (balloonLines[balloonLine] is not None) and (balloonIndent == indent):
|
||||||
data = balloonLines[balloonLine]
|
data = balloonLines[balloonLine]
|
||||||
datalen = self.__len(data)
|
datalen = Backend.len(data)
|
||||||
skip += datalen
|
skip += datalen
|
||||||
nonskip += datalen
|
nonskip += datalen
|
||||||
data = data.replace('$', '$$')
|
data = data.replace('$', '$$')
|
||||||
|
@ -1840,7 +1840,8 @@ class Backend():
|
||||||
@param offset:int The offset at where to start reading, a escape must begin here
|
@param offset:int The offset at where to start reading, a escape must begin here
|
||||||
@return :str The escape sequence
|
@return :str The escape sequence
|
||||||
'''
|
'''
|
||||||
def __getcolour(self, input, offset):
|
@staticmethod
|
||||||
|
def getcolour(input, offset):
|
||||||
(i, n) = (offset, len(input))
|
(i, n) = (offset, len(input))
|
||||||
rc = input[i]
|
rc = input[i]
|
||||||
i += 1
|
i += 1
|
||||||
|
@ -1878,12 +1879,13 @@ class Backend():
|
||||||
@param input:str The input buffer
|
@param input:str The input buffer
|
||||||
@return :int The number of visible characters
|
@return :int The number of visible characters
|
||||||
'''
|
'''
|
||||||
def __len(self, input):
|
@staticmethod
|
||||||
|
def len(input):
|
||||||
(rc, i, n) = (0, 0, len(input))
|
(rc, i, n) = (0, 0, len(input))
|
||||||
while i < n:
|
while i < n:
|
||||||
c = input[i]
|
c = input[i]
|
||||||
if c == '\033':
|
if c == '\033':
|
||||||
i += len(self.__getcolour(input, i))
|
i += len(Backend.getcolour(input, i))
|
||||||
else:
|
else:
|
||||||
i += 1
|
i += 1
|
||||||
if not UCS.isCombining(c):
|
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
|
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:
|
if i < n:
|
||||||
d = line[i]
|
d = line[i]
|
||||||
i += 1
|
i += 1
|
||||||
if d == '\033': # TODO this should use self.__getcolour()
|
if d == '\033': # TODO this should use Backend.getcolour()
|
||||||
## Invisible stuff
|
## Invisible stuff
|
||||||
b[bi] = d
|
b[bi] = d
|
||||||
bi += 1
|
bi += 1
|
||||||
|
|
Loading…
Reference in a new issue