def _draw_text_display(self, text, space): wtext = WideString(text) wellip = WideString(self.ellipsis[self.settings.unicode_ellipsis]) if len(wtext) > space: wtext = wtext[:max(0, space - len(wellip))] + wellip return [[str(wtext), []]]
def _draw_text_display(self, text, space): wtext = WideString(text) wext = WideString(splitext(text)[1]) wellip = WideString(self.ellipsis[self.settings.unicode_ellipsis]) if len(wtext) > space: wtext = wtext[:max(1, space - len(wext) - len(wellip))] + wellip + wext # Truncate again if still too long. if len(wtext) > space: wtext = wtext[:max(0, space - len(wellip))] + wellip return [[str(wtext), []]]
def __init__(self, string, *lst): self.string = WideString(string) self.lst = lst self.fixed = False if not len(string) or not len(self.string.chars): self.min_size = 0 elif PY3: self.min_size = utf_char_width(string[0]) else: self.min_size = utf_char_width(self.string.chars[0].decode('utf-8'))
def draw(self): self.win.erase() if self.question_queue: assert isinstance(self.question_queue[0], tuple) assert len(self.question_queue[0]) == 3 self.addstr(0, 0, self.question_queue[0][0][self.pos:]) return self.addstr(0, 0, self.prompt) line = WideString(self.line) if line: x = self._calculate_offset() self.addstr(0, len(self.prompt), str(line[x:]))
def char_len(ansi_text): """Count the number of visible characters. >>> char_len("\x1b[0;30;40mX\x1b[0m") 1 >>> char_len("\x1b[0;30;40mXY\x1b[0m") 2 >>> char_len("\x1b[0;30;40mX\x1b[0mY") 2 >>> char_len("hello") 5 >>> char_len("") 0 """ return len(WideString(ansi_re.sub('', ansi_text)))
def char_slice(ansi_text, start, length): """Slices a string with respect to ansi code sequences Acts as if the ansi codes aren't there, slices the text from the given start point to the given length and adds the codes back in. >>> test_string = "abcde\x1b[30mfoo\x1b[31mbar\x1b[0mnormal" >>> split_ansi_from_text(test_string) ['abcde', '\\x1b[30m', 'foo', '\\x1b[31m', 'bar', '\\x1b[0m', 'normal'] >>> char_slice(test_string, 1, 3) 'bcd' >>> char_slice(test_string, 5, 6) '\\x1b[30mfoo\\x1b[31mbar' >>> char_slice(test_string, 0, 8) 'abcde\\x1b[30mfoo' >>> char_slice(test_string, 4, 4) 'e\\x1b[30mfoo' >>> char_slice(test_string, 11, 100) '\\x1b[0mnormal' >>> char_slice(test_string, 9, 100) '\\x1b[31mar\\x1b[0mnormal' >>> char_slice(test_string, 9, 4) '\\x1b[31mar\\x1b[0mno' """ chunks = [] last_color = "" pos = old_pos = 0 for i, chunk in enumerate(split_ansi_from_text(ansi_text)): if i % 2 == 1: last_color = chunk continue chunk = WideString(chunk) old_pos = pos pos += len(chunk) if pos <= start: pass # seek elif old_pos < start <= pos: chunks.append(last_color) chunks.append(str(chunk[start - old_pos:start - old_pos + length])) elif pos > length + start: chunks.append(last_color) chunks.append(str(chunk[:start - old_pos + length])) else: chunks.append(last_color) chunks.append(str(chunk)) if pos - start >= length: break return ''.join(chunks)
def draw(self): self.win.erase() if self.question_queue: assert isinstance(self.question_queue[0], tuple) assert len(self.question_queue[0]) == 3 self.addstr(0, 0, self.question_queue[0][0]) return self.addstr(0, 0, self.prompt) line = WideString(self.line) overflow = -self.wid + len(self.prompt) + len(line) + 1 if overflow > 0: self.addstr(0, len(self.prompt), str(line[overflow:])) else: self.addstr(0, len(self.prompt), self.line)
def widestring_len(ansi_text): """Count the number of visible widestring. >>> widestring_len("\x1b[0;34;40m中国China\x1b[0m") 9 >>> widestring_len("\x1b[0;34;40m你好\x1b[0m") 4 >>> widestring_len("\x1b[0;34;40m谢谢你\x1b[0mhello") 11 >>> widestring_len("hello") 5 >>> widestring_len("") 0 """ return len(WideString(ansi_re.sub('', ansi_text)))
def _total_len(predisplay): return sum([len(WideString(s)) for s, _ in predisplay])
def _total_len(self, predisplay): return sum([len(WideString(s)) for s, L in predisplay])
def get_line(k, v): keys = '.'.join(k) return '{wlen:>2} | {value} | {keys}'.format(wlen=len(WideString(v)), value=v, keys=keys)
def _draw_directory(self): """Draw the contents of a directory""" if self.level > 0 and not self.settings.preview_directories: return base_color = ['in_browser'] self.win.move(0, 0) if not self.target.content_loaded: self.color(base_color) self.addnstr("...", self.wid) self.color_reset() return if self.main_column: base_color.append('main_column') if not self.target.accessible: self.color(base_color, 'error') self.addnstr("not accessible", self.wid) self.color_reset() return if self.target.empty(): self.color(base_color, 'empty') self.addnstr("empty", self.wid) self.color_reset() return self._set_scroll_begin() copied = [f.path for f in self.env.copy] ellipsis = self.ellipsis[self.settings.unicode_ellipsis] selected_i = self.target.pointer for line in range(self.hei): i = line + self.scroll_begin try: drawn = self.target.files[i] except IndexError: break if self.display_infostring and drawn.infostring \ and self.settings.display_size_in_main_column: infostring = str(drawn.infostring) + " " else: infostring = "" bad_info_color = None this_color = base_color + list(drawn.mimetype_tuple) text = drawn.basename tagged = self.fm.tags and drawn.realpath in self.fm.tags space = self.wid - len(infostring) if self.main_column: space -= 2 # if len(text) > space: # text = text[:space-1] + self.ellipsis if i == selected_i: this_color.append('selected') if drawn.marked: this_color.append('marked') if self.main_column: text = " " + text if tagged: this_color.append('tagged') if self.main_column: text = self.tagged_marker + text if drawn.is_directory: this_color.append('directory') else: this_color.append('file') if drawn.stat: mode = drawn.stat.st_mode if mode & stat.S_IXUSR: this_color.append('executable') if stat.S_ISFIFO(mode): this_color.append('fifo') if stat.S_ISSOCK(mode): this_color.append('socket') if drawn.is_device: this_color.append('device') if drawn.path in copied: this_color.append('cut' if self.env.cut else 'copied') if drawn.is_link: this_color.append('link') this_color.append(drawn.exists and 'good' or 'bad') string = drawn.basename from ranger.ext.widestring import WideString wtext = WideString(text) if len(wtext) > space: wtext = wtext[:space - 1] + ellipsis if self.main_column: if tagged: self.addstr(line, 0, str(wtext)) elif self.wid > 1: self.addstr(line, 1, str(wtext)) else: self.addstr(line, 0, str(wtext)) if infostring: x = self.wid - 1 - len(infostring) if infostring is BAD_INFO: bad_info_color = (x, len(infostring)) if x > 0: self.addstr(line, x, infostring) self.color_at(line, 0, self.wid, this_color) if bad_info_color: start, wid = bad_info_color self.color_at(line, start, wid, this_color, 'badinfo') if self.main_column and tagged and self.wid > 2: this_color.append('tag_marker') self.color_at(line, 0, len(self.tagged_marker), this_color) self.color_reset()