def send_above_prompt(self, formatted_text): """ Send text to the client. This is asynchronous, returns a `Future`. """ formatted_text = to_formatted_text(formatted_text) return self._run_in_terminal(lambda: self.send(formatted_text))
def print_formatted_text(output, formatted_text, style, color_depth=None): """ Print a list of (style_str, text) tuples in the given style to the output. """ assert isinstance(output, Output) assert isinstance(style, BaseStyle) assert color_depth is None or color_depth in ColorDepth._ALL fragments = to_formatted_text(formatted_text) color_depth = color_depth or ColorDepth.default() # Reset first. output.reset_attributes() output.enable_autowrap() # Print all (style_str, text) tuples. attrs_for_style_string = _StyleStringToAttrsCache( style.get_attrs_for_style_str) for style_str, text in fragments: attrs = attrs_for_style_string[style_str] if attrs: output.set_attributes(attrs, color_depth) else: output.reset_attributes() # Assume that the output is raw, and insert a carriage return before # every newline. (Also important when the front-end is a telnet client.) assert '\r' not in text output.write(text.replace('\n', '\r\n')) # Reset again. output.reset_attributes() output.flush()
def send(self, formatted_text): """ Send text to the client. """ formatted_text = to_formatted_text(formatted_text) print_formatted_text(self.vt100_output, formatted_text, self.style or DummyStyle())
def _get_text_fragments(self): result = [] for i, value in enumerate(self.values): checked = (value[0] == self.current_value) selected = (i == self._selected_index) style = '' if checked: style += ' class:radio-checked' if selected: style += ' class:radio-selected' result.append((style, '(')) if selected: result.append(('[SetCursorPosition]', '')) if checked: result.append((style, '*')) else: result.append((style, ' ')) result.append((style, ')')) result.append(('class:radio', ' ')) result.extend(to_formatted_text(value[1], style='class:radio')) result.append(('', '\n')) result.pop() # Remove last newline. return result
def apply_transformation(self, ti): # Insert fragments after the last line. if ti.lineno == ti.document.line_count - 1: # Get fragments. fragments_after = to_formatted_text(self.text, self.style) return Transformation(fragments=ti.fragments + fragments_after) else: return Transformation(fragments=ti.fragments)
def create_margin(self, window_render_info, width, height): get_continuation = self.get_continuation result = [] # First line. result.extend(to_formatted_text(self.get_prompt())) # Next lines. if get_continuation: last_y = None for y in window_render_info.displayed_lines[1:]: result.append(('', '\n')) result.extend( to_formatted_text(get_continuation(width, y, y == last_y))) last_y = y return result
def get_width(): if width is None: text_fragments = to_formatted_text(self.text) text = fragment_list_to_text(text_fragments) if text: longest_line = max( get_cwidth(line) for line in text.splitlines()) else: return D(preferred=0) return D(preferred=longest_line) else: return width
def format(self, progress_bar, progress, width): # Get formatted text from nested formatter, and explode it in # text/style tuples. result = self.formatter.format(progress_bar, progress, width) result = explode_text_fragments(to_formatted_text(result)) # Insert colors. result2 = [] shift = int(time.time() * 3) % len(self.colors) for i, (style, text) in enumerate(result): result2.append( (style + ' ' + self.colors[(i + shift) % len(self.colors)], text)) return result2
def apply_transformation(self, ti): if ti.lineno == 0: # Get fragments. fragments_before = to_formatted_text(self.text, self.style) fragments = fragments_before + ti.fragments shift_position = fragment_list_len(fragments_before) source_to_display = lambda i: i + shift_position display_to_source = lambda i: i - shift_position else: fragments = ti.fragments source_to_display = None display_to_source = None return Transformation(fragments, source_to_display=source_to_display, display_to_source=display_to_source)
def create_content(self, width, height): items = [] for pr in self.progress_bar.counters: try: text = self.formatter.format(self.progress_bar, pr, width) except BaseException: traceback.print_exc() text = 'ERROR' items.append(to_formatted_text(text)) def get_line(i): return items[i] return UIContent(get_line=get_line, line_count=len(items), show_cursor=False)
def _get_continuation(self, width, line_number, is_soft_wrap): """ Insert the prompt continuation. :param width: The width that's available for the continuation (don't exceed this). :param line_number: :param is_soft_wrap: True when we got a soft wrap here instead of a hard line ending. """ prompt_continuation = self.prompt_continuation if callable(prompt_continuation): prompt_continuation = prompt_continuation(width, line_number, is_soft_wrap) return to_formatted_text(prompt_continuation, style='class:prompt-continuation')
def _get_prompt(self): return to_formatted_text(self.message, style='class:prompt')
def _add_suffix(self, label): label = to_formatted_text(label, style='class:label') return label + [('', self.suffix)]
def __init__(self, text, style=''): self.text = to_formatted_text(text, style=style)