arbitrary balloon positioning

This commit is contained in:
Mattias Andrée 2012-08-21 01:46:24 +02:00
parent 0cbb8574c9
commit 3324c3e51c

87
ponysay
View file

@ -789,8 +789,9 @@ class Backend():
indent = 0 indent = 0
dollar = None dollar = None
balloonLines = None
(i, n) = (0, len(self.pony)) (i, n, lineindex, skip, nonskip) = (0, len(self.pony), 0, 0, 0)
while i < n: while i < n:
c = self.pony[i] c = self.pony[i]
if c == '\t': if c == '\t':
@ -806,18 +807,18 @@ class Backend():
value = dollar[find('=') + 1:] value = dollar[find('=') + 1:]
variables[name] = value variables[name] = value
elif (len(dollar) < 7) or not (dollar[:7] == 'balloon'): elif (len(dollar) < 7) or not (dollar[:7] == 'balloon'):
if dollar in ('\\', '/'): data = variables[dollar].replace('$', '$$')
i += len(variables[dollar]) - 1 if data == '$$': # if not handled specially we will get an infinity loop
lines = variables[dollar].split('\n') if (skip == 0) or (nonskip > 0):
firstvarline = True if nonskip > 0:
for line in lines: nonskip -= 1
if firstvarline: self.output += '$'
firstvarline = False indent += 1
else: else:
indent = 0 skip -= 1
self.output = '\n' else:
self.output += line n += len(data)
indent += len(line) self.pony = self.pony[:i] + data + self.pony[i:]
else: else:
(w, h) = (0, 0) (w, h) = (0, 0)
props = dollar[7:] props = dollar[7:]
@ -829,12 +830,19 @@ class Backend():
else: else:
w = int(props) w = int(props)
balloon = self.__getballoon(w, h, indent).split('\n') balloon = self.__getballoon(w, h, indent).split('\n')
balloonpre = '\n' + (' ' * indent)
self.output += balloon[0] self.output += balloon[0]
for line in balloon[1:]: if lineindex == 0:
self.output += balloonpre; balloonpre = '\n' + (' ' * indent)
self.output += line; for line in balloon[1:]:
indent = 0 self.output += balloonpre;
self.output += line;
indent = 0
elif len(balloon) > 1:
balloonLines = balloon
balloonLine = 0
balloonIndent = indent
indent += self.__len(balloonLines[0])
balloonLines[0] = None
dollar = None dollar = None
else: else:
dollar = '' dollar = ''
@ -850,9 +858,35 @@ class Backend():
elif c == '\n': elif c == '\n':
self.output += c self.output += c
indent = 0 indent = 0
(skip, nonskip) = (0, 0)
lineindex += 1
if balloonLines is not None:
balloonLine += 1
if balloonLine == len(balloonLines):
balloonLines = None
else: else:
self.output += c if (balloonLines is not None) and (balloonLines[balloonLine] is not None) and (balloonIndent == indent):
indent += 1 data = balloonLines[balloonLine]
datalen = self.__len(data)
skip += datalen
nonskip += datalen
data = data.replace("$", '$$')
n += len(data)
self.pony = self.pony[:i] + data + self.pony[i:]
balloonLines[balloonLine] = None
else:
if (skip == 0) or (nonskip > 0):
if nonskip > 0:
nonskip -= 1
self.output += c
indent += 1
else:
skip -= 1
if balloonLines is not None:
for line in balloonLines[balloonLine:]:
self.output += ' ' * (balloonIndent - indent) + line + '\n'
indent = 0
''' '''
@ -890,6 +924,21 @@ class Backend():
return rc return rc
'''
Calculates the number of visible characters in a text
'''
def __len(self, input):
(rc, i, n) = (0, 0, len(input))
while i < n:
c = input[i]
if c == '\033':
i += len(self.__getcolour(input, i))
else:
i += 1
rc += 1
return rc
''' '''
Generates a balloon with the message Generates a balloon with the message
''' '''