@1-/bar 0.1.0
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 +143 -0
- package/_.js +111 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
[English](#en) | [中文](#zh)
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<a id="en"></a>
|
|
6
|
+
# @1-/bar : TTY-aware command-line progress bar and status display
|
|
7
|
+
|
|
8
|
+
- [@1-/bar : TTY-aware command-line progress bar and status display](#1-bar-tty-aware-command-line-progress-bar-and-status-display)
|
|
9
|
+
- [Functionality](#functionality)
|
|
10
|
+
- [Usage demonstration](#usage-demonstration)
|
|
11
|
+
- [Design rationale](#design-rationale)
|
|
12
|
+
- [Technology stack](#technology-stack)
|
|
13
|
+
- [Code structure](#code-structure)
|
|
14
|
+
- [Historical background](#historical-background)
|
|
15
|
+
- [About](#about)
|
|
16
|
+
|
|
17
|
+
## Functionality
|
|
18
|
+
TTY-aware command-line progress bar and status display utility for Node.js applications. Provides spinner animation, task tracking, ETA estimation, and safe logging that doesn't disrupt the display.
|
|
19
|
+
|
|
20
|
+
## Usage demonstration
|
|
21
|
+
```bash
|
|
22
|
+
npm install @1-/bar
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import bar from '@1-/bar';
|
|
27
|
+
|
|
28
|
+
const [start, stop, incr, log] = bar();
|
|
29
|
+
|
|
30
|
+
// Start progress bar with total tasks
|
|
31
|
+
start(100);
|
|
32
|
+
|
|
33
|
+
// Log subtasks
|
|
34
|
+
log.start('Processing files');
|
|
35
|
+
|
|
36
|
+
// Increment progress
|
|
37
|
+
for (let i = 0; i < 100; i++) {
|
|
38
|
+
incr();
|
|
39
|
+
// Simulate work
|
|
40
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
log.end('Processing files');
|
|
44
|
+
stop();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Design rationale
|
|
48
|
+
The implementation uses terminal escape sequences for efficient rendering without disrupting the display. It maintains state for progress tracking, task management, and timing calculations for ETA estimation.
|
|
49
|
+
|
|
50
|
+

|
|
51
|
+
|
|
52
|
+
## Technology stack
|
|
53
|
+
- Node.js runtime
|
|
54
|
+
- Standard JavaScript modules
|
|
55
|
+
- Terminal escape sequences for TTY control
|
|
56
|
+
- Set data structure for task tracking
|
|
57
|
+
|
|
58
|
+
## Code structure
|
|
59
|
+
```
|
|
60
|
+
src/
|
|
61
|
+
├── _.js # Main module exporting progress utilities
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Historical background
|
|
65
|
+
The Mulan Public Software License (MulanPSL) is an open-source license developed in China, designed to be compatible with international open-source practices while addressing local legal requirements. Version 2.0, released in 2020, improved compatibility with other licenses and clarified terms for patent grants and trademark usage.
|
|
66
|
+
|
|
67
|
+
## About
|
|
68
|
+
|
|
69
|
+
This library is developed by [WebC.site](https://webc.site).
|
|
70
|
+
|
|
71
|
+
[WebC.site](https://webc.site): A new paradigm of web development for AI
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
<a id="zh"></a>
|
|
77
|
+
# @1-/bar : TTY 感知的命令行进度条及状态显示
|
|
78
|
+
|
|
79
|
+
- [@1-/bar : TTY 感知的命令行进度条及状态显示](#1-bar-tty-感知的命令行进度条及状态显示)
|
|
80
|
+
- [功能介绍](#功能介绍)
|
|
81
|
+
- [使用演示](#使用演示)
|
|
82
|
+
- [设计思路](#设计思路)
|
|
83
|
+
- [技术栈](#技术栈)
|
|
84
|
+
- [代码结构](#代码结构)
|
|
85
|
+
- [历史故事](#历史故事)
|
|
86
|
+
- [关于](#关于)
|
|
87
|
+
|
|
88
|
+
## 功能介绍
|
|
89
|
+
TTY 感知的命令行进度条及状态显示工具,适用于 Node.js 应用程序。提供旋转动画、任务跟踪、ETA 预估及安全日志输出功能,确保日志不会破坏进度条显示。
|
|
90
|
+
|
|
91
|
+
## 使用演示
|
|
92
|
+
```bash
|
|
93
|
+
npm install @1-/bar
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
import bar from '@1-/bar';
|
|
98
|
+
|
|
99
|
+
const [start, stop, incr, log] = bar();
|
|
100
|
+
|
|
101
|
+
// 启动进度条并设置总任务数
|
|
102
|
+
start(100);
|
|
103
|
+
|
|
104
|
+
// 记录子任务
|
|
105
|
+
log.start('处理文件');
|
|
106
|
+
|
|
107
|
+
// 增加已完成任务数
|
|
108
|
+
for (let i = 0; i < 100; i++) {
|
|
109
|
+
incr();
|
|
110
|
+
// 模拟工作
|
|
111
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
log.end('处理文件');
|
|
115
|
+
stop();
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 设计思路
|
|
119
|
+
实现采用终端转义序列进行高效渲染,避免显示中断。维护进度跟踪、任务管理及时间计算状态,支持 ETA(预估完成时间)估算。
|
|
120
|
+
|
|
121
|
+

|
|
122
|
+
|
|
123
|
+
## 技术栈
|
|
124
|
+
- Node.js 运行时
|
|
125
|
+
- 标准 JavaScript 模块
|
|
126
|
+
- 终端转义序列实现 TTY 控制
|
|
127
|
+
- Set 数据结构进行任务跟踪
|
|
128
|
+
|
|
129
|
+
## 代码结构
|
|
130
|
+
```
|
|
131
|
+
src/
|
|
132
|
+
├── _.js # 主模块,导出进度工具函数
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## 历史故事
|
|
136
|
+
木兰公共许可证(MulanPSL)是中国开发的开源许可证,旨在兼容国际开源实践同时满足本地法律要求。2.0 版本于 2020 年发布,提升了与其他许可证的兼容性,并明确了专利授权和商标使用条款。
|
|
137
|
+
|
|
138
|
+
## 关于
|
|
139
|
+
|
|
140
|
+
本库由 [WebC.site](https://webc.site) 开发。
|
|
141
|
+
|
|
142
|
+
[WebC.site](https://webc.site) : 面向人工智能的网站开发新范式
|
|
143
|
+
|
package/_.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/*
|
|
2
|
+
命令行任务进度及状态显示器
|
|
3
|
+
返回值: [start, stop, incr, log]
|
|
4
|
+
start: (total) => void 开始进度条,设置总任务数
|
|
5
|
+
stop: () => void 停止进度条
|
|
6
|
+
incr: () => void 增加已完成任务数
|
|
7
|
+
log: (...args) => void 安全输出日志,不破坏进度条
|
|
8
|
+
log.start: (task) => void 开始子任务
|
|
9
|
+
log.end: (task) => void 结束子任务
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
|
|
13
|
+
{ stdout } = process,
|
|
14
|
+
IS_TTY = stdout.isTTY,
|
|
15
|
+
clear = IS_TTY
|
|
16
|
+
? (n) => {
|
|
17
|
+
if (n > 0) {
|
|
18
|
+
stdout.write("\r\x1b[" + n + "A\x1b[J");
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
: () => 0;
|
|
23
|
+
|
|
24
|
+
export default () => {
|
|
25
|
+
const running = new Set();
|
|
26
|
+
let done = 0,
|
|
27
|
+
total = 0,
|
|
28
|
+
last_n = 0,
|
|
29
|
+
start_at = 0,
|
|
30
|
+
spin_idx = 0,
|
|
31
|
+
timer = null;
|
|
32
|
+
|
|
33
|
+
const format = (secs) => {
|
|
34
|
+
if (secs < 0 || !isFinite(secs)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const m = Math.floor(secs / 60),
|
|
38
|
+
s = secs % 60;
|
|
39
|
+
return (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
|
|
40
|
+
},
|
|
41
|
+
render = () => {
|
|
42
|
+
if (!IS_TTY) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
last_n = clear(last_n);
|
|
46
|
+
const lines = [];
|
|
47
|
+
if (total > 0) {
|
|
48
|
+
const pct = Math.round((done / total) * 100);
|
|
49
|
+
let eta;
|
|
50
|
+
if (done > 0) {
|
|
51
|
+
const elapsed = Date.now() - start_at,
|
|
52
|
+
avg = elapsed / done;
|
|
53
|
+
eta = format(Math.round(((total - done) * avg) / 1000));
|
|
54
|
+
}
|
|
55
|
+
lines.push(done + "/" + total + " (" + pct + "%) " + (eta ? "ETA " + eta : ""));
|
|
56
|
+
}
|
|
57
|
+
for (const task of running) {
|
|
58
|
+
lines.push(SPINNER[spin_idx] + " " + task);
|
|
59
|
+
}
|
|
60
|
+
if (lines.length > 0) {
|
|
61
|
+
lines.unshift("");
|
|
62
|
+
stdout.write("\r" + lines.join("\n") + "\n");
|
|
63
|
+
last_n = lines.length;
|
|
64
|
+
} else {
|
|
65
|
+
last_n = 0;
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
log = (...args) => {
|
|
69
|
+
last_n = clear(last_n);
|
|
70
|
+
console.log(...args);
|
|
71
|
+
render();
|
|
72
|
+
},
|
|
73
|
+
start = (val) => {
|
|
74
|
+
total = val;
|
|
75
|
+
done = 0;
|
|
76
|
+
start_at = Date.now();
|
|
77
|
+
if (IS_TTY) {
|
|
78
|
+
timer = setInterval(() => {
|
|
79
|
+
spin_idx = (spin_idx + 1) % SPINNER.length;
|
|
80
|
+
render();
|
|
81
|
+
}, 80);
|
|
82
|
+
}
|
|
83
|
+
render();
|
|
84
|
+
},
|
|
85
|
+
stop = () => {
|
|
86
|
+
if (timer) {
|
|
87
|
+
clearInterval(timer);
|
|
88
|
+
timer = null;
|
|
89
|
+
}
|
|
90
|
+
last_n = clear(last_n);
|
|
91
|
+
if (!IS_TTY && total > 0) {
|
|
92
|
+
console.log(done + "/" + total);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
incr = () => {
|
|
96
|
+
++done;
|
|
97
|
+
render();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
log.start = (task) => {
|
|
101
|
+
running.add(task);
|
|
102
|
+
render();
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
log.end = (task) => {
|
|
106
|
+
running.delete(task);
|
|
107
|
+
render();
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return [start, stop, incr, log];
|
|
111
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1-/bar",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TTY-aware command-line progress bar and status display utility",
|
|
5
|
+
"description_zh": "TTY 感知的命令行进度条及状态显示工具",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"progress",
|
|
8
|
+
"bar",
|
|
9
|
+
"cli",
|
|
10
|
+
"tty",
|
|
11
|
+
"spinner"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/webc-site/npm/tree/main/bar",
|
|
14
|
+
"license": "MulanPSL-2.0",
|
|
15
|
+
"author": "x-at-01@googlegroups.com",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/webc-site/npm.git"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./_.js",
|
|
23
|
+
"./*": "./*"
|
|
24
|
+
}
|
|
25
|
+
}
|