Permalink
Browse files

word wrapping bug fixed + word wrapper supports soft hythens and non-…

…breaking space + word wrapper colour inserted hyphens red
  • Loading branch information...
1 parent 1ca6079 commit e6bdf2e5ba92b4f01178a1d1bd1d601da38375da @maandree maandree committed Oct 4, 2012
Showing with 67 additions and 19 deletions.
  1. +10 −0 CHANGELOG
  2. +13 −1 manuals/ponysay.texinfo
  3. +44 −18 ponysay.py
View
10 CHANGELOG
@@ -1,4 +1,14 @@
+Version 2.8
+
+ Support for explicit hypthenation using soft hyphens had been added to the word wrapper.
+
+ Support for explicit non-word wrapping using non-breaking space had been added to the word wrapper.
+
+ The word wrapper colours the inserted hyphens in red.
+
+
Version 2.7
+
New ponies: basil, cloudkicker, cerberus, cow, derpysad, flowertrio, frederickhorseshoepin,
horsemd, jeffletroski, jesuspezuna, joe, joetuxedo, manticore, meadowsong,
meliot, pinkiegummydisguise, seaswirl, theodoredonaldkerabatsos, turf,
View
14 manuals/ponysay.texinfo
@@ -8,7 +8,7 @@
@documentlanguage en
@finalout
@c %**end of header
-@set VERSION 2.7
+@set VERSION 2.8
@defindex op
@synindex op vr
@@ -1848,6 +1848,18 @@ sequences.
@cindex versions
@cindex previous releases
+
+@heading Version 2.8
+@itemize @bullet
+@item
+Support for explicit hypthenation using soft hyphens had been added to the word wrapper.
+@item
+Support for explicit non-word wrapping using non-breaking space had been added to the word wrapper.
+@item
+The word wrapper colours the inserted hyphens in red.
+@end itemize
+
+
@heading Version 2.7
@itemize @bullet
@item
View
62 ponysay.py
@@ -1473,6 +1473,8 @@ def __getballoon(self, width, height, left):
Wraps the message
'''
def __wrapMessage(self, message, wrap):
+ AUTO_PUSH = '\033[01010~'
+ AUTO_POP = '\033[10101~'
lines = message.split('\n')
buf = ''
for line in lines:
@@ -1484,10 +1486,11 @@ def __wrapMessage(self, message, wrap):
(i, n) = (0, len(line))
while i <= n:
d = None
- if i != n:
+ if i < n:
d = line[i]
i += 1
if d == '\033': # TODO this should use self.__getcolour()
+ ## Invisible stuff
b[bi] = d
bi += 1
b[bi] = line[i]
@@ -1513,34 +1516,54 @@ def __wrapMessage(self, message, wrap):
bi += 1
i += 1
elif (d is not None) and (d != ' '):
+ ## Fetch word
if indent == -1:
indent = i - 1
for j in range(0, indent):
if line[j] == ' ':
indentc += 1
b[bi] = d
bi += 1
- if not UCS.isCombining(d):
+ if (not UCS.isCombining(d)) and (d != '­'):
cols += 1
map[cols] = bi
else:
+ ## Wrap?
mm = 0
- while (w > 8) and (cols > w + 3):
- mm += w - 1
- m = map[mm]
- for bb in b[:m]:
+ bisub = 0
+ iwrap = wrap - (0 if indent == 1 else indentc)
+
+ while ((w > 8) and (cols > w + 5)) or (cols > iwrap): # TODO make configurable
+ ## wrap
+ x = w;
+ nbsp = b[map[mm + x]] == ' '
+ m = map[mm + x]
+
+ if ('­' in b[bisub : m]) and not nbsp:
+ hyphen = m - 1
+ while b[hyphen] != '­':
+ hyphen -= 1
+ while map[mm + x] > hyphen: ## Only looking backward, if foreward is required the word is probabily not hythenated correctly
+ x -= 1
+ x += 1
+ m = map[mm + x]
+
+ mm += x - (0 if nbsp else 1) ## − 1 so we have space for a hythen
+
+ for bb in b[bisub : m]:
buf += bb
- buf += '-\n'
- cols -= w - 1
- m += w -1
- bi -= m
- bb = b[m:]
- for j in range(0, bi):
- b[j] = bb[j]
- w = wrap
+ buf += '\n' if nbsp else '\0\n'
+ cols -= x - (0 if nbsp else 1)
+ bisub = m
+
+ w = iwrap
if indent != -1:
buf += line[:indent]
- w -= indentc
+
+ for j in range(bisub, bi):
+ b[j - bisub] = b[j]
+ bi -= bisub
+
if cols > w:
buf += '\n'
w = wrap
@@ -1552,7 +1575,7 @@ def __wrapMessage(self, message, wrap):
w -= cols
cols = 0
bi = 0
- if d == -1:
+ if d is None:
i += 1
else:
if w > 0:
@@ -1564,9 +1587,12 @@ def __wrapMessage(self, message, wrap):
if indent != -1:
buf + line[:indent]
w -= indentc
-
buf += '\n'
- return '\n'.join(line.rstrip() for line in buf[:-1].split('\n'))
+
+ rc = '\n'.join(line.rstrip() for line in buf[:-1].split('\n'));
+ rc = rc.replace('­', ''); # remove soft hyphens
+ rc = rc.replace('\0', '%s%s%s' % (AUTO_PUSH, '\033[31m-', AUTO_POP)) # TODO make configurable
+ return rc
'''

0 comments on commit e6bdf2e

Please sign in to comment.