From 39331620afe0f1dfddac671780a6c080c4439eff Mon Sep 17 00:00:00 2001
From: Guy Zana <guy@cloudius-systems.com>
Date: Thu, 9 May 2013 17:32:07 +0300
Subject: [PATCH] cli: add a tab_pretty() function to a cli command interface

the tab_pretty(arg) function is called just before printing an
autocompleted suggestion, it may be used by commands to prettify their
autocompletion output, will shortly be used by the cd command
---
 console/autocomplete.js | 17 +++++++++++++----
 console/cli.js          |  4 ++--
 documentation/cli.txt   |  6 ++++++
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/console/autocomplete.js b/console/autocomplete.js
index ea0c2285c..887ca9ba7 100644
--- a/console/autocomplete.js
+++ b/console/autocomplete.js
@@ -1,7 +1,16 @@
 // Pretty print suggestions
-function write_suggestions(arr)
+function write_suggestions(arr, command)
 {
-    write_string("\n" + arr.join("\t") + "\n");
+    write_char('\n');
+    for (var i=0; i < arr.length; i++) {
+        var suggestion = arr[i];
+        if ((command != undefined) && (command.tab_pretty)) {
+            suggestion = command.tab_pretty(suggestion);
+        }
+        write_string(suggestion);
+        write_char('\t');
+    }
+    write_char('\n');
     flush();
 }
 
@@ -33,7 +42,7 @@ function autocomplete(word)
     }
 }
 
-function get_suggestions(arr, partial)
+function get_suggestions(arr, partial, command)
 {
     // Find suggestions
     var suggestions = new Array();
@@ -49,7 +58,7 @@ function get_suggestions(arr, partial)
         return (true);
     } else if (suggestions.length > 1) {
         // FIXME: Find a common prefix for all suggestions if possible
-        write_suggestions(suggestions);
+        write_suggestions(suggestions, command);
     } else {
         beep();
     }
diff --git a/console/cli.js b/console/cli.js
index 76da89eb7..0c741099d 100644
--- a/console/cli.js
+++ b/console/cli.js
@@ -166,9 +166,9 @@ function tab()
             var last_idx = inp.length-1;
             var suggested = false;
             if (last_idx == 0) {
-                suggested = get_suggestions(results, "");
+                suggested = get_suggestions(results, "", cmd);
             } else {
-                suggested = get_suggestions(results, inp[last_idx]);
+                suggested = get_suggestions(results, inp[last_idx], cmd);
             }
             
             if (cmd.tab_final) {
diff --git a/documentation/cli.txt b/documentation/cli.txt
index 15fa38a7b..f98de6ed3 100644
--- a/documentation/cli.txt
+++ b/documentation/cli.txt
@@ -91,6 +91,12 @@ tab_final(found_match)
                 called after tab has completed and possibly a single match have 
                 been found (tab completed), see cd.ls for an example
 
+tab_pretty(arg)
+
+                called just before printing an autocompleted suggestion, 
+                used by commands to prettify their autocompletion output, 
+                used by the cd command
+
 help()          should print a helpful description for the command, called when 
                 the user execute "help cmd"
 
-- 
GitLab