Пример #1
0
def sidebar(name, kvdict):
    # shamelessly stolen and adapted from ptpython/layout.py
    _MAX_KEY_WIDTH = 8
    _VAL_WIDTH = 14  # sufficient to print "0x" + 12hex chars for a 48bit memory address
    _CTR_WIDTH = _MAX_KEY_WIDTH + _VAL_WIDTH

    def center_str(s, w):
        l = len(s)
        e = w - l
        t = ''
        i = 0
        while i < e / 2:
            t += ' '
            i += 1
        t += s
        i = len(t)
        while i < w:
            t += ' '
            i += 1
        return t

    def pad_or_cut(s, w):
        if len(s) > w: s = s[:w]
        while len(s) < w:
            s += ' '
        return s

    def get_text_fragments():
        tokens = []

        def append_title(title):
            @if_mousedown
            def focus_from_title(mouse_event):
                get_app().my.set_focus(name)

            foc = ',focused' if get_app().my.focused_control == name else ''
            tokens.extend([
                ('class:sidebar', ' ', focus_from_title),
                ('class:sidebar.title' + foc, center_str(title, _CTR_WIDTH),
                 focus_from_title),
                ('class:sidebar', '\n'),
            ])

        def append(index, label, status, max_key_len):
            key_len = min(_MAX_KEY_WIDTH, max_key_len)
            val_len = _CTR_WIDTH - key_len
            selected = get_app(
            ).my.controls[name].selected_option_index == index

            @if_mousedown
            def select_item(mouse_event):
                get_app().my.set_focus(name)
                get_app().my.controls[name].selected_option_index = index

            @if_mousedown
            def trigger_vardetail(mouse_event):
                get_app().my.set_focus(name)
                get_app().my.controls[name].selected_option_index = index
                vardetails_toggle_on_off()

            odd = 'odd' if index % 2 != 0 else ''
            sel = ',selected' if selected else ''
            chg = ',changed' if kvdict().was_changed(label) else ''
            tokens.append(('class:sidebar' + sel, '>' if selected else ' '))
            tokens.append(('class:sidebar.label' + odd + sel,
                           pad_or_cut(label, key_len), select_item))
            tokens.append(('class:sidebar.status' + odd + sel + chg,
                           pad_or_cut(status, val_len), trigger_vardetail))
            if selected:
                tokens.append(('[SetCursorPosition]', ''))
            tokens.append(('class:sidebar', '<' if selected else ' '))
            tokens.append(('class:sidebar', '\n'))

        i = 0
        append_title(name)
        mydict = kvdict() if callable(kvdict) else kvdict
        max_key_len = 0
        for key in mydict:
            max_key_len = max(max_key_len, len(key))
        for key in mydict:
            values = mydict[key]
            append(i, key, '%s' % values[0], max_key_len + 1)
            i += 1
        tokens.pop()  # Remove last newline.
        i += 1  # title
        get_app().my.controls[name].height = Dimension(min=2,
                                                       max=i +
                                                       1 if i > 1 else 2)
        return tokens

    ctrl = Window(SidebarControl(get_text_fragments),
                  style='class:sidebar',
                  width=Dimension.exact(_CTR_WIDTH + 2),
                  height=Dimension(min=2),
                  scroll_offsets=ScrollOffsets(top=1, bottom=1))
    ctrl.selected_option_index = 0
    return ctrl