Ooops, urwid-unicode.patch and urwid-unicode-tab2.patch were buggy. I fixed
the bug and also improve it.
New version (urwid-unicode-v7.patch), better (no more space/tab conflict) and
*faster*!
New benchmark
without the patch: 7.8 sec
with the patch: 2.7 sec
Sorry for the pollution (three emails)...
Finally, the idea is to minimize use of calc_width() => keep widths list in
Canvas so CanvasCombine can reuse it.
Haypo
diff -ur urwid-0.9.7.1/urwid/canvas.py urwid-unicode/urwid/canvas.py
--- urwid-0.9.7.1/urwid/canvas.py 2006-10-09 17:18:35.000000000 +0200
+++ urwid-unicode/urwid/canvas.py 2006-12-31 03:13:06.000000000 +0100
@@ -22,6 +22,7 @@
from __future__ import nested_scopes
from util import *
from escape import *
+from util import _byte_encoding
try: True # old python?
except: False, True = 0, 1
@@ -35,7 +36,7 @@
class for storing rendered text and attributes
"""
def __init__(self,text = None,attr = None, cs = None,
- cursor = None, maxcol=None):
+ cursor = None, maxcol=None, widths=None):
"""
text -- list of strings, one for each line
attr -- list of run length encoded attributes for text
@@ -45,11 +46,11 @@
"""
if text == None:
text = []
- widths = []
- for t in text:
- if type(t) != type(""):
- raise CanvasError("Canvas text must be plain strings encoded in the screen's encoding", `text`)
- widths.append( calc_width( t, 0, len(t)) )
+ if widths is None:
+ widths = []
+ for t in text:
+ t = unicode(t, _byte_encoding)
+ widths.append( calc_width( t, 0, len(t)) )
if maxcol is None:
if widths:
@@ -69,7 +70,8 @@
if w > maxcol:
raise CanvasError("Canvas text is wider than the maxcol specified \n%s\n%s\n%s"%(`maxcol`,`widths`,`text`))
if w < maxcol:
- text[i] = text[i] + " "*(maxcol-w)
+ text[i] = text[i].ljust(maxcol)
+ widths[i] = maxcol
a_gap = len(text[i]) - rle_len( attr[i] )
if a_gap < 0:
raise CanvasError("Attribute extends beyond text \n%s\n%s" % (`text[i]`,`attr[i]`) )
@@ -87,6 +89,7 @@
self.cursor = cursor
self.text = text
self.maxcol = maxcol
+ self.widths = widths
def rows(self):
"""Return the number of rows in this canvas."""
@@ -276,11 +279,13 @@
t = []
a = []
c = []
+ w = []
rows = 0
cols = 0
cursor = None
for r in l:
t += r.text
+ w += r.widths
a += r.attr
c += r.cs
cols = max(cols, r.cols())
@@ -288,7 +293,7 @@
x,y = r.cursor
cursor = x, y+rows
rows = len( t )
- d = Canvas(t, a, c, cursor, cols )
+ d = Canvas(t, a, c, cursor, cols, widths=w)
return d
Seulement dans urwid-unicode/urwid: canvas.py~
_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid