From ae12b0ead17471a832178783214589944cf3b417 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=C3=ABtan=20D=C3=A9l=C3=A9az?=
 <gaetan.deleaz@smartjog.com>
Date: Fri, 13 May 2011 18:30:51 +0200
Subject: [PATCH] Add vertical display in list command

---
 cccli/command/list.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/cccli/command/list.py b/cccli/command/list.py
index ebb4454..3244267 100644
--- a/cccli/command/list.py
+++ b/cccli/command/list.py
@@ -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()
-- 
GitLab