def __init__(self, text, style='', width=None, dont_extend_height=True, dont_extend_width=False): assert is_formatted_text(text) self.text = text 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 self.formatted_text_control = FormattedTextControl( text=lambda: self.text) self.window = Window(content=self.formatted_text_control, width=get_width, style='class:label ' + style, dont_extend_height=dont_extend_height, dont_extend_width=dont_extend_width)
def __init__(self, text, style='', width=None, dont_extend_height=True, dont_extend_width=False): assert is_formatted_text(text) self.text = text 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 self.formatted_text_control = FormattedTextControl( text=lambda: self.text) self.window = Window( content=self.formatted_text_control, width=get_width, style='class:label ' + style, dont_extend_height=dont_extend_height, dont_extend_width=dont_extend_width)
def __init__(self, body, title='', buttons=None, modal=True, width=None, with_background=False): assert is_formatted_text(title) assert buttons is None or isinstance(buttons, list) self.body = body self.title = title buttons = buttons or [] # When a button is selected, handle left/right key bindings. buttons_kb = KeyBindings() if len(buttons) > 1: first_selected = has_focus(buttons[0]) last_selected = has_focus(buttons[-1]) buttons_kb.add('left', filter=~first_selected)(focus_previous) buttons_kb.add('right', filter=~last_selected)(focus_next) if buttons: frame_body = HSplit([ # Add optional padding around the body. Box(body=DynamicContainer(lambda: self.body), padding=D(preferred=1, max=1), padding_bottom=0), # The buttons. Box(body=VSplit(buttons, padding=1, key_bindings=buttons_kb), height=D(min=1, max=3, preferred=3)) ]) else: frame_body = body # Key bindings for whole dialog. kb = KeyBindings() kb.add('tab', filter=~has_completions)(focus_next) kb.add('s-tab', filter=~has_completions)(focus_previous) frame = Shadow(body=Frame( title=lambda: self.title, body=frame_body, style='class:dialog.body', width=(None if with_background is None else width), key_bindings=kb, modal=modal, )) if with_background: self.container = Box(body=frame, style='class:dialog', width=width) else: self.container = frame
def __init__(self, body, title='', buttons=None, modal=True, width=None, with_background=False): assert is_formatted_text(title) assert buttons is None or isinstance(buttons, list) self.body = body self.title = title buttons = buttons or [] # When a button is selected, handle left/right key bindings. buttons_kb = KeyBindings() if len(buttons) > 1: first_selected = has_focus(buttons[0]) last_selected = has_focus(buttons[-1]) buttons_kb.add('left', filter=~first_selected)(focus_previous) buttons_kb.add('right', filter=~last_selected)(focus_next) if buttons: frame_body = HSplit([ # Add optional padding around the body. Box(body=DynamicContainer(lambda: self.body), padding=D(preferred=1, max=1), padding_bottom=0), # The buttons. Box(body=VSplit(buttons, padding=1, key_bindings=buttons_kb), height=D(min=1, max=3, preferred=3)) ]) else: frame_body = body # Key bindings for whole dialog. kb = KeyBindings() kb.add('tab', filter=~has_completions)(focus_next) kb.add('s-tab', filter=~has_completions)(focus_previous) frame = Shadow(body=Frame( title=lambda: self.title, body=frame_body, style='class:dialog.body', width=(None if with_background is None else width), key_bindings=kb, modal=modal, )) if with_background: self.container = Box( body=frame, style='class:dialog', width=width) else: self.container = frame
def __call__(self, data=None, label='', remove_when_done=False, total=None): """ Start a new counter. :param label: Title text or description for this progress. (This can be formatted text as well). :param remove_when_done: When `True`, hide this progress bar. :param total: Specify the maximum value if it can't be calculated by calling ``len``. """ assert is_formatted_text(label) assert isinstance(remove_when_done, bool) counter = ProgressBarCounter( self, data, label=label, remove_when_done=remove_when_done, total=total) self.counters.append(counter) return counter
def __init__(self, body, title='', buttons=None, modal=True, width=None, with_background=False): assert is_formatted_text(title) assert buttons is None or isinstance(buttons, list) buttons = buttons or [] if buttons: frame_body = HSplit([ # Add optional padding around the body. Box(body=body, padding=D(preferred=1, max=1), padding_bottom=0), # The buttons. Box(body=VSplit(buttons, padding=1), height=D(min=1, max=3, preferred=3)) ]) else: frame_body = body # Key bindings. kb = KeyBindings() kb.add('tab', filter=~has_completions)(focus_next) kb.add('s-tab', filter=~has_completions)(focus_previous) frame = Shadow(body=Frame( title=title, body=frame_body, style='class:dialog.body', width=(None if with_background is None else width), key_bindings=kb, modal=modal, )) if with_background: self.container = Box(body=frame, style='class:dialog', width=width) else: self.container = frame
def __init__(self, text=''): assert is_formatted_text(text) self.checked = True kb = KeyBindings() @kb.add(' ') @kb.add('enter') def _(event): self.checked = not self.checked self.control = FormattedTextControl(self._get_text_fragments, key_bindings=kb, focusable=True) self.window = Window(width=3, content=self.control, height=1) self.container = VSplit( [self.window, Label(text=Template(' {}').format(text))], style='class:checkbox')
def __init__(self, text=''): assert is_formatted_text(text) self.checked = True kb = KeyBindings() @kb.add(' ') @kb.add('enter') def _(event): self.checked = not self.checked self.control = FormattedTextControl( self._get_text_fragments, key_bindings=kb, focusable=True) self.window = Window( width=3, content=self.control, height=1) self.container = VSplit([ self.window, Label(text=Template(' {}').format(text)) ], style='class:checkbox')
def get_raw_text(text): if is_formatted_text(text): return ''.join([token[1] for token in text.formatted_text]) else: return text
def __init__(self, body, title='', style='', width=None, height=None, key_bindings=None, modal=False): assert is_container(body) assert is_formatted_text(title) assert isinstance(style, six.text_type) assert is_dimension(width) assert is_dimension(height) assert key_bindings is None or isinstance(key_bindings, KeyBindings) assert isinstance(modal, bool) fill = partial(Window, style='class:frame.border') style = 'class:frame ' + style if title: top_row = VSplit([ fill(width=1, height=1, char=Border.TOP_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char='|'), Label(Template(' {} ').format(title), style='class:frame.label', dont_extend_width=True), fill(width=1, height=1, char='|'), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.TOP_RIGHT), ], height=1) else: top_row = VSplit([ fill(width=1, height=1, char=Border.TOP_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.TOP_RIGHT), ], height=1) self.container = HSplit( [ top_row, VSplit( [ fill(width=1, char=Border.VERTICAL), body, fill(width=1, char=Border.VERTICAL), # Padding is required to make sure that if the content is # too small, that the right frame border is still aligned. ], padding=0), VSplit([ fill(width=1, height=1, char=Border.BOTTOM_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.BOTTOM_RIGHT), ]), ], width=width, height=height, style=style, key_bindings=key_bindings, modal=modal)
def __init__(self, body, title='', style='', width=None, height=None, key_bindings=None, modal=False): assert is_container(body) assert is_formatted_text(title) assert isinstance(style, six.text_type) assert is_dimension(width) assert is_dimension(height) assert key_bindings is None or isinstance(key_bindings, KeyBindings) assert isinstance(modal, bool) self.title = title self.body = body fill = partial(Window, style='class:frame.border') style = 'class:frame ' + style top_row_with_title = VSplit( [ fill(width=1, height=1, char=Border.TOP_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char='|'), # Notice: we use `Template` here, because `self.title` can be an # `HTML` object for instance. Label(lambda: Template(' {} ').format(self.title), style='class:frame.label', dont_extend_width=True), fill(width=1, height=1, char='|'), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.TOP_RIGHT), ], height=1) top_row_without_title = VSplit([ fill(width=1, height=1, char=Border.TOP_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.TOP_RIGHT), ], height=1) @Condition def has_title(): return bool(self.title) self.container = HSplit( [ ConditionalContainer(content=top_row_with_title, filter=has_title), ConditionalContainer(content=top_row_without_title, filter=~has_title), VSplit( [ fill(width=1, char=Border.VERTICAL), DynamicContainer(lambda: self.body), fill(width=1, char=Border.VERTICAL), # Padding is required to make sure that if the content is # too small, that the right frame border is still aligned. ], padding=0), VSplit([ fill(width=1, height=1, char=Border.BOTTOM_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.BOTTOM_RIGHT), ]), ], width=width, height=height, style=style, key_bindings=key_bindings, modal=modal)
def __init__(self, body, title='', style='', width=None, height=None, key_bindings=None, modal=False): assert is_container(body) assert is_formatted_text(title) assert isinstance(style, six.text_type) assert is_dimension(width) assert is_dimension(height) assert key_bindings is None or isinstance(key_bindings, KeyBindings) assert isinstance(modal, bool) self.title = title self.body = body fill = partial(Window, style='class:frame.border') style = 'class:frame ' + style top_row_with_title = VSplit([ fill(width=1, height=1, char=Border.TOP_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char='|'), # Notice: we use `Template` here, because `self.title` can be an # `HTML` object for instance. Label(lambda: Template(' {} ').format(self.title), style='class:frame.label', dont_extend_width=True), fill(width=1, height=1, char='|'), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.TOP_RIGHT), ], height=1) top_row_without_title = VSplit([ fill(width=1, height=1, char=Border.TOP_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.TOP_RIGHT), ], height=1) @Condition def has_title(): return bool(self.title) self.container = HSplit([ ConditionalContainer( content=top_row_with_title, filter=has_title), ConditionalContainer( content=top_row_without_title, filter=~has_title), VSplit([ fill(width=1, char=Border.VERTICAL), DynamicContainer(lambda: self.body), fill(width=1, char=Border.VERTICAL), # Padding is required to make sure that if the content is # too small, the right frame border is still aligned. ], padding=0), VSplit([ fill(width=1, height=1, char=Border.BOTTOM_LEFT), fill(char=Border.HORIZONTAL), fill(width=1, height=1, char=Border.BOTTOM_RIGHT), ]), ], width=width, height=height, style=style, key_bindings=key_bindings, modal=modal)