Commit ae12b0ea authored by Gaëtan Déléaz's avatar Gaëtan Déléaz Committed by Seblu
Browse files

Add vertical display in list command

parent 790052a9
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ class Command_list(TqlCommand):
                        help="column aligment display")
        self.add_option("-l", action="store_true", dest="align",
                        help="line aligment display")
        self.add_option("-v", action="store_true", dest="vertical",
                        help="vertical display")
        self.add_option("-m", action="store_true", dest="mikrotik",
                        help="mikrotik display")
        self.remove_option("--quiet")
@@ -40,6 +42,8 @@ class Command_list(TqlCommand):
            self.list_table(objs)
        elif self.options.mikrotik:
            self.list_mikrotik(objs)
        elif self.options.vertical:
            self.list_vertical(objs)
        else:
            self.print_objects(objs, index=self.options.index)

@@ -100,6 +104,41 @@ class Command_list(TqlCommand):
                self.printer.out(self.tdr(t, o.get(t, u"")).ljust(tags[t]) ,nl=" ")
            self.printer.out(color["reset"])

    def list_vertical(self, objs):
        '''Vertical display'''
        term_height, term_width = self.printer.get_term_size()
        # set margin for tags
        margin = 3
        # build full tag order list:
        tags = set((t for o in objs['objects'] for t in o.keys()))
        order = [t for t in objs.get("order", []) if t in tags]
        order.extend(sorted(tags - set(order)))
        # compute index width
        indexw = int(math.log10(len(objs["objects"]))) + 1
        for (i, o) in enumerate(objs["objects"]):
            line = ""
            if self.options.index:
                # +3 is the len("["), len("]"), len(" ")
                line = ("[%d]"%i).ljust(indexw + 3)
            # write id
            tag = order[0]
            line += "%s%s:%s%s "%(self.tdtc(tag), tag, self.tdc(tag), self.tdr(tag, o.get(tag)))
            line += os.linesep
            for t in order[1:]:
                # write tag
                if o.get(t) is not None:
                    # write tag title
                    buf, line_pos = self._format_indent_text(t + ":", margin, margin, term_width)
                    line += " " * margin + self.tdtc(t) + buf
                    # write tag value
                    buf, line_pos = self._format_indent_text(self.tdr(t, o.get(t)) + " ", line_pos, margin, term_width)
                    line += self.tdc(t)
                    if line_pos != margin:
                        line += (buf + os.linesep)
                    else:
                        line += (buf[:-(margin + 1)] + os.linesep)
            self.printer.out("%s%s"%(line[:-1], color["reset"]))

    def list_mikrotik(self, objs):
        '''Mikrotik style display'''
        term_height, term_width = self.printer.get_term_size()