Skip to content

Commit 1bdae4e

Browse files
committed
feat: add flush method to Printer interface for console output without EOL
1 parent 1c29c3e commit 1bdae4e

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ interface Printer {
432432
*/
433433
println(str?: string): this;
434434

435+
/**
436+
* Print some string on the console without EOL.
437+
* @param str
438+
*/
439+
flush(str?: string): this;
440+
435441
/**
436442
* Print some string on the console without EOL.
437443
* @param str

src/printer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ function println(str = '') {
5050
return this;
5151
}
5252

53+
function flush(str = null) {
54+
str = is.invalid(str) ? `${buffer}` : `${buffer}${str}`;
55+
buffer = '';
56+
if (!quiet) {
57+
process.stdout.write('\r' + str);
58+
}
59+
return this;
60+
}
61+
5362
function themes(options = {}) {
5463
if (!is.empty(options)) {
5564
Object.assign(themesConfig, options);
@@ -149,6 +158,7 @@ module.exports = {
149158
print,
150159
themes,
151160
println,
161+
flush,
152162
enable,
153163
disable,
154164
input,

tests/printer.tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,33 @@ describe('print test case', function () {
5151
it('print with null', function () {
5252
printer.print(null).println(null);
5353
});
54+
55+
it('flush test', function () {
56+
// 测试基本的 flush 调用
57+
printer.flush('Progress: 0%');
58+
printer.flush('Progress: 50%');
59+
printer.flush('Progress: 100%');
60+
printer.println(); // 换行
61+
62+
// 测试链式调用
63+
printer.fixed('Loading', 10).flush();
64+
printer.fixed('Loading.', 10).flush();
65+
printer.fixed('Loading..', 10).flush();
66+
printer.fixed('Loading...', 10).flush();
67+
printer.println(); // 换行
68+
69+
// 测试 flush 与 quiet 模式
70+
printer.disable();
71+
printer.flush('This should not display');
72+
printer.enable();
73+
printer.flush('This should display');
74+
printer.println();
75+
76+
// 测试 flush 参数为空
77+
printer.flush();
78+
printer.println();
79+
80+
// exec here is ok
81+
expect(true).to.be.true;
82+
});
5483
});

0 commit comments

Comments
 (0)