OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart2js_info.src.table; | |
Siggi Cherem (dart-lang)
2015/10/02 17:16:17
I seem to have forgotten to include this in the fi
| |
6 | |
7 import 'dart:math' show max; | |
8 | |
9 /// Helper class to present data on the command-line in a table form. | |
10 class Table { | |
11 int _totalColumns = 0; | |
12 int get totalColumns => _totalColumns; | |
13 | |
14 /// Abbreviations, used to make headers shorter. | |
15 Map<String, String> abbreviations = {}; | |
16 | |
17 /// Width of each column. | |
18 List<int> widths = <int>[]; | |
19 | |
20 /// The header for each column (`header.length == totalColumns`). | |
21 List header = []; | |
22 | |
23 /// The color for each column (`color.length == totalColumns`). | |
24 List colors = []; | |
25 | |
26 /// Each row on the table. Note that all rows have the same size | |
27 /// (`rows[*].length == totalColumns`). | |
28 List<List> rows = []; | |
29 | |
30 /// Columns to skip, for example, if they are all zero entries. | |
31 List<bool> _skipped = <bool>[]; | |
32 | |
33 /// Whether we started adding entries. Indicates that no more columns can be | |
34 /// added. | |
35 bool _sealed = false; | |
36 | |
37 /// Current row being built by [addEntry]. | |
38 List _currentRow; | |
39 | |
40 /// Add a column with the given [name]. | |
41 void declareColumn(String name, | |
42 {bool abbreviate: false, String color: _NO_COLOR}) { | |
43 assert(!_sealed); | |
44 var headerName = name; | |
45 if (abbreviate) { | |
46 // abbreviate the header by using only the initials of each word | |
47 headerName = | |
48 name.split(' ').map((s) => s.substring(0, 1).toUpperCase()).join(''); | |
49 while (abbreviations[headerName] != null) headerName = "$headerName'"; | |
50 abbreviations[headerName] = name; | |
51 } | |
52 widths.add(max(5, headerName.length + 1)); | |
53 header.add(headerName); | |
54 colors.add(color); | |
55 _skipped.add(_totalColumns > 0); | |
56 _totalColumns++; | |
57 } | |
58 | |
59 /// Add an entry in the table, creating a new row each time [totalColumns] | |
60 /// entries are added. | |
61 void addEntry(entry) { | |
62 if (_currentRow == null) { | |
63 _sealed = true; | |
64 _currentRow = []; | |
65 } | |
66 int pos = _currentRow.length; | |
67 assert(pos < _totalColumns); | |
68 | |
69 widths[pos] = max(widths[pos], '$entry'.length + 1); | |
70 _currentRow.add('$entry'); | |
71 if (entry is int && entry != 0) { | |
72 _skipped[pos] = false; | |
73 } | |
74 | |
75 if (pos + 1 == _totalColumns) { | |
76 rows.add(_currentRow); | |
77 _currentRow = []; | |
78 } | |
79 } | |
80 | |
81 /// Add an empty row to divide sections of the table. | |
82 void addEmptyRow() { | |
83 var emptyRow = []; | |
84 for (int i = 0; i < _totalColumns; i++) { | |
85 emptyRow.add('-' * widths[i]); | |
86 } | |
87 rows.add(emptyRow); | |
88 } | |
89 | |
90 /// Enter the header titles. OK to do so more than once in long tables. | |
91 void addHeader() { | |
92 rows.add(header); | |
93 } | |
94 | |
95 /// Generates a string representation of the table to print on a terminal. | |
96 // TODO(sigmund): add also a .csv format | |
97 String toString() { | |
98 var sb = new StringBuffer(); | |
99 sb.write('\n'); | |
100 for (var row in rows) { | |
101 var lastColor = _NO_COLOR; | |
102 for (int i = 0; i < _totalColumns; i++) { | |
103 if (_skipped[i]) continue; | |
104 var entry = row[i]; | |
105 var color = colors[i]; | |
106 if (lastColor != color) { | |
107 sb.write(color); | |
108 lastColor = color; | |
109 } | |
110 // Align first column to the left, everything else to the right. | |
111 sb.write( | |
112 i == 0 ? entry.padRight(widths[i]) : entry.padLeft(widths[i] + 1)); | |
113 } | |
114 if (lastColor != _NO_COLOR) sb.write(_NO_COLOR); | |
115 sb.write('\n'); | |
116 } | |
117 sb.write('\nWhere:\n'); | |
118 for (var id in abbreviations.keys) { | |
119 sb.write(' $id:'.padRight(7)); | |
120 sb.write(' ${abbreviations[id]}\n'); | |
121 } | |
122 return sb.toString(); | |
123 } | |
124 } | |
125 | |
126 const _NO_COLOR = "\x1b[0m"; | |
OLD | NEW |