Parcourir la source

Replace old do_list with a new one

oz123 il y a 10 ans
Parent
commit
38d0133375
3 fichiers modifiés avec 30 ajouts et 64 suppressions
  1. 1 1
      pwman/tests/test_base_ui.py
  2. 0 51
      pwman/ui/base.py
  3. 29 12
      pwman/ui/baseui.py

+ 1 - 1
pwman/tests/test_base_ui.py

@@ -48,7 +48,7 @@ class TestBaseUI(unittest.TestCase):
         self.assertListEqual(['foo', 'bar', 'baz'], tags)
         sys.stdin = sys.__stdin__
 
-    def test_do_newn(self):
+    def test_do_new(self):
         sys.stdin = StringIO(("alice\nsecret\nexample.com\nsome notes"
                               "\nfoo bar baz"))
         _node = self.tester.cli.do_newn('')

+ 0 - 51
pwman/ui/base.py

@@ -420,59 +420,8 @@ class BaseCommands(BaseUI, HelpUI):
                                  reader=reader)
         tagstrings = taglist.split()
         tags = [TagNew(tn) for tn in tagstrings]
-
         return tags
 
-    def do_list(self, args):
-
-        if len(args.split()) > 0:
-            self.do_clear('')
-            self.do_filter(args)
-        try:
-            if sys.platform != 'win32':
-                rows, cols = tools.gettermsize()
-            else:
-                rows, cols = 18, 80  # fix this !
-            nodeids = self._db.listnodes()
-            nodes = self._db.getnodes(nodeids)
-
-            cols -= 8
-            i = 0
-            for n in nodes:
-                tags = n.tags
-                tags = filter(None, tags)
-                tagstring = ''
-                first = True
-                for t in tags:
-                    if not first:
-                        tagstring += ", "
-                    else:
-                        first = False
-                    tagstring += t
-
-                name = "%s@%s" % (n.username, n.url)
-
-                name_len = cols * 2 / 3
-                tagstring_len = cols / 3
-                if len(name) > name_len:
-                    name = name[:name_len - 3] + "..."
-                if len(tagstring) > tagstring_len:
-                    tagstring = tagstring[:tagstring_len - 3] + "..."
-                fmt = "%%5d. %%-%ds %%-%ds" % (name_len, tagstring_len)
-                formatted_entry = tools.typeset(fmt % (n._id,
-                                                name, tagstring),
-                                                Fore.YELLOW, False)
-                print (formatted_entry)
-                i += 1
-                if i > rows - 2:
-                    i = 0
-                    c = tools.getonechar("Press <Space> for more,"
-                                         " or 'Q' to cancel")
-                    if c.lower() == 'q':
-                        break
-
-        except Exception as e:
-            self.error(e)
 
     def do_filter(self, args):
         tagstrings = args.split()

+ 29 - 12
pwman/ui/baseui.py

@@ -32,7 +32,6 @@ from pwman.data.nodes import NewNode, Node
 from pwman.ui.tools import CMDLoop
 import getpass
 from pwman.data.tags import TagNew
-import csv
 
 if sys.version_info.major > 2:
     raw_input = input
@@ -92,10 +91,30 @@ class BaseCommands(HelpUI):
         tags = [tn for tn in tagstrings]
         return tags
 
-    def do_listn(self, args):
-        """list all existing nodes in database"""
+    def _prep_term(self, args):
         self.do_cls('')
-        self.do_filter(args)
+        # TODO: fix do_filter!
+        #self.do_filter(args)
+        if sys.platform != 'win32':
+            rows, cols = tools.gettermsize()
+        else:
+            rows, cols = 18, 80  # fix this !
+
+        cols -= 8
+        return rows, cols
+
+    def _print_node_line(self, node, rows, cols):
+        tagstring = ','.join([str(t) for t in node.tags])
+        fmt = ('{nid}. Username: {name:<{f}} URL: {url:<{f}} Tags: {tags:<{f}}'
+               ''.format(url=node.url + ',', name=node.username + ',', f=10,
+                         tags=tagstring, nid=node._id))
+        formatted_entry = tools.typeset(fmt, Fore.YELLOW, False)
+        print(formatted_entry)
+
+    def do_list(self, args):
+        """list all existing nodes in database"""
+        rows, cols = self._prep_term(args)
+
         nodeids = self._db.listnodes()
         nodes = self._db.getnodes(nodeids)
         _nodes_inst = []
@@ -103,15 +122,13 @@ class BaseCommands(HelpUI):
         for node in nodes:
             _nodes_inst.append(Node.from_encrypted_entries(
                 node[1],
-                'xxxxx',
                 node[2],
-                'yyyyy',
-                []))
-        # TOOD: after geting the nodes,
-        #       one needs to get their prospective tags
-        for node in _nodes_inst:
-            print(node)
-
+                node[3],
+                node[4],
+                node[5:]))
+        for idx, node in enumerate(_nodes_inst):
+            node._id = idx + 1
+            self._print_node_line(node, rows, cols)
 
     def do_filter(self, args):
         pass