Example #1
0
    def render(self, size, focus=False):
        if len(size) < 2:
            cols, rows = (size[0], None)
        else:
            cols, rows = size

        if len(self._contents) < 1:
            # No contents - return empty canvas
            return urwid.SolidCanvas(' ', cols, rows)

        combinelist = []
        position = self._contents.focus

        # Render tab bar and add it to combinelist.  The tab bar is always
        # rendered as focused to highlight the focused tab.
        canvas = self._tabbar_render((cols,), True)
        combinelist.append((canvas, position, True))
        if rows is not None:
            rows -= 1  # Account for title bar

        # Render and add content of currently selected tab
        current_widget = self._contents[position]
        if current_widget is None:
            canvas = urwid.SolidCanvas(' ', cols, rows)
        else:
            if rows is not None:
                canvas = current_widget.render((cols,rows), focus)
            else:
                canvas = current_widget.render((cols,), focus)
        combinelist.append((canvas, position, focus))
        return urwid.CanvasCombine(combinelist)
Example #2
0
File: tabs.py Project: rndusr/stig
    def render(self, size, focus=False):
        if len(size) < 2:
            cols, rows = (size[0], None)
        else:
            cols, rows = size

        if len(self._contents) < 1:
            # No contents - return empty canvas
            return urwid.SolidCanvas(' ', cols, rows)

        if rows is not None:
            size_content = (cols, rows - self._tabbar.rows((cols, )))
        else:
            size_content = (cols, )

        combinelist = []
        position = self._contents.focus

        # Always render tab titles as focused to highlight the focused tab
        canvas = self._tabbar.render((cols, ), focus=True)
        combinelist.append((canvas, position, True))

        # Render and add content of currently selected tab
        current_widget = self._contents[position]
        if current_widget is None:
            canvas = urwid.SolidCanvas(' ', *size_content)
        else:
            canvas = current_widget.render(size_content, focus)
        combinelist.append((canvas, position, focus))
        return urwid.CanvasCombine(combinelist)
Example #3
0
    def render(self, size, focus=False):
        (maxcol, ) = size
        if self._history is None:
            # No data; display nothing
            return ur.SolidCanvas('╌', maxcol, 1)
        if self._minimum == self._maximum:
            # Minimum and maximum are equal; display nothing
            return ur.SolidCanvas('╌', maxcol, 1)

        if isinstance(self._format, str):
            label = self._format.format(min=self._minimum, max=self._maximum)
        else:
            label = self._format(self._minimum, self._maximum)
        bar_len = maxcol - len(label)
        if bar_len < 4:
            # Bar is too short to be useful; just display ▸▸▸
            return ur.SolidCanvas('▸', maxcol, 1)

        step = (self._maximum - self._minimum) / len(self.chars)
        bar_ranges = [
            self._minimum + (i * step) for i in range(len(self.chars))
        ]
        bar = ''.join(self.chars[min(
            len(self.chars) - 1, bisect_left(bar_ranges, value))]
                      for value in reversed(self._history[:bar_len]))
        s = '{bar:{bar_align}{bar_len}s}{label}'.format(
            bar=bar, bar_align=self.bar_align, bar_len=bar_len, label=label)
        text, cs = ur.apply_target_encoding(s)
        return ur.TextCanvas([text], [cs], maxcol=maxcol)
Example #4
0
    def render(self, size, focus=False):
        (maxcol, ) = size
        if not self._total:
            return ur.SolidCanvas('╌', maxcol, 1)
        total_label = str(self._total)

        bar_len = maxcol - sum(
            len(s) for s in (self.left, self.right, total_label))
        bar_len -= len(self._parts) - 1  # separators
        if bar_len < len(self._parts):
            # Bar is too short to be useful; just display ▸▸▸
            return ur.SolidCanvas('▸', maxcol, 1)

        part_lens = [
            round(bar_len * n / self._total) for part, n in self._parts
        ]
        if sum(part_lens) > bar_len:
            longest_ix = part_lens.index(max(part_lens))
            part_lens[longest_ix] -= 1
        elif sum(part_lens) < bar_len:
            shortest_ix = part_lens.index(min(part_lens))
            part_lens[shortest_ix] += 1
        assert sum(part_lens) == bar_len

        bar = self.sep.join(
            '{0:{fill}^{width}}'.format(part, fill=bar_char, width=part_len)
            if len(part) + 2 <= part_len else bar_char * part_len
            for (part, count), part_len, bar_char in zip(
                self._parts, part_lens, cycle(self.bar)))
        s = ''.join((self.left, bar, self.right, total_label))
        text, cs = ur.apply_target_encoding(s)
        return ur.TextCanvas([text], [cs], maxcol=maxcol)
Example #5
0
    def render(self, size, focus=False):
        maxcol, maxrow = size

        sb_width = self._scrollbar_width
        ow_size = (max(0, maxcol - sb_width), maxrow)
        sb_width = maxcol - ow_size[0]

        ow = self._original_widget
        ow_base = self.scrolling_base_widget
        ow_rows_max = ow_base.rows_max(size, focus)
        if ow_rows_max <= maxrow:
            # Canvas fits without scrolling - no scrollbar needed
            self._original_widget_size = size
            return ow.render(size, focus)
        ow_rows_max = ow_base.rows_max(ow_size, focus)

        ow_canv = ow.render(ow_size, focus)
        self._original_widget_size = ow_size

        pos = ow_base.get_scrollpos(ow_size, focus)
        posmax = ow_rows_max - maxrow

        # Thumb shrinks/grows according to the ratio of
        # <number of visible lines> / <number of total lines>
        thumb_weight = min(1, maxrow / max(1, ow_rows_max))
        thumb_height = max(1, round(thumb_weight * maxrow))

        # Thumb may only touch top/bottom if the first/last row is visible
        top_weight = float(pos) / max(1, posmax)
        top_height = int((maxrow - thumb_height) * top_weight)
        if top_height == 0 and top_weight > 0:
            top_height = 1

        # Bottom part is remaining space
        bottom_height = maxrow - thumb_height - top_height
        assert thumb_height + top_height + bottom_height == maxrow

        # Create scrollbar canvas
        # Creating SolidCanvases of correct height may result in "cviews do not
        # fill gaps in shard_tail!" or "cviews overflow gaps in shard_tail!"
        # exceptions. Stacking the same SolidCanvas is a workaround.
        # https://github.com/urwid/urwid/issues/226#issuecomment-437176837
        top = urwid.SolidCanvas(self._trough_char, sb_width, 1)
        thumb = urwid.SolidCanvas(self._thumb_char, sb_width, 1)
        bottom = urwid.SolidCanvas(self._trough_char, sb_width, 1)
        sb_canv = urwid.CanvasCombine(
            [(top, None, False)] * top_height +
            [(thumb, None, False)] * thumb_height +
            [(bottom, None, False)] * bottom_height,
        )

        combinelist = [(ow_canv, None, True, ow_size[0]),
                       (sb_canv, None, False, sb_width)]
        if self._scrollbar_side != SCROLLBAR_LEFT:
            return urwid.CanvasJoin(combinelist)
        else:
            return urwid.CanvasJoin(reversed(combinelist))
Example #6
0
    def render(self, size, focus=False):
        maxcol, maxrow = size

        ow = self._original_widget
        ow_base = self.scrolling_base_widget
        ow_rows_max = ow_base.rows_max(size, focus)
        if ow_rows_max <= maxrow:
            # Canvas fits without scrolling - no scrollbar needed
            self._original_widget_size = size
            return ow.render(size, focus)

        sb_width = self._scrollbar_width
        self._original_widget_size = ow_size = (maxcol - sb_width, maxrow)
        ow_canv = ow.render(ow_size, focus)

        pos = ow_base.get_scrollpos(ow_size, focus)
        posmax = ow_rows_max - maxrow

        # Thumb shrinks/grows according to the ratio of
        # <number of visible lines> / <number of total lines>
        thumb_weight = min(1, maxrow / max(1, ow_rows_max))
        thumb_height = max(1, round(thumb_weight * maxrow))

        # Thumb may only touch top/bottom if the first/last row is visible
        top_weight = float(pos) / max(1, posmax)
        top_height = int((maxrow - thumb_height) * top_weight)
        if top_height == 0 and top_weight > 0:
            top_height = 1

        # Bottom part is remaining space
        bottom_height = maxrow - thumb_height - top_height
        assert thumb_height + top_height + bottom_height == maxrow

        # Create scrollbar canvas
        top = urwid.SolidCanvas(self._trough_char, sb_width, top_height)
        thumb = urwid.SolidCanvas(self._thumb_char, sb_width, thumb_height)
        bottom = urwid.SolidCanvas(self._trough_char, sb_width, bottom_height)
        sb_canv = urwid.CanvasCombine([
            (top, None, False),
            (thumb, None, False),
            (bottom, None, False),
        ])

        combinelist = [(ow_canv, None, True, ow_size[0]),
                       (sb_canv, None, False, sb_width)]
        if self._scrollbar_side != SCROLLBAR_LEFT:
            return urwid.CanvasJoin(combinelist)
        else:
            return urwid.CanvasJoin(reversed(combinelist))
Example #7
0
    def render_init(self, size):
        (maxcol, maxrow) = size
        bardata, top, hlines = self.get_data((maxcol, maxrow))

        back_char = self.char[0]

        self.column_canvas_list = []

        # create the empty bars, for the bar columns that do not have data yet
        for no_data in range(maxcol - len(bardata)):
            back_canvas = urwid.SolidCanvas(back_char, 1, maxrow)
            combine_list_item = [(back_canvas, None, False)]

            column_canvas = urwid.CanvasCombine(combine_list_item)

            self.column_canvas_list.append((column_canvas, None, False, 1))

        # for each data point, create a single bar graph
        color_index = 0
        data_value = 0
        for single_bar_data in bardata:
            for pallet_index, value in enumerate(single_bar_data):
                color_index = pallet_index
                data_value = value

                if data_value != 0:
                    break

            self.render_incremental(size, data_value, color_index)
Example #8
0
    def render(self, size, focus=False):
        maxcol = size[0]
        item_rows = None

        combinelist = []
        for i, (w, (f, height)) in enumerate(self.contents):
            item_focus = self.focus_item == w
            canv = None
            if f == urwid.widget.GIVEN:
                canv = w.render((maxcol, height), focus=focus)
            elif f == urwid.widget.PACK or len(size) == 1:
                canv = w.render((maxcol, ), focus=focus)
            else:
                if item_rows is None:
                    item_rows = self.get_item_rows(size, focus)
                rows = item_rows[i]
                if rows > 0:
                    canv = w.render((maxcol, rows), focus=focus)
            if canv:
                combinelist.append((canv, i, item_focus))
        if not combinelist:
            return urwid.SolidCanvas(" ", size[0], (size[1:] + (0, ))[0])

        out = urwid.CanvasCombine(combinelist)
        if len(size) == 2 and size[1] != out.rows():
            # flow/fixed widgets rendered too large/small
            out = urwid.CompositeCanvas(out)
            out.pad_trim_top_bottom(0, size[1] - out.rows())
        return out
Example #9
0
 def render(self, size, focus=False):
     (maxcol, maxrow) = size
     rows = int((self.size * maxrow) + 0.5)
     comp = urwid.CompositeCanvas(
         urwid.SolidCanvas(self._fill, maxcol, rows))
     pad = maxrow - rows
     toppad = int((float(pad) * self.location) + 0.5)
     bottompad = pad - toppad
     comp.pad_trim_top_bottom(toppad, bottompad)
     comp.fill_attr(self._attr)
     return comp
Example #10
0
 def render(self, size, focus=False):
     (maxcol, maxrow) = size
     cols = int((self.size * maxcol) + 0.5)
     comp = urwid.CompositeCanvas(
         urwid.SolidCanvas(self._fill, cols, maxrow))
     pad = maxcol - cols
     leftpad = int((float(pad) * self.location) + 0.5)
     rightpad = pad - leftpad
     comp.pad_trim_left_right(leftpad, rightpad)
     comp.fill_attr(self._attr)
     return comp
    def render(self, szie, focus=False):
        maxcol, maxrow = size
        ow = self._original_widget
        ow_base = self._scrolling_base_widget
        ow_rows_max = ow_base.rows_max(size, focus)
        if ow_rows_max <= maxrow:
            self._original_widget_size = size
            return ow.render(size, focus)

        sb_width = self._scrollbar_width
        self._original_widget_size = ow_size = (maxcol - sb_width, maxrow)
        ow_canv = ow.render(ow_size, focus)

        pos = ow_base.get_scrollpos(ow_size, focus)
        posmax = ow_rows_max - maxrow

        thumb_weight = min(1, maxrow / max(1, ow_rows_max))
        thumb_height = max(1, round(thumb_weight * maxrow))

        top_weight = float(pos) / max(1, posmax)
        top_height = int((maxrow - thumb_height) * top_weight)
        if top_height == 0 and top_weight > 0:
            top_height = 1

        bottom_height = maxrow - thumb_height - top_height
        assert thumb_height + top_height + bottom_height == maxrow

        top = urwid.SolidCanvas(self._trough_char, sb_width, top_height)
        thumb = urwid.SolidCanvas(self._thumb_char, sb_width, thumb_height)
        bottom = urwid.SolidCanvas(self._trough_char, sb_width, bottom_height)
        sb_canv = urwid.CanvasCombine([
            (top, None, False),
            (thumb, None, False),
            (bottom, None, False),
        ])
        combinelist = [(ow_canv, None, True, ow_size[0]),
                       (sb_canv, None, False, sb_widt)]
        if self._scrollbar_side != SCROLLBAR_LEFT:
            return urwid.CanvasJoin(combinelist)
        else:
            return urwid.CanvasJoin(reversed(combinelist))
Example #12
0
 def render(self, size, focus=False):
     return urwid.SolidCanvas(' ', size[0], self.rows(size, focus))
Example #13
0
 def render(self, size, focus=False):
     return urwid.SolidCanvas(" ", *size, 0)
Example #14
0
    def render(self, size, focus=False):
        clamp = lambda _min, _max, v: min(_max, max(_min, v))

        (maxcol, ) = size
        if self._recent is None:
            # No data; display nothing
            return ur.SolidCanvas('╌', maxcol, 1)
        min_label = '{label:>{width}}'.format(label=self._format(
            self._minimum),
                                              width=self.label_width)
        max_label = '{label:<{width}}'.format(label=self._format(
            self._maximum),
                                              width=self.label_width)

        bar_range = self._maximum - self._minimum
        if not bar_range:
            # Minimum and maximum are equal; display nothing
            return ur.SolidCanvas('╌', maxcol, 1)

        while True:
            bar_len = maxcol - sum(
                len(s) for s in (min_label, self.left, self.right, max_label))
            bar_len //= len(self.back)
            if bar_len > 4:
                break
            else:
                # Bar is too short to be useful; if the minimum and maximum are
                # trivial attempt to eliminate their labels and if this isn't
                # enough just display >>>>
                if self._minimum == 0:
                    if min_label != '':
                        min_label = ''
                        continue
                    elif self._maximum in (1, 100) and max_label != '':
                        max_label = ''
                        continue
                return ur.SolidCanvas('▸', maxcol, 1)

        pre_len = clamp(
            0, bar_len,
            round(bar_len *
                  ((min(self._recent, self._history) - self._minimum) /
                   bar_range)))
        post_len = clamp(
            0, bar_len,
            round(bar_len *
                  ((self._maximum - max(self._recent, self._history)) /
                   bar_range)))
        trend_len = bar_len - (pre_len + post_len)
        while trend_len < 0:
            # Can happen by rounding; knock 1 off the first largest value.
            trend_len += 1
            if pre_len >= post_len:
                pre_len -= 1
            else:
                post_len -= 1

        s = ''.join((
            self.fore * pre_len,
            (self.falling if self._recent < self._history else
             self.rising if self._recent > self._history else self.fore) *
            trend_len,
            self.back * post_len,
        ))
        if self.show_current:
            latest_pos = clamp(
                0, bar_len - 1,
                round(bar_len * ((self._latest - self._minimum) / bar_range)))
            s = s[:latest_pos] + self.current + s[latest_pos + 1:]
        s = ''.join((min_label, self.left, s, self.right, max_label))
        text, cs = ur.apply_target_encoding(s)
        return ur.TextCanvas([text], [cs], maxcol=maxcol)
Example #15
0
    def render(self, size, focus=False):
        """Render listbox and return canvas. """
        (maxcol, maxrow) = size

        middle, top, bottom = self.calculate_visible((maxcol, maxrow),
                                                     focus=focus)

        if middle is None:
            return urwid.SolidCanvas(" ", maxcol, maxrow)

        _ignore, focus_widget, focus_pos, focus_rows, cursor = middle
        trim_top, fill_above = top
        trim_bottom, fill_below = bottom

        if bottom[1]:
            self._bottom_pos = bottom[1][-1][1]
        else:
            self._bottom_pos = None
        if top[1]:
            self._top_pos = top[1][-1][1]
        else:
            self._top_pos = None
        self._focus_pos = focus_pos

        combinelist = []
        rows = 0
        fill_above.reverse()  # fill_above is in bottom-up order

        for widget, w_pos, w_rows in fill_above:
            canvas = widget.render((maxcol, ))
            attr = self.get_row_attr(w_pos)
            if attr:
                canvas = urwid.CompositeCanvas(canvas)
                canvas.fill_attr(attr)

            if w_rows != canvas.rows():
                raise urwid.ListBoxError, BADROWSMSG % ( ` widget `, ` w_pos `,
                                                         w_rows, canvas.rows())
            rows += w_rows
            combinelist.append((canvas, w_pos, False))

        focus_canvas = focus_widget.render((maxcol, ), focus=focus)

        focus_attr = None
        if focus_pos in self.row_attrs:
            focus_attr = self.get_row_attr(focus_pos)
            if focus and self.focus_str:
                focus_attr += self.focus_str
        elif focus:
            focus_attr = self.focus_attr

        if focus_attr:
            focus_canvas = urwid.CompositeCanvas(focus_canvas)
            focus_canvas.fill_attr(focus_attr)

        if focus_canvas.rows() != focus_rows:
            raise ListBoxError, BADFOCUSROWSMSG % ( ` focus_widget `, `
                                                    focus_pos `, focus_rows,
                                                    focus_canvas.rows())
        c_cursor = focus_canvas.cursor
        if cursor != c_cursor:
            raise urwid.ListBoxError, BADCURSORMSG % (
                ` focus_widget `, ` focus_pos `, ` cursor `, ` c_cursor `)

        rows += focus_rows
        combinelist.append((focus_canvas, focus_pos, True))

        for widget, w_pos, w_rows in fill_below:
            canvas = widget.render((maxcol, ))
            attr = self.get_row_attr(w_pos)
            if attr:
                canvas = urwid.CompositeCanvas(canvas)
                canvas.fill_attr(attr)
            if w_rows != canvas.rows():
                raise urwid.ListBoxError, BADROWSMSG % ( ` widget `, ` w_pos `,
                                                         w_rows, canvas.rows())
            rows += w_rows
            combinelist.append((canvas, w_pos, False))

        final_canvas = urwid.CanvasCombine(combinelist)

        if trim_top:
            final_canvas.trim(trim_top)
            rows -= trim_top
        if trim_bottom:
            final_canvas.trim_end(trim_bottom)
            rows -= trim_bottom

        assert rows <= maxrow

        if rows < maxrow:
            bottom_pos = focus_pos
            if fill_below:
                bottom_pos = fill_below[-1][1]
            assert trim_bottom == 0 and self.body.get_next(bottom_pos) == (
                None, None)
            final_canvas.pad_trim_top_bottom(0, maxrow - rows)

        return final_canvas