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