-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.js
221 lines (188 loc) · 4.5 KB
/
components.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const blessed = require("blessed");
const assert = require("assert");
const util = require("./util");
/**
* Create a new column element for mongo-ranger
*
* Wraps blessed.list, and adds the following functions:
* setLevel(level), getItems(), copyFrom(other), moveLevel(delta),
* setKeys(keyList), getKey(index)
*
* @param options.width
* @param options.left
* @param options.right
* @param options.level where 0 is Database, 1 is Collection, 2 is top level of Document, etc
* @param options.search a function to run on key /, accepts a cb which expects a search string
* @returns a column object
*/
function column(options) {
const style = {
item: {
hover: {
bg: "blue",
bold: true
}
},
selected: {
bg: "blue",
bold: true
}
};
const col = blessed.list({
left: options.left,
width: options.width,
right: options.right,
height: "100%-2",
tags: true,
keys: true,
vi: true,
border: {
type: "line"
},
style,
search: options.search
});
// contains the raw keys used to index into the next level
// unlike col.items which contains a human-readable formatted string
col.keys = [];
col.setKeys = keys => {
col.keys = keys;
};
col.getKey = index => {
return col.keys[index];
};
col.setLevel = level => {
assert(level >= util.levels.DATABASE);
col.level = level;
col.setLabel(getLabel(level));
};
col.moveLevel = delta => {
col.setLevel(col.level + delta);
};
col.getItems = () => {
return col.items.map(i => i.content);
};
col.copyFrom = other => {
col.setItems(other.getItems());
col.select(other.selected);
col.setLevel(other.level);
col.setKeys(other.keys);
};
if (options.level != undefined) {
col.setLevel(options.level);
}
col.on("focus", () => {
if (col.level == undefined) {
return;
}
// there doesn't seem to be a good way to do this from `style`
col.setLabel(`{red-fg}{bold}${getLabel(col.level)}{/bold}{/red-fg}`);
});
col.on("blur", () => {
if (col.level == undefined) {
return;
}
// strips formatting
col.setLabel(getLabel(col.level));
});
return col;
}
function getLabel(level) {
switch (level) {
case util.levels.DATABASE:
return "Databases";
case util.levels.COLLECTION:
return "Collections";
case util.levels.DOCUMENT_BASE:
return "Documents";
}
return `Document (Level ${level - util.levels.DOCUMENT_BASE})`;
}
/**
* Returns a log for debugging purposes anchored on the right of the screen.
*/
function logger() {
const logger = blessed.log({
left: "80%",
height: "100%",
label: "Debug Log",
width: "20%",
tags: true,
border: {
type: "line"
}
});
logger.logObject = obj => logger.log(util.stringify(obj));
return logger;
}
/**
* Returns a textbox across the bottom of the screen
* Provides promisified read functions
*/
function input() {
const input = blessed.textbox({
top: "100%-3",
height: 3,
tags: true,
width: "100%",
border: {
type: "line"
}
});
input.getLabelText = () => {
const label = input.children[0];
if (!label) return "";
return blessed.stripTags(label.content);
};
// will not clear errors unless force=true
input.clear = force => {
if (!force && input.getLabelText() === "Error") return;
input.removeLabel();
input.clearValue();
};
const errorDisplayTime = 2000;
input.setError = msg => {
input.setLabel("{red-fg}{bold}Error{/}");
input.setValue(msg);
// error auto disappear after errorDisplayTime
clearTimeout(input.errorTimeout);
input.errorTimeout = setTimeout(() => {
if (input.getLabelText() === "Error") {
input.clear(true);
}
}, errorDisplayTime);
};
input.read = () => {
return new Promise(resolve => {
input.readInput((err, val) => {
if (err) {
return resolve("");
}
resolve(val || "");
});
});
};
input.readObject = async () => {
const val = await input.read();
if (!val) return null;
try {
return util.stringToObject(val);
} catch (e) {
input.setError("Object is malformed, aborting");
return null;
}
};
input.readString = async () => {
const val = await input.read();
if (val[0] === '"' && val[val.length - 1] === '"') {
return val.substr(1, val.length - 2);
}
return val;
};
return input;
}
module.exports = {
column,
logger,
input
};