1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | var chalk = require("chalk"),
|
15 | fs = require("fs");
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var utils = module.exports = {};
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | utils.pad = function(str, len) {
|
37 | while (str.length < len) str += " ";
|
38 | return str;
|
39 | };
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | utils.showGrid = function(grid, options) {
|
55 |
|
56 | var str, i, j, m, n;
|
57 |
|
58 | options = options || {};
|
59 | if ("undefined" === typeof options.sort) options.sort = true;
|
60 |
|
61 | if (options.sort)
|
62 | grid.sort(function(a, b) {
|
63 | return (a[0] < b[0]) ? -1 : (a[0] > b[0]) ? 1 : 0;
|
64 | });
|
65 |
|
66 | console.log("");
|
67 |
|
68 | if (options.title) {
|
69 | console.log(" " + chalk.cyan(options.title));
|
70 | console.log("");
|
71 | }
|
72 |
|
73 | if (options.headers) {
|
74 | str = " ";
|
75 | for (j = 0, m = options.headers.length; j < m; j++)
|
76 | str += utils.pad(options.headers[j], options.len);
|
77 | console.log(chalk.yellow(str));
|
78 | }
|
79 |
|
80 | for (i = 0, n = grid.length; i < n; i++) {
|
81 | str = " ";
|
82 | for (j = 0, m = grid[i].length; j < m; j++)
|
83 | str += utils.pad(grid[i][j], options.len);
|
84 | console.log(str);
|
85 | }
|
86 |
|
87 | console.log("");
|
88 |
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | utils.readFile = function(p) {
|
102 | return require("fs").readFileSync(p, "utf8");
|
103 | };
|