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

Modify the display list_align

parent 4217d216
Loading
Loading
Loading
Loading
+34 −2
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ class Command_list(TqlCommand):

    def list_align(self, objs):
        '''Listing line aligned'''
        term_height, term_width = self.printer.get_term_size()
        # get max size by tag
        tags = dict()
        for o in objs["objects"]:
@@ -61,18 +62,49 @@ class Command_list(TqlCommand):
        order = [ t for t in objs.get("order", []) if t in tags ]
        order.extend(sorted(set(tags.keys()) - set(order)))
        # compute index width
        indexw = int(math.log10(len(objs["objects"])))
        indexw = int(math.log10(len(objs["objects"]))) + 1
        # dislay each object by line
        for (i,o) in enumerate(objs["objects"]):
            line_pos = 0
            num_pos = 0
            # tag position on the line
            pos = dict()
            if self.options.index:
                line = ("[%d]"%i).ljust(indexw + 4)
                line = ("[%d]"%i).ljust(indexw + 3)
                line_pos = len(line)
            else:
                line = ""
            # variable for create a newline
            new_line = False
            first_line = True
            for t in order:
                # if tag doesn't fit into the space left, newline
                if line_pos + len(t) + len(":") + tags[t] + len(" ") >= term_width or new_line:
                    line += os.linesep
                    line_pos = 0
                    num_pos = 0
                    new_line = False
                    first_line = False
                line += "%s%s:%s%s "%(self.tdtc(t),
                                      t,
                                      self.tdc(t),
                                      self.tdr(t, o.get(t, u"")).ljust(tags[t]))
                pos[num_pos] = line_pos
                line_pos += len(t) + len(":") + tags[t] + len(" ")
                num_pos += 1
                # align the next tag on the tag above and right
                tmp_pos = num_pos
                found_pos = False
                while tmp_pos in pos:
                    # if the tag position above is greater than the line position, add space to align the next tag
                    if pos[tmp_pos] > line_pos:
                        line += " " * (pos[tmp_pos] - line_pos)
                        line_pos = pos[tmp_pos]
                        found_pos = True
                        break
                    else:
                        tmp_pos += 1
                new_line = not found_pos and not first_line
            self.printer.out("%s%s"%(line, color["reset"]))

    def list_table(self, objs):