@@ -3,118 +3,118 @@ var cursor = require('./lib/cursor');
3
3
var color = require ( 'colors-cli' )
4
4
5
5
function loading ( options ) {
6
- if ( ! ( this instanceof loading ) ) {
7
- return new loading ( options )
8
- }
9
- if ( typeof options === 'string' ) {
10
- options = {
11
- text : options
12
- }
6
+ if ( ! ( this instanceof loading ) ) {
7
+ return new loading ( options )
8
+ }
9
+ if ( typeof options === 'string' ) {
10
+ options = {
11
+ text : options
13
12
}
13
+ }
14
14
15
- this . options = ut . extend ( options , {
16
- text : '' ,
17
- color : 'cyan' ,
18
- stream : process . stderr ,
19
- // stream: process.stdout
20
- // loading 样式
21
- frames : [ "◜" , "◠" , "◝" , "◞" , "◡" , "◟" ]
22
- } ) ;
15
+ this . options = ut . extend ( options , {
16
+ text : '' ,
17
+ color : 'cyan' ,
18
+ stream : process . stderr ,
19
+ // stream: process.stdout
20
+ // loading 样式
21
+ frames : [ "◜" , "◠" , "◝" , "◞" , "◡" , "◟" ]
22
+ } ) ;
23
23
24
- // 文本显示
25
- this . text = this . options . text ;
24
+ // 文本显示
25
+ this . text = this . options . text ;
26
26
27
- // 颜色显示
28
- this . color = this . options . color ;
27
+ // 颜色显示
28
+ this . color = this . options . color ;
29
29
30
- // 动画间隔时间
31
- this . interval = this . options . interval || 60 ;
32
- this . stream = this . options . stream ;
30
+ // 动画间隔时间
31
+ this . interval = this . options . interval || 60 ;
32
+ this . stream = this . options . stream ;
33
33
34
- // loading 样式
35
- this . frames = this . options . frames ;
34
+ // loading 样式
35
+ this . frames = this . options . frames ;
36
36
37
- // 不存在
38
- this . id = null ;
37
+ // 不存在
38
+ this . id = null ;
39
39
40
- // 要检查 Node 是否正在运行一个 TTY上下文 中
41
- // linux 中没有运行在 tty 下的进程是 守护进程
42
- this . enabled = this . options . enabled || ( ( this . stream && this . stream . isTTY ) && ! process . env . CI ) ;
43
- this . frameIndex = 0 ;
40
+ // 要检查 Node 是否正在运行一个 TTY上下文 中
41
+ // linux 中没有运行在 tty 下的进程是 守护进程
42
+ this . enabled = this . options . enabled || ( ( this . stream && this . stream . isTTY ) && ! process . env . CI ) ;
43
+ this . frameIndex = 0 ;
44
44
}
45
45
46
46
loading . prototype . frame = function ( ) {
47
- var frames = this . frames ;
48
- // var frames = ["◜", "◠", "◝", "◞", "◡", "◟"];
49
- // var frames = ["◰", "◳", "◲", "◱"]
50
- // var frames = ["◐", "◓", "◑", "◒"]
51
- // var frames = [".", "o", "O", "°", "O", "o", "."]
52
- // var frames = ["⊶", "⊷"]
53
- // var frames = ["ဝ", "၀"]
54
- // var frames = ["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"]
55
- // var frames = ["🕐 ", "🕑 ", "🕒 ", "🕓 ", "🕔 ", "🕕 ", "🕖 ", "🕗 ", "🕘 ", "🕙 ", "🕚 "]
56
- var frame = frames [ this . frameIndex ] ;
57
- if ( this . color ) {
58
- frame = color [ this . color ] ( frame ) ;
59
- }
60
- this . frameIndex = ++ this . frameIndex % frames . length ;
61
- return frame + ' ' + this . text ;
47
+ var frames = this . frames ;
48
+ // var frames = ["◜", "◠", "◝", "◞", "◡", "◟"];
49
+ // var frames = ["◰", "◳", "◲", "◱"]
50
+ // var frames = ["◐", "◓", "◑", "◒"]
51
+ // var frames = [".", "o", "O", "°", "O", "o", "."]
52
+ // var frames = ["⊶", "⊷"]
53
+ // var frames = ["ဝ", "၀"]
54
+ // var frames = ["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"]
55
+ // var frames = ["🕐 ", "🕑 ", "🕒 ", "🕓 ", "🕔 ", "🕕 ", "🕖 ", "🕗 ", "🕘 ", "🕙 ", "🕚 "]
56
+ var frame = frames [ this . frameIndex ] ;
57
+ if ( this . color ) {
58
+ frame = color [ this . color ] ( frame ) ;
59
+ }
60
+ this . frameIndex = ++ this . frameIndex % frames . length ;
61
+ return frame + ' ' + this . text ;
62
62
}
63
63
64
64
loading . prototype . clear = function ( ) {
65
65
66
- if ( ! this . enabled ) {
67
- return this ;
68
- }
66
+ if ( ! this . enabled ) {
67
+ return this ;
68
+ }
69
69
70
- this . stream . clearLine ( ) ;
71
- this . stream . cursorTo ( 0 ) ;
70
+ this . stream . clearLine ( ) ;
71
+ this . stream . cursorTo ( 0 ) ;
72
72
73
- return this ;
73
+ return this ;
74
74
}
75
75
76
76
loading . prototype . render = function ( ) {
77
- this . clear ( ) ;
78
- this . stream . write ( this . frame ( ) ) ;
79
- return this ;
80
-
77
+ this . clear ( ) ;
78
+ this . stream . write ( this . frame ( ) ) ;
79
+ return this ;
81
80
}
82
81
83
- loading . prototype . start = function ( ) {
84
- if ( ! this . enabled || this . id ) return this ;
85
- this . clear ( ) ;
86
- cursor . hide ( this . stream ) ;
87
- this . id = setInterval ( this . render . bind ( this ) , this . interval ) ;
88
- return this ;
82
+ loading . prototype . start = function ( text ) {
83
+ if ( text ) this . text = text ;
84
+ if ( ! this . enabled || this . id ) return this ;
85
+ this . clear ( ) ;
86
+ cursor . hide ( this . stream ) ;
87
+ this . id = setInterval ( this . render . bind ( this ) , this . interval ) ;
88
+ return this ;
89
89
}
90
90
91
91
loading . prototype . stop = function ( ) {
92
- if ( ! this . enabled ) return this ;
93
- clearInterval ( this . id ) ;
94
- this . id = null ;
95
- this . clear ( ) ;
96
- cursor . show ( this . stream ) ;
97
- return this ;
92
+ if ( ! this . enabled ) return this ;
93
+ clearInterval ( this . id ) ;
94
+ this . id = null ;
95
+ this . clear ( ) ;
96
+ cursor . show ( this . stream ) ;
97
+ return this ;
98
98
}
99
99
100
100
101
101
loading . prototype . succeed = function ( text ) {
102
- return this . stopAndPersist ( color . green ( '✔' ) , text ) ;
102
+ return this . stopAndPersist ( color . green ( '✔' ) , text ) ;
103
103
}
104
104
loading . prototype . fail = function ( text ) {
105
- return this . stopAndPersist ( color . red ( '✖' ) , text ) ;
105
+ return this . stopAndPersist ( color . red ( '✖' ) , text ) ;
106
106
}
107
107
loading . prototype . warn = function ( text ) {
108
- return this . stopAndPersist ( color . yellow ( '⚠' ) , text ) ;
108
+ return this . stopAndPersist ( color . yellow ( '⚠' ) , text ) ;
109
109
}
110
110
loading . prototype . info = function ( text ) {
111
- return this . stopAndPersist ( color . blue ( 'ℹ' ) , text ) ;
111
+ return this . stopAndPersist ( color . blue ( 'ℹ' ) , text ) ;
112
112
}
113
113
loading . prototype . stopAndPersist = function ( symbol , text ) {
114
- text = text || this . text
115
- this . stop ( ) ;
116
- this . stream . write ( ( symbol ? symbol + ' ' : ' ' ) + text + '\n' ) ;
117
- return this ;
114
+ text = text || this . text
115
+ this . stop ( ) ;
116
+ this . stream . write ( ( symbol ? symbol + ' ' : ' ' ) + text + '\n' ) ;
117
+ return this ;
118
118
}
119
119
120
120
module . exports = loading
0 commit comments