Created
November 19, 2012 19:56
-
-
Save wardi/4113436 to your computer and use it in GitHub Desktop.
An incomplete toy RTL text layout class for Urwid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urwid | |
from urwid.util import calc_width | |
class RTLTextLayout(urwid.TextLayout): | |
""" | |
A toy text layout that arranges all characters from | |
right to left. | |
Only works with unicode strings and narrow characters | |
Currently fails if text is too long for the line | |
(clipping not yet implemented) | |
""" | |
def layout(self, text, width, align, wrap): | |
assert isinstance(text, unicode), 'bytes not handled yet' | |
p = 0 | |
b = [] | |
while p <= len(text): | |
n_cr = text.find(u'\n', p) | |
if n_cr == -1: | |
n_cr = len(text) | |
sc = calc_width(text, p, n_cr) | |
#FIXME this doesn't actually check what it's claiming: | |
assert sc == n_cr - p, 'wide/combining characters not handled yet' | |
assert sc <= width, 'sorry, clipping not yet implementd' | |
l = [(width - sc, None)] | |
l += [(1, x, x+1) for x in reversed(range(p, n_cr))] | |
b.append(l) | |
p = n_cr+1 | |
return b | |
def supports_align_mode(self, align): | |
return align == 'right' | |
def supports_wrap_mode(self, wrap): | |
return wrap == 'clip' | |
rtl_layout = RTLTextLayout() | |
text = urwid.Text(u"hello\nworld\n", align='right', wrap='clip', layout=rtl_layout) | |
edit = urwid.Edit(u"caption: ", u"value", align='right', wrap='clip', layout=rtl_layout) | |
urwid.MainLoop(urwid.Filler(urwid.Pile([text, edit]))).run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment