vmc 0.4.0.beta.12 → 0.4.0.beta.13

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.
@@ -1,190 +0,0 @@
1
- require "rbconfig"
2
-
3
- module VMC
4
- module Dots
5
- DOT_COUNT = 3
6
- DOT_TICK = 0.15
7
-
8
- class Skipper
9
- def initialize(&ret)
10
- @return = ret
11
- end
12
-
13
- def skip(&callback)
14
- @return.call("SKIPPED", :warning, callback)
15
- end
16
-
17
- def give_up(&callback)
18
- @return.call("GAVE UP", :bad, callback)
19
- end
20
-
21
- def fail(&callback)
22
- @return.call("FAILED", :error, callback)
23
- end
24
- end
25
-
26
- def with_progress(message)
27
- unless simple_output?
28
- print message
29
- dots!
30
- end
31
-
32
- skipper = Skipper.new do |status, color, callback|
33
- unless simple_output?
34
- stop_dots!
35
- puts "... #{c(status, color)}"
36
- end
37
-
38
- return callback && callback.call
39
- end
40
-
41
- begin
42
- res = yield skipper
43
- unless simple_output?
44
- stop_dots!
45
- puts "... #{c("OK", :good)}"
46
- end
47
- res
48
- rescue
49
- unless simple_output?
50
- stop_dots!
51
- puts "... #{c("FAILED", :error)}"
52
- end
53
-
54
- raise
55
- end
56
- end
57
-
58
- WINDOWS = !!(RbConfig::CONFIG['host_os'] =~ /mingw|mswin32|cygwin/)
59
-
60
- def color?
61
- !WINDOWS && $stdout.tty?
62
- end
63
-
64
- COLOR_CODES = {
65
- :black => 0,
66
- :red => 1,
67
- :green => 2,
68
- :yellow => 3,
69
- :blue => 4,
70
- :magenta => 5,
71
- :cyan => 6,
72
- :white => 7
73
- }
74
-
75
- DEFAULT_COLORS = {
76
- :name => :blue,
77
- :neutral => :blue,
78
- :good => :green,
79
- :bad => :red,
80
- :error => :magenta,
81
- :unknown => :cyan,
82
- :warning => :yellow,
83
- :instance => :yellow,
84
- :number => :green,
85
- :prompt => :blue,
86
- :yes => :green,
87
- :no => :red,
88
- :dim => :black,
89
- :default => :black
90
- }
91
-
92
- def user_colors
93
- return @user_colors if @user_colors
94
-
95
- colors = File.expand_path VMC::COLORS_FILE
96
-
97
- if File.exists? colors
98
- @user_colors = DEFAULT_COLORS.dup
99
-
100
- YAML.load_file(colors).each do |k, v|
101
- if k == true
102
- k = :yes
103
- elsif k == false
104
- k = :no
105
- else
106
- k = k.to_sym
107
- end
108
-
109
- @user_colors[k] = v.to_sym
110
- end
111
-
112
- @user_colors
113
- else
114
- @user_colors = DEFAULT_COLORS
115
- end
116
- end
117
-
118
- # colored text
119
- #
120
- # shouldn't use bright colors, as some color themes abuse
121
- # the bright palette (I'm looking at you, Solarized)
122
- def c(str, type)
123
- return str unless color?
124
-
125
- bright = false
126
- color = user_colors[type]
127
- if color =~ /bright-(.+)/
128
- bright = true
129
- color = $1.to_sym
130
- end
131
-
132
- return str unless color
133
-
134
- "\e[#{bright ? 9 : 3}#{COLOR_CODES[color]}m#{str}\e[0m"
135
- end
136
- module_function :c
137
-
138
- # bold text
139
- def b(str)
140
- return str unless color?
141
- "\e[1m#{str}\e[0m"
142
- end
143
- module_function :b
144
-
145
- def dots!
146
- @dots ||=
147
- Thread.new do
148
- before_sync = $stdout.sync
149
-
150
- $stdout.sync = true
151
-
152
- printed = false
153
- i = 1
154
- until @stop_dots
155
- if printed
156
- print "\b" * DOT_COUNT
157
- end
158
-
159
- print ("." * i).ljust(DOT_COUNT)
160
- printed = true
161
-
162
- if i == DOT_COUNT
163
- i = 0
164
- else
165
- i += 1
166
- end
167
-
168
- sleep DOT_TICK
169
- end
170
-
171
- if printed
172
- print "\b" * DOT_COUNT
173
- print " " * DOT_COUNT
174
- print "\b" * DOT_COUNT
175
- end
176
-
177
- $stdout.sync = before_sync
178
- @stop_dots = nil
179
- end
180
- end
181
-
182
- def stop_dots!
183
- return unless @dots
184
- return if @stop_dots
185
- @stop_dots = true
186
- @dots.join
187
- @dots = nil
188
- end
189
- end
190
- end