changeset 14841:afa5e1d20671

* str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix.
author John W. Eaton <jwe@octave.org>
date Thu, 05 Jul 2012 15:44:06 -0400
parents 355ff41f3f97
children aa4c25ca498e
files liboctave/str-vec.cc liboctave/str-vec.h src/strfns.cc
diffstat 3 files changed, 65 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/str-vec.cc	Wed Jul 04 13:38:37 2012 -0400
+++ b/liboctave/str-vec.cc	Thu Jul 05 15:44:06 2012 -0400
@@ -212,7 +212,8 @@
 // Format a list in neat columns.
 
 std::ostream&
-string_vector::list_in_columns (std::ostream& os, int width) const
+string_vector::list_in_columns (std::ostream& os, int width,
+                                const std::string& prefix) const
 {
   // Compute the maximum name length.
 
@@ -241,7 +242,8 @@
   // Calculate the maximum number of columns that will fit.
 
   octave_idx_type line_length
-    = (width <= 0) ? command_editor::terminal_cols () : width;
+    = ((width <= 0 ? command_editor::terminal_cols () : width)
+       - prefix.length ());
 
   octave_idx_type nc = line_length / max_name_length;
   if (nc == 0)
@@ -264,6 +266,8 @@
 
       // Print the next row.
 
+      os << prefix;
+
       while (1)
         {
           std::string nm = elem (count);
--- a/liboctave/str-vec.h	Wed Jul 04 13:38:37 2012 -0400
+++ b/liboctave/str-vec.h	Thu Jul 05 15:44:06 2012 -0400
@@ -111,7 +111,9 @@
 
   static void delete_c_str_vec (const char * const*);
 
-  std::ostream& list_in_columns (std::ostream&, int width = 0) const;
+  std::ostream&
+  list_in_columns (std::ostream&, int width = 0,
+                   const std::string& prefix = std::string ()) const;
 };
 
 #endif
--- a/src/strfns.cc	Wed Jul 04 13:38:37 2012 -0400
+++ b/src/strfns.cc	Thu Jul 05 15:44:06 2012 -0400
@@ -858,11 +858,13 @@
 
 DEFUN (list_in_columns, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width})\n\
+@deftypefn {Built-in Function} {} list_in_columns (@var{arg}, @var{width}, @var{prefix})\n\
 Return a string containing the elements of @var{arg} listed in\n\
-columns with an overall maximum width of @var{width}.  The argument\n\
-@var{arg} must be a cell array of character strings or a character array.\n\
-If @var{width} is not specified, the width of the terminal screen is used.\n\
+columns with an overall maximum width of @var{width} and optional\n\
+prefix @var{prefix}.  The argument @var{arg} must be a cell array\n\
+of character strings or a character array.  If @var{width} is not\n\
+specified or is an empty matrix, or less than or equal to zero,\n\
+the width of the terminal screen is used.\n\
 Newline characters are used to break the lines in the output string.\n\
 For example:\n\
 @c Set example in small font to prevent overfull line\n\
@@ -893,34 +895,59 @@
 
   int nargin = args.length ();
 
-  if (nargin == 1 || nargin == 2)
+  if (nargin < 1 || nargin > 3)
     {
-      string_vector s = args(0).all_strings ();
+      print_usage ();
+      return retval;
+    }
+
+  string_vector s = args(0).all_strings ();
 
-      if (! error_state)
-        {
-          std::ostringstream buf;
+  if (error_state)
+    {
+      error ("list_in_columns: expecting cellstr or char array");
+      return retval;
+    }
+
+  int width = -1;
+
+  if (nargin > 1 && ! args(1).is_empty ())
+    {
+      width = args(1).int_value ();
 
-          if (nargin == 1)
-            // Let list_in_columns query terminal width.
-            s.list_in_columns (buf);
-          else
-            {
-              int width = args(1).int_value ();
+      if (error_state)
+        {
+          error ("list_in_columns: WIDTH must be an integer");
+          return retval;
+        }
+    }
+                
+  std::string prefix;
 
-              if (! error_state)
-                s.list_in_columns (buf, width);
-              else
-                error ("list_in_columns: WIDTH must be an integer");
+  if (nargin > 2)
+    {
+      if (args(2).is_string ())
+        {
+          prefix = args(2).string_value ();
+
+          if (error_state)
+            {
+              error ("list_in_columns: PREFIX must be a character string");
+              return retval;
             }
-
-          retval = buf.str ();
         }
       else
-        error ("list_in_columns: expecting cellstr or char array");
+        {
+          error ("list_in_columns: PREFIX must be a character string");
+          return retval;
+        }
     }
-  else
-    print_usage ();
+
+  std::ostringstream buf;
+
+  s.list_in_columns (buf, width, prefix);
+
+  retval = buf.str ();
 
   return retval;
 }
@@ -934,8 +961,13 @@
 %! input  = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"];
 %! result = "abc     mnop  \ndef     qrs   \nghijkl  tuv   \n";
 %! assert (list_in_columns (input, 20), result);
+%!test
+%! input  = ["abc"; "def"; "ghijkl"; "mnop"; "qrs"; "tuv"];
+%! result = "  abc     mnop  \n  def     qrs   \n  ghijkl  tuv   \n";
+%! assert (list_in_columns (input, 20, "  "), result);
 
 %!error list_in_columns ()
 %!error list_in_columns (["abc", "def"], 20, 2)
+%!error list_in_columns (["abc", "def"], 20, "  ", 3)
 %!error <invalid conversion from string to real scalar> list_in_columns (["abc", "def"], "a")
 */