本文整理汇总了Python中pyreadline.unicode_helper.ensure_unicode函数的典型用法代码示例。如果您正苦于以下问题:Python ensure_unicode函数的具体用法?Python ensure_unicode怎么用?Python ensure_unicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ensure_unicode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_plain
def write_plain(self, text, attr=None):
u"""write text at current cursor position."""
text = ensure_unicode(text)
log(u'write("%s", %s)' % (text, attr))
if attr is None:
attr = self.attr
junk = DWORD(0)
self.SetConsoleTextAttribute(self.hout, attr)
for short_chunk in split_block(chunk):
self.WriteConsoleW(self.hout, ensure_unicode(short_chunk), len(short_chunk), byref(junk), None)
return len(text)
示例2: _get_completions
def _get_completions(self):
"""Return a list of possible completions for the string ending at the point.
Also set begidx and endidx in the process."""
completions = []
self.begidx = self.l_buffer.point
self.endidx = self.l_buffer.point
buf=self.l_buffer.line_buffer
if self.completer:
# get the string to complete
while self.begidx > 0:
self.begidx -= 1
if buf[self.begidx] in self.completer_delims:
self.begidx += 1
break
text = ensure_str(''.join(buf[self.begidx:self.endidx]))
log('complete text="%s"' % ensure_unicode(text))
i = 0
while 1:
try:
r = self.completer(ensure_unicode(text), i)
except IndexError:
break
except TypeError:
break
i += 1
if r is None:
break
elif r and r not in completions:
completions.append(r)
else:
pass
log('text completions=<%s>' % list(map(ensure_unicode, completions)))
if (self.complete_filesystem == "on") and not completions:
# get the filename to complete
while self.begidx > 0:
self.begidx -= 1
if buf[self.begidx] in ' \t\n':
self.begidx += 1
break
text = ensure_str(''.join(buf[self.begidx:self.endidx]))
log('file complete text="%s"' % ensure_unicode(text))
completions = list(map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'.encode('ascii'))))
if self.mark_directories == 'on':
mc = []
for f in completions:
if os.path.isdir(f):
mc.append(f + os.sep)
else:
mc.append(f)
completions = mc
log('fnames=<%s>' % list(map(ensure_unicode, completions)))
return completions
示例3: _get_completions
def _get_completions(self):
"""Return a list of possible completions for the string ending at the point.
Also set begidx and endidx in the process."""
completions = []
self.begidx = self.l_buffer.point
self.endidx = self.l_buffer.point
buf=self.l_buffer.line_buffer
if self.completer:
# get the string to complete
while self.begidx > 0:
self.begidx -= 1
if buf[self.begidx] in self.completer_delims:
self.begidx += 1
break
text = ensure_str(u''.join(buf[self.begidx:self.endidx]))
log(u'complete text="%s"' % ensure_unicode(text))
i = 0
while 1:
try:
r = ensure_unicode(self.completer(text, i))
except:
break
i += 1
if r and r not in completions:
completions.append(r)
else:
break
log(u'text completions=<%s>' % map(ensure_unicode, completions))
if not completions:
# get the filename to complete
while self.begidx > 0:
self.begidx -= 1
if buf[self.begidx] in u' \t\n':
self.begidx += 1
break
text = ensure_str(u''.join(buf[self.begidx:self.endidx]))
log(u'file complete text="%s"' % ensure_unicode(text))
completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'))
if self.mark_directories == u'on':
mc = []
for f in completions:
if os.path.isdir(f):
mc.append(f + os.sep)
else:
mc.append(f)
completions = mc
log(u'fnames=<%s>' % map(ensure_unicode, completions))
return completions
示例4: parse_history_from_string
def parse_history_from_string(self, string=None):
'''Create a readline history from a string.
Each history item must be separated by a newline character (\n)'''
if not string:
return
for line in string.split("\n"):
self.add_history(ensure_unicode(line.rstrip()))
示例5: add_item
def add_item(self, line, force=False):
# Kludge. pyreadline is a pain in the ass.
from pyreadline import lineobj
from pyreadline.unicode_helper import ensure_unicode
line = ensure_unicode(line.rstrip())
readline.add_history(lineobj.ReadLineTextBuffer(line))
示例6: check_key
def check_key():
if msvcrt is None:
return False
else:
if msvcrt.kbhit():
q = ensure_unicode(msvcrt.getch())
return q
return ""
示例7: _readline_from_keyboard
def _readline_from_keyboard(self):
c = self.console
def nop(e):
pass
while 1:
self._update_line()
lbuf = self.l_buffer
log_sock("point:%d mark:%d selection_mark:%d" % (lbuf.point, lbuf.mark, lbuf.selection_mark))
try:
event = c.getkeypress()
log_sock(u">>%s" % event)
except KeyboardInterrupt:
from pyreadline.keysyms.common import KeyPress
from pyreadline.console.event import Event
event = Event(0, 0)
event.char = "c"
event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None)
log_sock("KBDIRQ")
if self.allow_ctrl_c:
now = time.time()
if (now - self.ctrl_c_timeout) < self.ctrl_c_tap_time_interval:
raise
else:
self.ctrl_c_timeout = now
pass
else:
raise
if self.next_meta:
self.next_meta = False
control, meta, shift, code = event.keyinfo
event.keyinfo = (control, True, shift, code)
# Process exit keys. Only exit on empty line
keyinfo = event.keyinfo.tuple()
if keyinfo in self.exit_dispatch:
if lineobj.EndOfLine(self.l_buffer) == 0:
raise EOFError
if len(keyinfo[-1]) > 1:
default = nop
else:
default = self.self_insert
dispatch_func = self.key_dispatch.get(keyinfo, default)
log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func))
log_sock((u"%s|%s" % (ensure_unicode(format(keyinfo)), dispatch_func.__name__)), "bound_function")
r = None
if dispatch_func:
r = dispatch_func(event)
self._keylog(dispatch_func, self.l_buffer)
self.l_buffer.push_undo()
self.previous_func = dispatch_func
if r:
self._update_line()
break
示例8: write_plain
def write_plain(self, text, attr=None):
'''write text at current cursor position.'''
log('write("%s", %s)' %(text,attr))
if attr is None:
attr = self.attr
n = c_int(0)
self.SetConsoleTextAttribute(self.hout, attr)
self.WriteConsoleW(self.hout, ensure_unicode(chunk), len(chunk), byref(junk), None)
return len(text)
示例9: write_scrolling
def write_scrolling(self, text, attr=None):
'''write text at current cursor position while watching for scrolling.
If the window scrolls because you are at the bottom of the screen
buffer, all positions that you are storing will be shifted by the
scroll amount. For example, I remember the cursor position of the
prompt so that I can redraw the line but if the window scrolls,
the remembered position is off.
This variant of write tries to keep track of the cursor position
so that it will know when the screen buffer is scrolled. It
returns the number of lines that the buffer scrolled.
'''
text = ensure_unicode(text)
x, y = self.pos()
w, h = self.size()
scroll = 0 # the result
# split the string into ordinary characters and funny characters
chunks = self.motion_char_re.split(text)
for chunk in chunks:
n = self.write_color(chunk, attr)
if len(chunk) == 1: # the funny characters will be alone
if chunk[0] == '\n': # newline
x = 0
y += 1
elif chunk[0] == '\r': # carriage return
x = 0
elif chunk[0] == '\t': # tab
x = 8 * (int(x / 8) + 1)
if x > w: # newline
x -= w
y += 1
elif chunk[0] == '\007': # bell
pass
elif chunk[0] == '\010':
x -= 1
if x < 0:
y -= 1 # backed up 1 line
else: # ordinary character
x += 1
if x == w: # wrap
x = 0
y += 1
if y == h: # scroll
scroll += 1
y = h - 1
else: # chunk of ordinary characters
x += n
l = int(x / w) # lines we advanced
x = x % w # new x value
y += l
if y >= h: # scroll
scroll += y - h + 1
y = h - 1
return scroll
示例10: GetClipboardText
def GetClipboardText():
text = u""
if OpenClipboard(0):
hClipMem = GetClipboardData(CF_TEXT)
if hClipMem:
GlobalLock.restype = c_char_p
text = GlobalLock(hClipMem)
GlobalUnlock(hClipMem)
CloseClipboard()
return ensure_unicode(text)
示例11: write_color
def write_color(self, text, attr=None):
text = ensure_unicode(text)
n,res= self.ansiwriter.write_color(text,attr)
junk = c_int(0)
for attr,chunk in res:
log(unicode(attr))
log(unicode(chunk))
self.SetConsoleTextAttribute(self.hout, attr.winattr)
self.WriteConsoleW(self.hout, chunk, len(chunk), byref(junk), None)
return n
示例12: read_history_file
def read_history_file(self, filename=None):
'''Load a readline history file.'''
if filename is None:
filename = self.history_filename
try:
for line in open(filename, 'r'):
self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip())))
except IOError:
self.history = []
self.history_cursor = 0
示例13: write_color
def write_color(self, text, attr=None):
text = ensure_unicode(text)
n, res = self.ansiwriter.write_color(text, attr)
junk = DWORD(0)
for attr, chunk in res:
log(u"console.attr:%s" % unicode(attr))
log(u"console.chunk:%s" % unicode(chunk))
self.SetConsoleTextAttribute(self.hout, attr.winattr)
for short_chunk in split_block(chunk):
self.WriteConsoleW(self.hout, short_chunk, len(short_chunk), byref(junk), None)
return n
示例14: add_history
def add_history(self, line):
'''Append a line to the history buffer, as if it was the last line typed.'''
line = ensure_unicode(line)
if not hasattr(line, "get_line_text"):
line = lineobj.ReadLineTextBuffer(line)
if not line.get_line_text():
pass
elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text():
pass
else:
self.history.append(line)
self.history_cursor = len(self.history)
示例15: SetClipboardText
def SetClipboardText(text):
buffer = create_unicode_buffer(ensure_unicode(text))
bufferSize = sizeof(buffer)
hGlobalMem = GlobalAlloc(c_int(GHND), c_int(bufferSize))
GlobalLock.restype = c_void_p
lpGlobalMem = GlobalLock(c_int(hGlobalMem))
memcpy(lpGlobalMem, addressof(buffer), c_int(bufferSize))
GlobalUnlock(c_int(hGlobalMem))
if OpenClipboard(0):
EmptyClipboard()
SetClipboardData(c_int(CF_UNICODETEXT), c_int(hGlobalMem))
CloseClipboard()