From 4217d216c58b3c8f3d5941855b007d61ee32dec0 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:32:06 +0200
Subject: [PATCH] Modify the display list_table

---
 cccli/command/list.py | 56 +++++++++++++++++++++++++++++++------------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/cccli/command/list.py b/cccli/command/list.py
index 3244267..6183dfa 100644
--- a/cccli/command/list.py
+++ b/cccli/command/list.py
@@ -77,32 +77,58 @@ class Command_list(TqlCommand):
 
     def list_table(self, objs):
         '''Listing table style'''
+        term_height, term_width = self.printer.get_term_size()
         # get max size by tag
         tags = dict()
         for o in objs["objects"]:
             for (t,v) in o.items():
                 tags[t] = max(len(self.tdr(t, v)), tags.get(t, len(t)))
+        size_id = tags["id"] + len(" ")
+        # compute index width
+        indexw = max(int(math.log10(len(objs["objects"]))+1), len("index "))
+        if size_id + indexw >= term_width:
+            raise cmdError("term width is too small")
         # build initial print order
         order = [ t for t in objs.get("order", []) if t in tags ]
         order.extend(sorted(set(tags.keys()) - set(order)))
-        if self.options.index:
-            # compute index width
-            indexw = max(int(math.log10(len(objs["objects"]))+1), len("index "))
-            # print index title
-            self.printer.out("index ", nl="")
-        # print tag title in order
-        for t in order:
-            self.printer.out(self.tdtc(t), nl="")
-            self.printer.out(t.ljust(tags[t]), nl=" ")
-        self.printer.out(color["reset"])
-        # print tags in order
-        for (i,o) in enumerate(objs["objects"]):
+        # remove id of order
+        order = order[1:]
+        # print tag table by title group
+        while order:
+            tag_index = 0
+            line_pos = 0
+            if self.options.index:
+                # print index title
+                self.printer.out("index ", nl="")
+                line_pos = indexw
+            # print id title
+            self.printer.out(self.tdtc("id") + "id".ljust(tags["id"]), nl=" ")
+            line_pos += size_id
+            # print tags title section in order
             for t in order:
+                # if the tag don't fit in space left, stop the title loop
+                if line_pos + tags[t] + len(" ") > term_width and tag_index != 0:
+                    break
+                self.printer.out(self.tdtc(t) + t.ljust(tags[t]), nl=" ")
+                line_pos += tags[t] + len(" ")
+                tag_index += 1
+            self.printer.out(color["reset"])
+            # print tags corresponding to the title
+            for (i,o) in enumerate(objs["objects"]):
+                line_pos = 0
                 if self.options.index:
                     self.printer.out(("%d "%i).ljust(indexw), nl="")
-                self.printer.out(self.tdc(t), nl="")
-                self.printer.out(self.tdr(t, o.get(t, u"")).ljust(tags[t]) ,nl=" ")
-            self.printer.out(color["reset"])
+                    line_pos += indexw
+                # print id value
+                self.printer.out(self.tdc("id") + self.tdr("id", o.get("id", u"")).ljust(tags["id"]), nl=" ")
+                line_pos += size_id
+                # print tag value
+                for t in order[:tag_index]:
+                    self.printer.out(self.tdc(t), nl="")
+                    buf, line_pos = self._format_indent_text(self.tdr(t, o.get(t, u"")).ljust(tags[t]) + " ", line_pos, size_id, term_width)
+                    self.printer.out(buf, nl="")
+                self.printer.out(color["reset"])
+            order = order[tag_index:]
 
     def list_vertical(self, objs):
         '''Vertical display'''
-- 
GitLab