# HG changeset patch # User John W. Eaton # Date 1341517446 14400 # Node ID afa5e1d20671aebac8e3aef59ed1b3d7b9e5a92b # Parent 355ff41f3f97daf526c1614edafbf94db123860d * str-vec.h, str-vec.cc (string_vector::list_in_columns): Accept prefix. diff --git a/liboctave/str-vec.cc b/liboctave/str-vec.cc --- a/liboctave/str-vec.cc +++ b/liboctave/str-vec.cc @@ -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); diff --git a/liboctave/str-vec.h b/liboctave/str-vec.h --- a/liboctave/str-vec.h +++ b/liboctave/str-vec.h @@ -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 diff --git a/src/strfns.cc b/src/strfns.cc --- a/src/strfns.cc +++ b/src/strfns.cc @@ -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 list_in_columns (["abc", "def"], "a") */