Exemplo n.º 1
0
 def render(self):
     self.draw()
     if not self._interactive:
         return
     old_handler = register_onresize(self._onresize)
     try:
         for ch in datatable.utils.terminal.wait_for_keypresses(0.5):
             if not ch:
                 # Signal handler could have invalidated interactive mode
                 # of the widget -- in which case we need to stop rendering
                 if not self._interactive:
                     break
                 else:
                     continue
             uch = ch.name if ch.is_sequence else ch.upper()
             if self._jump_string is None:
                 if uch == "Q" or uch == "KEY_ESCAPE": break
                 if uch in DataFrameWidget._MOVES:
                     DataFrameWidget._MOVES[uch](self)
             else:
                 if uch in {"Q", "KEY_ESCAPE", "KEY_ENTER"}:
                     self._jump_string = None
                     self.draw()
                 elif uch == "KEY_DELETE" or uch == "KEY_BACKSPACE":
                     self._jump_to(self._jump_string[:-1])
                 elif uch in "0123456789:":
                     self._jump_to(self._jump_string + uch)
     except KeyboardInterrupt:
         pass
     register_onresize(old_handler)
     print(term.move_x(0) + term.clear_eol)
Exemplo n.º 2
0
    def draw(self):
        data = self._fetch_data()
        colnames = data["names"]
        coltypes = data["types"]
        stypes = data["stypes"]
        coldata = data["columns"]
        indices = data["indices"]

        # Create column with row indices
        oldwidth = self._colwidths.get("index", 3)
        indexcolumn = _Column(name="", ctype="str", data=indices)
        indexcolumn.color = term.bright_black
        indexcolumn.margin = "  "
        indexcolumn.width = max(oldwidth, indexcolumn.width)
        self._colwidths["index"] = indexcolumn.width

        # Data columns
        columns = [indexcolumn]
        for i in range(self._view_ncols):
            if self._show_types == 1:
                name = term.green(coltypes[i])
            elif self._show_types == 2:
                name = term.cyan(stypes[i])
            else:
                name = colnames[i]
            oldwidth = self._colwidths.get(i + self._view_col0, 0)
            col = _Column(name=name, ctype=coltypes[i], data=coldata[i])
            col.width = max(col.width, oldwidth)
            if self._view_ncols < self._frame_ncols:
                col.width = min(col.width, _Column.MAX_WIDTH)
            self._colwidths[i + self._view_col0] = col.width
            columns.append(col)
        columns[-1].margin = ""

        # Adjust widths of columns
        total_width = sum(col.width + len(col.margin) for col in columns)
        extra_space = term.width - total_width - DataFrameWidget.RIGHT_MARGIN
        if extra_space > 0:
            if self._view_col0 + self._view_ncols < self._frame_ncols:
                self._view_ncols += max(1, extra_space // 8)
                return self.draw()
            elif self._view_col0 > 0:
                w = self._fetch_column_width(self._view_col0 - 1)
                if w + 2 <= extra_space:
                    self._view_col0 -= 1
                    self._view_ncols += 1
                    self._max_col0 = self._view_col0
                    return self.draw()
        else:
            if self._max_col0 == self._view_col0:
                self._max_col0 += 1
            available_width = term.width - DataFrameWidget.RIGHT_MARGIN
            for i, col in enumerate(columns):
                col.width = min(col.width, available_width)
                available_width -= col.width + len(col.margin)
                if available_width <= 0:
                    available_width = 0
                    col.margin = ""
                else:
                    self._view_ncols = i + 1

        # Generate the elements of the display
        grey = term.bright_black
        header = [
            "".join(col.header for col in columns),
            grey("".join(col.divider for col in columns))
        ]
        rows = [
            "".join(col.value(j) for col in columns)
            for j in range(self._view_nrows)
        ]
        srows = plural_form(self._frame_nrows, "row")
        scols = plural_form(self._frame_ncols, "column")
        footer = ["", "[%s x %s]" % (srows, scols), ""]

        # Display hint about navigation keys
        if self._show_navbar:
            remaining_width = term.width
            if self._jump_string is None:
                nav_elements = [
                    grey("Press") + " q " + grey("to quit"),
                    "  ↑←↓→ " + grey("to move"), "  wasd " + grey("to page"),
                    "  t " + grey("to toggle types"), "  g " + grey("to jump")
                ]
                for elem in nav_elements:
                    l = term.length(elem)
                    if l > remaining_width:
                        break
                    remaining_width -= l
                    footer[2] += elem
            else:
                footer[2] = grey("Go to (row:col): ") + self._jump_string

        # Render the table
        lines = header + rows + footer
        out = (term.move_x(0) + term.move_up * self._n_displayed_lines +
               (term.clear_eol + "\n").join(lines) + term.clear_eol)
        print(out, end="")
        self._n_displayed_lines = len(lines) - 1