1810
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <string> |
|
28 |
|
29 #include <iostream.h> |
|
30 |
|
31 #include "oct-term.h" |
|
32 #include "str-vec.h" |
|
33 |
2493
|
34 // Create a string vector from a NULL terminated list of C strings. |
|
35 |
|
36 string_vector::string_vector (const char * const *s) |
|
37 : Array<string> () |
|
38 { |
|
39 int n = 0; |
|
40 |
|
41 while (*s++) |
|
42 n++; |
|
43 |
|
44 resize (n); |
|
45 |
|
46 for (int i = 0; i < n; i++) |
|
47 elem (i) = s[i]; |
|
48 } |
|
49 |
|
50 // Create a string vector from up to N C strings. Assumes that N is |
|
51 // nonnegative. |
|
52 |
|
53 string_vector::string_vector (const char * const *s, int n) |
|
54 : Array<string> (n) |
|
55 { |
|
56 for (int i = 0; i < n; i++) |
|
57 elem (i) = s[i]; |
|
58 } |
|
59 |
1810
|
60 // Format a list in neat columns. Mostly stolen from GNU ls. |
|
61 |
|
62 ostream& |
|
63 string_vector::list_in_columns (ostream& os) const |
|
64 { |
|
65 // Compute the maximum name length. |
|
66 |
|
67 int max_name_length = 0; |
|
68 int total_names = length (); |
|
69 |
|
70 for (int i = 0; i < total_names; i++) |
|
71 { |
|
72 int name_length = elem (i).length (); |
|
73 if (name_length > max_name_length) |
|
74 max_name_length = name_length; |
|
75 } |
|
76 |
|
77 // Allow at least two spaces between names. |
|
78 |
|
79 max_name_length += 2; |
|
80 |
|
81 // Calculate the maximum number of columns that will fit. |
|
82 |
|
83 int line_length = terminal_columns (); |
|
84 int cols = line_length / max_name_length; |
|
85 if (cols == 0) |
|
86 cols = 1; |
|
87 |
|
88 // Calculate the number of rows that will be in each column except |
|
89 // possibly for a short column on the right. |
|
90 |
|
91 int rows = total_names / cols + (total_names % cols != 0); |
|
92 |
|
93 // Recalculate columns based on rows. |
|
94 |
|
95 cols = total_names / rows + (total_names % rows != 0); |
|
96 |
|
97 int count; |
|
98 for (int row = 0; row < rows; row++) |
|
99 { |
|
100 count = row; |
|
101 int pos = 0; |
|
102 |
|
103 // Print the next row. |
|
104 |
|
105 while (1) |
|
106 { |
|
107 string nm = elem (count); |
|
108 |
|
109 os << nm; |
|
110 int name_length = nm.length (); |
|
111 |
|
112 count += rows; |
|
113 if (count >= total_names) |
|
114 break; |
|
115 |
|
116 int spaces_to_pad = max_name_length - name_length; |
|
117 for (int i = 0; i < spaces_to_pad; i++) |
|
118 os << " "; |
|
119 pos += max_name_length; |
|
120 } |
|
121 os << "\n"; |
|
122 } |
|
123 |
|
124 return os; |
|
125 } |
|
126 |
|
127 /* |
|
128 ;;; Local Variables: *** |
|
129 ;;; mode: C++ *** |
|
130 ;;; End: *** |
|
131 */ |