From e387d138c9087eb6583264defaf55b8391ec36dc Mon Sep 17 00:00:00 2001
From: Seblu <sebastien.luttringer@smartjog.com>
Date: Fri, 25 Feb 2011 11:47:41 +0100
Subject: [PATCH] indexing in list -l and list -t

---
 cccli/command/list.py | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/cccli/command/list.py b/cccli/command/list.py
index 9c51855..9e2e5d9 100644
--- a/cccli/command/list.py
+++ b/cccli/command/list.py
@@ -9,6 +9,7 @@ from cccli.exception import *
 from sjrpc.core.exceptions import *
 from cccli.printer import Printer, color
 from cccli.command.command import TqlCommand
+import math
 
 class Command_list(TqlCommand):
     '''List objects'''
@@ -35,7 +36,7 @@ class Command_list(TqlCommand):
         elif self.options.table:
             self.list_table(objs)
         else:
-            self.print_objects(objs)
+            self.print_objects(objs, index=self.options.index)
 
     def list_align(self, objs):
         '''Listing line aligned'''
@@ -47,9 +48,14 @@ class Command_list(TqlCommand):
         # build initial print order
         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"])))
         # dislay each object by line
-        for o in objs["objects"]:
-            line = ""
+        for (i,o) in enumerate(objs["objects"]):
+            if self.options.index:
+                line = ("[%d]"%i).ljust(indexw + 4)
+            else:
+                line = ""
             for t in order:
                 line += "%s%s:%s%s "%(self.tdtc(t),
                                      t,
@@ -67,14 +73,21 @@ class Command_list(TqlCommand):
         # 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 o in objs["objects"]:
+        for (i,o) in enumerate(objs["objects"]):
             for t in order:
+                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"])
-- 
GitLab