OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Helper that binds the |this| object to a method to create a callback. | 6 * Helper that binds the |this| object to a method to create a callback. |
7 */ | 7 */ |
8 Function.prototype.bind = function(thisObj) { | 8 Function.prototype.bind = function(thisObj) { |
9 var func = this; | 9 var func = this; |
10 var args = Array.prototype.slice.call(arguments, 1); | 10 var args = Array.prototype.slice.call(arguments, 1); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 node.className = currentNames.join(" "); | 97 node.className = currentNames.join(" "); |
98 } | 98 } |
99 | 99 |
100 function getKeyWithValue(map, value) { | 100 function getKeyWithValue(map, value) { |
101 for (key in map) { | 101 for (key in map) { |
102 if (map[key] == value) | 102 if (map[key] == value) |
103 return key; | 103 return key; |
104 } | 104 } |
105 return '?'; | 105 return '?'; |
106 } | 106 } |
| 107 |
| 108 /** |
| 109 * Builds a string by repeating |str| |count| times. |
| 110 */ |
| 111 function makeRepeatedString(str, count) { |
| 112 var out = []; |
| 113 for (var i = 0; i < count; ++i) |
| 114 out.push(str); |
| 115 return out.join(''); |
| 116 } |
| 117 |
| 118 /** |
| 119 * TablePrinter is a helper to format a table as ascii art. |
| 120 * |
| 121 * Usage: call addRow() and addCell() repeatedly to specify the data. Ones |
| 122 * all the fields have been inputted, call toText() to format it as text. |
| 123 */ |
| 124 function TablePrinter() { |
| 125 this.rows_ = []; |
| 126 } |
| 127 |
| 128 function TablePrinterCell(value) { |
| 129 this.text = '' + value; |
| 130 this.alignRight = false; |
| 131 this.allowOverflow = false; |
| 132 } |
| 133 |
| 134 /** |
| 135 * Starts a new row. |
| 136 */ |
| 137 TablePrinter.prototype.addRow = function() { |
| 138 this.rows_.push([]); |
| 139 }; |
| 140 |
| 141 /** |
| 142 * Adds a column to the current row, setting its value to cellText. |
| 143 * |
| 144 * @returns {!TablePrinterCell} the cell that was added. |
| 145 */ |
| 146 TablePrinter.prototype.addCell = function(cellText) { |
| 147 var r = this.rows_[this.rows_.length - 1]; |
| 148 var cell = new TablePrinterCell(cellText); |
| 149 r.push(cell); |
| 150 return cell; |
| 151 }; |
| 152 |
| 153 /** |
| 154 * Returns the maximum number of columns this table contains. |
| 155 */ |
| 156 TablePrinter.prototype.getNumColumns = function() { |
| 157 var numColumns = 0; |
| 158 for (var i = 0; i < this.rows_.length; ++i) { |
| 159 numColumns = Math.max(numColumns, this.rows_[i].length); |
| 160 } |
| 161 return numColumns; |
| 162 } |
| 163 |
| 164 /** |
| 165 * Returns the cell at position (rowIndex, columnIndex), or null if there is |
| 166 * no such cell. |
| 167 */ |
| 168 TablePrinter.prototype.getCell_ = function(rowIndex, columnIndex) { |
| 169 if (rowIndex >= this.rows_.length) |
| 170 return null; |
| 171 var row = this.rows_[rowIndex]; |
| 172 if (columnIndex >= row.length) |
| 173 return null; |
| 174 return row[columnIndex]; |
| 175 }; |
| 176 |
| 177 /** |
| 178 * Returns a formatted text representation of the table data. |
| 179 */ |
| 180 TablePrinter.prototype.toText = function() { |
| 181 var numRows = this.rows_.length; |
| 182 var numColumns = this.getNumColumns(); |
| 183 |
| 184 // Figure out the maximum width of each column. |
| 185 var columnWidths = []; |
| 186 columnWidths.length = numColumns; |
| 187 for (var i = 0; i < numColumns; ++i) |
| 188 columnWidths[i] = 0; |
| 189 |
| 190 for (var c = 0; c < numColumns; ++c) { |
| 191 for (var r = 0; r < numRows; ++r) { |
| 192 var cell = this.getCell_(r, c); |
| 193 if (cell && !cell.allowOverflow) { |
| 194 columnWidths[c] = Math.max(columnWidths[c], cell.text.length); |
| 195 } |
| 196 } |
| 197 } |
| 198 |
| 199 // Print each row. |
| 200 var out = []; |
| 201 for (var r = 0; r < numRows; ++r) { |
| 202 for (var c = 0; c < numColumns; ++c) { |
| 203 var cell = this.getCell_(r, c); |
| 204 if (cell) { |
| 205 // Padd the cell with spaces to make it fit the maximum column width. |
| 206 var padding = columnWidths[c] - cell.text.length; |
| 207 var paddingStr = makeRepeatedString(' ', padding); |
| 208 |
| 209 if (cell.alignRight) { |
| 210 out.push(paddingStr); |
| 211 out.push(cell.text); |
| 212 } else { |
| 213 out.push(cell.text); |
| 214 out.push(paddingStr); |
| 215 } |
| 216 } |
| 217 } |
| 218 out.push('\n'); |
| 219 } |
| 220 |
| 221 return out.join(''); |
| 222 }; |
| 223 |
OLD | NEW |