@awsless/clui 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.MD +87 -0
- package/dist/index.cjs +62 -47
- package/dist/index.d.cts +18 -11
- package/dist/index.d.ts +18 -11
- package/dist/index.js +65 -50
- package/package.json +5 -4
package/README.MD
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
|
|
2
|
+
# @awsless/clui
|
|
3
|
+
|
|
4
|
+
The @awsless/clui package combines a couple of useful npm packages to create easy to use & beautiful terminal applications.
|
|
5
|
+
|
|
6
|
+
Special thanks to:
|
|
7
|
+
- @clack/prompts
|
|
8
|
+
- cli-table3
|
|
9
|
+
- chalk
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
Install with (NPM):
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm i @awsless/clui
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Basic Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { log, prompt, color } from '@awsless/clui';
|
|
23
|
+
|
|
24
|
+
log.intro(color.blue.bold('Create a blog post'))
|
|
25
|
+
|
|
26
|
+
const title = await prompt.text({ message: 'Title:' })
|
|
27
|
+
const message = await prompt.text({ message: 'Message:' })
|
|
28
|
+
|
|
29
|
+
log.list('Preview post', {
|
|
30
|
+
title,
|
|
31
|
+
message,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const ok = await prompt.confirm({ message: 'Are you sure?' })
|
|
35
|
+
|
|
36
|
+
if(!ok) {
|
|
37
|
+
log.error('Canceled')
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
await log.task({
|
|
42
|
+
initialMessage: 'Creating post...',
|
|
43
|
+
successMessage: 'You blog post was successfully created!',
|
|
44
|
+
async task() {
|
|
45
|
+
// Create your blog post
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
log.outro('The end!')
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Supported Prompts
|
|
54
|
+
|
|
55
|
+
- prompt.text
|
|
56
|
+
- prompt.password
|
|
57
|
+
- prompt.integer
|
|
58
|
+
- prompt.float
|
|
59
|
+
- prompt.confirm
|
|
60
|
+
- prompt.select
|
|
61
|
+
- prompt.multiSelect
|
|
62
|
+
|
|
63
|
+
## Supported Logs
|
|
64
|
+
|
|
65
|
+
- log.intro
|
|
66
|
+
- log.outro
|
|
67
|
+
- log.note
|
|
68
|
+
- log.message
|
|
69
|
+
- log.step
|
|
70
|
+
- log.info
|
|
71
|
+
- log.warning
|
|
72
|
+
- log.error
|
|
73
|
+
- log.success
|
|
74
|
+
- log.list
|
|
75
|
+
- log.table
|
|
76
|
+
- log.task
|
|
77
|
+
|
|
78
|
+
## Supported Ansi Helpers:
|
|
79
|
+
|
|
80
|
+
- ansi.wrap
|
|
81
|
+
- ansi.length
|
|
82
|
+
- ansi.truncate
|
|
83
|
+
- ansi.pad
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -42,17 +42,21 @@ module.exports = __toCommonJS(index_exports);
|
|
|
42
42
|
// src/symbols.ts
|
|
43
43
|
var symbols_exports = {};
|
|
44
44
|
__export(symbols_exports, {
|
|
45
|
+
ellipsis: () => ellipsis,
|
|
45
46
|
error: () => error,
|
|
46
47
|
info: () => info,
|
|
48
|
+
message: () => message,
|
|
47
49
|
step: () => step,
|
|
48
50
|
success: () => success,
|
|
49
51
|
warning: () => warning
|
|
50
52
|
});
|
|
53
|
+
var message = "\u2502";
|
|
51
54
|
var step = "\u25C7";
|
|
52
55
|
var error = "\xD7";
|
|
53
56
|
var success = "\u25C6";
|
|
54
57
|
var warning = "\u25B2";
|
|
55
58
|
var info = "\xB7";
|
|
59
|
+
var ellipsis = "\u2026";
|
|
56
60
|
|
|
57
61
|
// src/prompts.ts
|
|
58
62
|
var prompts_exports = {};
|
|
@@ -144,7 +148,7 @@ __export(logs_exports, {
|
|
|
144
148
|
info: () => info2,
|
|
145
149
|
intro: () => intro,
|
|
146
150
|
list: () => list,
|
|
147
|
-
message: () =>
|
|
151
|
+
message: () => message2,
|
|
148
152
|
note: () => note,
|
|
149
153
|
outro: () => outro,
|
|
150
154
|
step: () => step2,
|
|
@@ -159,25 +163,26 @@ var import_cli_table3 = __toESM(require("cli-table3"), 1);
|
|
|
159
163
|
// src/ansi.ts
|
|
160
164
|
var ansi_exports = {};
|
|
161
165
|
__export(ansi_exports, {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
+
length: () => length,
|
|
167
|
+
pad: () => pad,
|
|
168
|
+
truncate: () => truncate,
|
|
169
|
+
wrap: () => wrap
|
|
166
170
|
});
|
|
167
|
-
var
|
|
171
|
+
var import_ansi_truncate = __toESM(require("ansi-truncate"), 1);
|
|
168
172
|
var import_string_length = __toESM(require("string-length"), 1);
|
|
169
173
|
var import_wrap_ansi = __toESM(require("wrap-ansi"), 1);
|
|
170
|
-
var
|
|
171
|
-
return (0, import_wrap_ansi.default)(
|
|
174
|
+
var wrap = (value, width, options) => {
|
|
175
|
+
return (0, import_wrap_ansi.default)(value, width, options);
|
|
172
176
|
};
|
|
173
|
-
var
|
|
174
|
-
|
|
175
|
-
if (length > width - 1) {
|
|
176
|
-
return (0, import_ansi_substring.default)(message2, 0, width - 1) + "\u2026";
|
|
177
|
-
}
|
|
178
|
-
return (0, import_ansi_substring.default)(message2, 0, width);
|
|
177
|
+
var length = (value) => {
|
|
178
|
+
return (0, import_string_length.default)(value);
|
|
179
179
|
};
|
|
180
|
-
var
|
|
180
|
+
var truncate = (value, width) => {
|
|
181
|
+
return (0, import_ansi_truncate.default)(value, width, {
|
|
182
|
+
ellipsis
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
var pad = (texts) => {
|
|
181
186
|
const size = Math.max(...texts.map((text2) => (0, import_string_length.default)(text2)));
|
|
182
187
|
return (text2, padding = 0, fill) => {
|
|
183
188
|
return text2.padEnd(size + padding, fill);
|
|
@@ -191,37 +196,37 @@ var color = import_chalk.default;
|
|
|
191
196
|
// src/logs.ts
|
|
192
197
|
var endMargin = 3;
|
|
193
198
|
var intro = (title = "") => {
|
|
194
|
-
(0, import_prompts3.intro)(
|
|
199
|
+
(0, import_prompts3.intro)(truncate(title, process.stdout.columns - 6 - endMargin));
|
|
195
200
|
};
|
|
196
201
|
var outro = (title = "") => {
|
|
197
|
-
(0, import_prompts3.outro)(
|
|
202
|
+
(0, import_prompts3.outro)(truncate(title, process.stdout.columns - 6 - endMargin));
|
|
198
203
|
};
|
|
199
|
-
var note = (title,
|
|
204
|
+
var note = (title, message3) => {
|
|
200
205
|
const width = process.stdout.columns - 6 - endMargin;
|
|
201
206
|
(0, import_prompts3.note)(
|
|
202
|
-
|
|
207
|
+
wrap(message3, width, {
|
|
203
208
|
hard: true
|
|
204
209
|
}),
|
|
205
|
-
|
|
210
|
+
truncate(title, width)
|
|
206
211
|
);
|
|
207
212
|
};
|
|
208
|
-
var logMessage = (symbol,
|
|
213
|
+
var logMessage = (symbol, message3) => {
|
|
209
214
|
import_prompts3.log.message(
|
|
210
|
-
|
|
215
|
+
wrap(message3, process.stdout.columns - 6 - endMargin, {
|
|
211
216
|
hard: true,
|
|
212
217
|
trim: false
|
|
213
218
|
}),
|
|
214
219
|
{ symbol }
|
|
215
220
|
);
|
|
216
221
|
};
|
|
217
|
-
var
|
|
218
|
-
var error2 = (
|
|
219
|
-
var info2 = (
|
|
220
|
-
var step2 = (
|
|
221
|
-
var warning2 = (
|
|
222
|
-
var success2 = (
|
|
222
|
+
var message2 = (message3, symbol = color.gray(message)) => logMessage(symbol, message3);
|
|
223
|
+
var error2 = (message3) => logMessage(color.red(error), message3);
|
|
224
|
+
var info2 = (message3) => logMessage(color.blue(info), message3);
|
|
225
|
+
var step2 = (message3) => logMessage(color.green(step), message3);
|
|
226
|
+
var warning2 = (message3) => logMessage(color.yellow(warning), message3);
|
|
227
|
+
var success2 = (message3) => logMessage(color.green(success), message3);
|
|
223
228
|
var list = (title, data) => {
|
|
224
|
-
const padName =
|
|
229
|
+
const padName = pad(Object.keys(data));
|
|
225
230
|
note(
|
|
226
231
|
title,
|
|
227
232
|
Object.entries(data).map(([name, value]) => {
|
|
@@ -230,41 +235,51 @@ var list = (title, data) => {
|
|
|
230
235
|
);
|
|
231
236
|
};
|
|
232
237
|
var task = async (opts) => {
|
|
233
|
-
let
|
|
238
|
+
let initialMessage = opts.initialMessage;
|
|
239
|
+
let successMessage = opts.successMessage;
|
|
240
|
+
let errorMessage = opts.errorMessage;
|
|
234
241
|
const spin = (0, import_prompts3.spinner)();
|
|
235
242
|
spin.start(opts.initialMessage);
|
|
236
|
-
const stop = (
|
|
237
|
-
spin.stop(
|
|
243
|
+
const stop = (message3, code) => {
|
|
244
|
+
spin.stop(truncate(message3 ?? initialMessage, process.stdout.columns - 6 - endMargin), code);
|
|
238
245
|
};
|
|
239
246
|
try {
|
|
240
|
-
const result = await opts.task(
|
|
241
|
-
|
|
242
|
-
|
|
247
|
+
const result = await opts.task({
|
|
248
|
+
updateMessage(m) {
|
|
249
|
+
spin.message(truncate(m, process.stdout.columns - 6 - endMargin));
|
|
250
|
+
initialMessage = m;
|
|
251
|
+
},
|
|
252
|
+
updateSuccessMessage(m) {
|
|
253
|
+
successMessage = m;
|
|
254
|
+
},
|
|
255
|
+
updateErrorMessage(m) {
|
|
256
|
+
errorMessage = m;
|
|
257
|
+
}
|
|
243
258
|
});
|
|
244
|
-
stop(
|
|
259
|
+
stop(successMessage);
|
|
245
260
|
return result;
|
|
246
261
|
} catch (error3) {
|
|
247
|
-
stop(
|
|
262
|
+
stop(errorMessage, 2);
|
|
248
263
|
throw error3;
|
|
249
264
|
}
|
|
250
265
|
};
|
|
251
266
|
var table = (props) => {
|
|
252
267
|
import_prompts3.log.message();
|
|
253
|
-
const
|
|
268
|
+
const length2 = Math.max(props.head.length, ...props.body.map((b) => b.length));
|
|
254
269
|
const padding = 2;
|
|
255
|
-
const totalPadding = padding * 2 *
|
|
270
|
+
const totalPadding = padding * 2 * length2;
|
|
256
271
|
const border = 1;
|
|
257
|
-
const totalBorder = (
|
|
272
|
+
const totalBorder = (length2 - 1) * border + 2;
|
|
258
273
|
const windowSize = process.stdout.columns;
|
|
259
|
-
const
|
|
260
|
-
const contentSizes = Array.from({ length }).map((_, i) => {
|
|
261
|
-
return Math.max((
|
|
274
|
+
const maxTableSize = windowSize - totalPadding - totalBorder - endMargin;
|
|
275
|
+
const contentSizes = Array.from({ length: length2 }).map((_, i) => {
|
|
276
|
+
return Math.max(length(props.head[i] ?? ""), ...props.body.map((b) => length(String(b[i]))));
|
|
262
277
|
});
|
|
263
|
-
const columnSizes = Array.from({ length }).map(() => {
|
|
278
|
+
const columnSizes = Array.from({ length: length2 }).map(() => {
|
|
264
279
|
return 0;
|
|
265
280
|
});
|
|
266
281
|
let leftover = Math.min(
|
|
267
|
-
|
|
282
|
+
maxTableSize,
|
|
268
283
|
contentSizes.reduce((total, size) => total + size, 0)
|
|
269
284
|
);
|
|
270
285
|
while (leftover > 0) {
|
|
@@ -280,7 +295,7 @@ var table = (props) => {
|
|
|
280
295
|
const table2 = new import_cli_table3.default({
|
|
281
296
|
head: props.head.map(
|
|
282
297
|
(value, x) => "\n" + color.reset.whiteBright.bold(
|
|
283
|
-
|
|
298
|
+
wrap(value, columnSizes[x], {
|
|
284
299
|
hard: true
|
|
285
300
|
})
|
|
286
301
|
)
|
|
@@ -305,7 +320,7 @@ var table = (props) => {
|
|
|
305
320
|
if (typeof value === "number") {
|
|
306
321
|
return color.blue(value);
|
|
307
322
|
}
|
|
308
|
-
return
|
|
323
|
+
return wrap(value, columnSizes[x], {
|
|
309
324
|
hard: true
|
|
310
325
|
});
|
|
311
326
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { TextOptions, PasswordOptions, ConfirmOptions, SelectOptions, MultiSelectOptions } from '@clack/prompts';
|
|
2
|
-
import stringLength from 'string-length';
|
|
3
2
|
import { Options } from 'wrap-ansi';
|
|
4
3
|
import * as chalk from 'chalk';
|
|
5
4
|
|
|
5
|
+
declare const message$1 = "\u2502";
|
|
6
6
|
declare const step$1 = "\u25C7";
|
|
7
7
|
declare const error$1 = "\u00D7";
|
|
8
8
|
declare const success$1 = "\u25C6";
|
|
9
9
|
declare const warning$1 = "\u25B2";
|
|
10
10
|
declare const info$1 = "\u00B7";
|
|
11
|
+
declare const ellipsis = "\u2026";
|
|
11
12
|
|
|
13
|
+
declare const symbols_ellipsis: typeof ellipsis;
|
|
12
14
|
declare namespace symbols {
|
|
13
|
-
export { error$1 as error, info$1 as info, step$1 as step, success$1 as success, warning$1 as warning };
|
|
15
|
+
export { symbols_ellipsis as ellipsis, error$1 as error, info$1 as info, message$1 as message, step$1 as step, success$1 as success, warning$1 as warning };
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
declare const text: (opts: TextOptions) => Promise<string>;
|
|
@@ -52,7 +54,11 @@ type TaskOptions<T> = {
|
|
|
52
54
|
initialMessage: string;
|
|
53
55
|
errorMessage?: string;
|
|
54
56
|
successMessage?: string;
|
|
55
|
-
task: (
|
|
57
|
+
task: (context: {
|
|
58
|
+
updateMessage: (message: string) => void;
|
|
59
|
+
updateErrorMessage: (message: string) => void;
|
|
60
|
+
updateSuccessMessage: (message: string) => void;
|
|
61
|
+
}) => Promise<T>;
|
|
56
62
|
};
|
|
57
63
|
declare const task: <T>(opts: TaskOptions<T>) => Promise<T>;
|
|
58
64
|
declare const table: (props: {
|
|
@@ -76,16 +82,17 @@ declare namespace logs {
|
|
|
76
82
|
export { logs_error as error, logs_info as info, logs_intro as intro, logs_list as list, logs_message as message, logs_note as note, logs_outro as outro, logs_step as step, logs_success as success, logs_table as table, logs_task as task, logs_warning as warning };
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
declare const
|
|
80
|
-
declare const
|
|
81
|
-
declare const
|
|
85
|
+
declare const wrap: (value: string, width: number, options?: Options) => string;
|
|
86
|
+
declare const length: (value: string) => number;
|
|
87
|
+
declare const truncate: (value: string, width: number) => string;
|
|
88
|
+
declare const pad: (texts: string[]) => (text: string, padding?: number, fill?: string) => string;
|
|
82
89
|
|
|
83
|
-
declare const
|
|
84
|
-
declare const
|
|
85
|
-
declare const
|
|
86
|
-
declare const
|
|
90
|
+
declare const ansi_length: typeof length;
|
|
91
|
+
declare const ansi_pad: typeof pad;
|
|
92
|
+
declare const ansi_truncate: typeof truncate;
|
|
93
|
+
declare const ansi_wrap: typeof wrap;
|
|
87
94
|
declare namespace ansi {
|
|
88
|
-
export {
|
|
95
|
+
export { ansi_length as length, ansi_pad as pad, ansi_truncate as truncate, ansi_wrap as wrap };
|
|
89
96
|
}
|
|
90
97
|
|
|
91
98
|
declare const color: chalk.ChalkInstance;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { TextOptions, PasswordOptions, ConfirmOptions, SelectOptions, MultiSelectOptions } from '@clack/prompts';
|
|
2
|
-
import stringLength from 'string-length';
|
|
3
2
|
import { Options } from 'wrap-ansi';
|
|
4
3
|
import * as chalk from 'chalk';
|
|
5
4
|
|
|
5
|
+
declare const message$1 = "\u2502";
|
|
6
6
|
declare const step$1 = "\u25C7";
|
|
7
7
|
declare const error$1 = "\u00D7";
|
|
8
8
|
declare const success$1 = "\u25C6";
|
|
9
9
|
declare const warning$1 = "\u25B2";
|
|
10
10
|
declare const info$1 = "\u00B7";
|
|
11
|
+
declare const ellipsis = "\u2026";
|
|
11
12
|
|
|
13
|
+
declare const symbols_ellipsis: typeof ellipsis;
|
|
12
14
|
declare namespace symbols {
|
|
13
|
-
export { error$1 as error, info$1 as info, step$1 as step, success$1 as success, warning$1 as warning };
|
|
15
|
+
export { symbols_ellipsis as ellipsis, error$1 as error, info$1 as info, message$1 as message, step$1 as step, success$1 as success, warning$1 as warning };
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
declare const text: (opts: TextOptions) => Promise<string>;
|
|
@@ -52,7 +54,11 @@ type TaskOptions<T> = {
|
|
|
52
54
|
initialMessage: string;
|
|
53
55
|
errorMessage?: string;
|
|
54
56
|
successMessage?: string;
|
|
55
|
-
task: (
|
|
57
|
+
task: (context: {
|
|
58
|
+
updateMessage: (message: string) => void;
|
|
59
|
+
updateErrorMessage: (message: string) => void;
|
|
60
|
+
updateSuccessMessage: (message: string) => void;
|
|
61
|
+
}) => Promise<T>;
|
|
56
62
|
};
|
|
57
63
|
declare const task: <T>(opts: TaskOptions<T>) => Promise<T>;
|
|
58
64
|
declare const table: (props: {
|
|
@@ -76,16 +82,17 @@ declare namespace logs {
|
|
|
76
82
|
export { logs_error as error, logs_info as info, logs_intro as intro, logs_list as list, logs_message as message, logs_note as note, logs_outro as outro, logs_step as step, logs_success as success, logs_table as table, logs_task as task, logs_warning as warning };
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
declare const
|
|
80
|
-
declare const
|
|
81
|
-
declare const
|
|
85
|
+
declare const wrap: (value: string, width: number, options?: Options) => string;
|
|
86
|
+
declare const length: (value: string) => number;
|
|
87
|
+
declare const truncate: (value: string, width: number) => string;
|
|
88
|
+
declare const pad: (texts: string[]) => (text: string, padding?: number, fill?: string) => string;
|
|
82
89
|
|
|
83
|
-
declare const
|
|
84
|
-
declare const
|
|
85
|
-
declare const
|
|
86
|
-
declare const
|
|
90
|
+
declare const ansi_length: typeof length;
|
|
91
|
+
declare const ansi_pad: typeof pad;
|
|
92
|
+
declare const ansi_truncate: typeof truncate;
|
|
93
|
+
declare const ansi_wrap: typeof wrap;
|
|
87
94
|
declare namespace ansi {
|
|
88
|
-
export {
|
|
95
|
+
export { ansi_length as length, ansi_pad as pad, ansi_truncate as truncate, ansi_wrap as wrap };
|
|
89
96
|
}
|
|
90
97
|
|
|
91
98
|
declare const color: chalk.ChalkInstance;
|
package/dist/index.js
CHANGED
|
@@ -7,17 +7,21 @@ var __export = (target, all) => {
|
|
|
7
7
|
// src/symbols.ts
|
|
8
8
|
var symbols_exports = {};
|
|
9
9
|
__export(symbols_exports, {
|
|
10
|
+
ellipsis: () => ellipsis,
|
|
10
11
|
error: () => error,
|
|
11
12
|
info: () => info,
|
|
13
|
+
message: () => message,
|
|
12
14
|
step: () => step,
|
|
13
15
|
success: () => success,
|
|
14
16
|
warning: () => warning
|
|
15
17
|
});
|
|
18
|
+
var message = "\u2502";
|
|
16
19
|
var step = "\u25C7";
|
|
17
20
|
var error = "\xD7";
|
|
18
21
|
var success = "\u25C6";
|
|
19
22
|
var warning = "\u25B2";
|
|
20
23
|
var info = "\xB7";
|
|
24
|
+
var ellipsis = "\u2026";
|
|
21
25
|
|
|
22
26
|
// src/prompts.ts
|
|
23
27
|
var prompts_exports = {};
|
|
@@ -115,7 +119,7 @@ __export(logs_exports, {
|
|
|
115
119
|
info: () => info2,
|
|
116
120
|
intro: () => intro,
|
|
117
121
|
list: () => list,
|
|
118
|
-
message: () =>
|
|
122
|
+
message: () => message2,
|
|
119
123
|
note: () => note,
|
|
120
124
|
outro: () => outro,
|
|
121
125
|
step: () => step2,
|
|
@@ -130,26 +134,27 @@ import Table from "cli-table3";
|
|
|
130
134
|
// src/ansi.ts
|
|
131
135
|
var ansi_exports = {};
|
|
132
136
|
__export(ansi_exports, {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
length: () => length,
|
|
138
|
+
pad: () => pad,
|
|
139
|
+
truncate: () => truncate,
|
|
140
|
+
wrap: () => wrap
|
|
137
141
|
});
|
|
138
|
-
import
|
|
139
|
-
import
|
|
140
|
-
import
|
|
141
|
-
var
|
|
142
|
-
return
|
|
142
|
+
import ansiTruncate from "ansi-truncate";
|
|
143
|
+
import ansiLength from "string-length";
|
|
144
|
+
import ansiWrap from "wrap-ansi";
|
|
145
|
+
var wrap = (value, width, options) => {
|
|
146
|
+
return ansiWrap(value, width, options);
|
|
143
147
|
};
|
|
144
|
-
var
|
|
145
|
-
|
|
146
|
-
if (length > width - 1) {
|
|
147
|
-
return ansiSubstring(message2, 0, width - 1) + "\u2026";
|
|
148
|
-
}
|
|
149
|
-
return ansiSubstring(message2, 0, width);
|
|
148
|
+
var length = (value) => {
|
|
149
|
+
return ansiLength(value);
|
|
150
150
|
};
|
|
151
|
-
var
|
|
152
|
-
|
|
151
|
+
var truncate = (value, width) => {
|
|
152
|
+
return ansiTruncate(value, width, {
|
|
153
|
+
ellipsis
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
var pad = (texts) => {
|
|
157
|
+
const size = Math.max(...texts.map((text2) => ansiLength(text2)));
|
|
153
158
|
return (text2, padding = 0, fill) => {
|
|
154
159
|
return text2.padEnd(size + padding, fill);
|
|
155
160
|
};
|
|
@@ -162,37 +167,37 @@ var color = chalk;
|
|
|
162
167
|
// src/logs.ts
|
|
163
168
|
var endMargin = 3;
|
|
164
169
|
var intro = (title = "") => {
|
|
165
|
-
p_intro(
|
|
170
|
+
p_intro(truncate(title, process.stdout.columns - 6 - endMargin));
|
|
166
171
|
};
|
|
167
172
|
var outro = (title = "") => {
|
|
168
|
-
p_outro(
|
|
173
|
+
p_outro(truncate(title, process.stdout.columns - 6 - endMargin));
|
|
169
174
|
};
|
|
170
|
-
var note = (title,
|
|
175
|
+
var note = (title, message3) => {
|
|
171
176
|
const width = process.stdout.columns - 6 - endMargin;
|
|
172
177
|
p_note(
|
|
173
|
-
|
|
178
|
+
wrap(message3, width, {
|
|
174
179
|
hard: true
|
|
175
180
|
}),
|
|
176
|
-
|
|
181
|
+
truncate(title, width)
|
|
177
182
|
);
|
|
178
183
|
};
|
|
179
|
-
var logMessage = (symbol,
|
|
184
|
+
var logMessage = (symbol, message3) => {
|
|
180
185
|
log.message(
|
|
181
|
-
|
|
186
|
+
wrap(message3, process.stdout.columns - 6 - endMargin, {
|
|
182
187
|
hard: true,
|
|
183
188
|
trim: false
|
|
184
189
|
}),
|
|
185
190
|
{ symbol }
|
|
186
191
|
);
|
|
187
192
|
};
|
|
188
|
-
var
|
|
189
|
-
var error2 = (
|
|
190
|
-
var info2 = (
|
|
191
|
-
var step2 = (
|
|
192
|
-
var warning2 = (
|
|
193
|
-
var success2 = (
|
|
193
|
+
var message2 = (message3, symbol = color.gray(message)) => logMessage(symbol, message3);
|
|
194
|
+
var error2 = (message3) => logMessage(color.red(error), message3);
|
|
195
|
+
var info2 = (message3) => logMessage(color.blue(info), message3);
|
|
196
|
+
var step2 = (message3) => logMessage(color.green(step), message3);
|
|
197
|
+
var warning2 = (message3) => logMessage(color.yellow(warning), message3);
|
|
198
|
+
var success2 = (message3) => logMessage(color.green(success), message3);
|
|
194
199
|
var list = (title, data) => {
|
|
195
|
-
const padName =
|
|
200
|
+
const padName = pad(Object.keys(data));
|
|
196
201
|
note(
|
|
197
202
|
title,
|
|
198
203
|
Object.entries(data).map(([name, value]) => {
|
|
@@ -201,41 +206,51 @@ var list = (title, data) => {
|
|
|
201
206
|
);
|
|
202
207
|
};
|
|
203
208
|
var task = async (opts) => {
|
|
204
|
-
let
|
|
209
|
+
let initialMessage = opts.initialMessage;
|
|
210
|
+
let successMessage = opts.successMessage;
|
|
211
|
+
let errorMessage = opts.errorMessage;
|
|
205
212
|
const spin = spinner();
|
|
206
213
|
spin.start(opts.initialMessage);
|
|
207
|
-
const stop = (
|
|
208
|
-
spin.stop(
|
|
214
|
+
const stop = (message3, code) => {
|
|
215
|
+
spin.stop(truncate(message3 ?? initialMessage, process.stdout.columns - 6 - endMargin), code);
|
|
209
216
|
};
|
|
210
217
|
try {
|
|
211
|
-
const result = await opts.task(
|
|
212
|
-
|
|
213
|
-
|
|
218
|
+
const result = await opts.task({
|
|
219
|
+
updateMessage(m) {
|
|
220
|
+
spin.message(truncate(m, process.stdout.columns - 6 - endMargin));
|
|
221
|
+
initialMessage = m;
|
|
222
|
+
},
|
|
223
|
+
updateSuccessMessage(m) {
|
|
224
|
+
successMessage = m;
|
|
225
|
+
},
|
|
226
|
+
updateErrorMessage(m) {
|
|
227
|
+
errorMessage = m;
|
|
228
|
+
}
|
|
214
229
|
});
|
|
215
|
-
stop(
|
|
230
|
+
stop(successMessage);
|
|
216
231
|
return result;
|
|
217
232
|
} catch (error3) {
|
|
218
|
-
stop(
|
|
233
|
+
stop(errorMessage, 2);
|
|
219
234
|
throw error3;
|
|
220
235
|
}
|
|
221
236
|
};
|
|
222
237
|
var table = (props) => {
|
|
223
238
|
log.message();
|
|
224
|
-
const
|
|
239
|
+
const length2 = Math.max(props.head.length, ...props.body.map((b) => b.length));
|
|
225
240
|
const padding = 2;
|
|
226
|
-
const totalPadding = padding * 2 *
|
|
241
|
+
const totalPadding = padding * 2 * length2;
|
|
227
242
|
const border = 1;
|
|
228
|
-
const totalBorder = (
|
|
243
|
+
const totalBorder = (length2 - 1) * border + 2;
|
|
229
244
|
const windowSize = process.stdout.columns;
|
|
230
|
-
const
|
|
231
|
-
const contentSizes = Array.from({ length }).map((_, i) => {
|
|
232
|
-
return Math.max(
|
|
245
|
+
const maxTableSize = windowSize - totalPadding - totalBorder - endMargin;
|
|
246
|
+
const contentSizes = Array.from({ length: length2 }).map((_, i) => {
|
|
247
|
+
return Math.max(length(props.head[i] ?? ""), ...props.body.map((b) => length(String(b[i]))));
|
|
233
248
|
});
|
|
234
|
-
const columnSizes = Array.from({ length }).map(() => {
|
|
249
|
+
const columnSizes = Array.from({ length: length2 }).map(() => {
|
|
235
250
|
return 0;
|
|
236
251
|
});
|
|
237
252
|
let leftover = Math.min(
|
|
238
|
-
|
|
253
|
+
maxTableSize,
|
|
239
254
|
contentSizes.reduce((total, size) => total + size, 0)
|
|
240
255
|
);
|
|
241
256
|
while (leftover > 0) {
|
|
@@ -251,7 +266,7 @@ var table = (props) => {
|
|
|
251
266
|
const table2 = new Table({
|
|
252
267
|
head: props.head.map(
|
|
253
268
|
(value, x) => "\n" + color.reset.whiteBright.bold(
|
|
254
|
-
|
|
269
|
+
wrap(value, columnSizes[x], {
|
|
255
270
|
hard: true
|
|
256
271
|
})
|
|
257
272
|
)
|
|
@@ -276,7 +291,7 @@ var table = (props) => {
|
|
|
276
291
|
if (typeof value === "number") {
|
|
277
292
|
return color.blue(value);
|
|
278
293
|
}
|
|
279
|
-
return
|
|
294
|
+
return wrap(value, columnSizes[x], {
|
|
280
295
|
hard: true
|
|
281
296
|
});
|
|
282
297
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@awsless/clui",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -25,15 +25,16 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@clack/prompts": "^0.11.0",
|
|
28
|
-
"ansi-
|
|
28
|
+
"ansi-truncate": "^1.2.0",
|
|
29
29
|
"chalk": "^5.4.1",
|
|
30
30
|
"cli-table3": "^0.6.5",
|
|
31
31
|
"string-length": "^6.0.0",
|
|
32
32
|
"wrap-ansi": "^9.0.0"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
|
-
"
|
|
36
|
-
"
|
|
35
|
+
"bug": "bun ./demo/bug.ts",
|
|
36
|
+
"prompts": "bun ./demo/prompts.ts",
|
|
37
|
+
"logs": "bun ./demo/logs.ts",
|
|
37
38
|
"test": "pnpm code test",
|
|
38
39
|
"build": "pnpm tsup src/index.ts --format cjs,esm --dts --clean",
|
|
39
40
|
"prepublish": "if pnpm test; then pnpm build; else exit; fi"
|