interact 0.4.8 → 0.5.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.
- data/lib/interact/pretty.rb +93 -0
- data/lib/interact/progress.rb +114 -0
- data/lib/interact/version.rb +1 -1
- metadata +7 -5
@@ -0,0 +1,93 @@
|
|
1
|
+
require "rbconfig"
|
2
|
+
|
3
|
+
# Mix in to your Interactive class to enable user-toggleable colors.
|
4
|
+
#
|
5
|
+
# Redefine color_enabled? to control color enabling/disabling. Colors will be
|
6
|
+
# auto-disabled if the platform is Windows or if $stdout is not a tty.
|
7
|
+
#
|
8
|
+
# Redefine user_colors to return a hash from tags to color, e.g. from a user's
|
9
|
+
# color config file.
|
10
|
+
module Interact
|
11
|
+
module Pretty
|
12
|
+
WINDOWS = !!(RbConfig::CONFIG['host_os'] =~ /mingw|mswin32|cygwin/)
|
13
|
+
|
14
|
+
COLOR_CODES = {
|
15
|
+
:black => 0,
|
16
|
+
:red => 1,
|
17
|
+
:green => 2,
|
18
|
+
:yellow => 3,
|
19
|
+
:blue => 4,
|
20
|
+
:magenta => 5,
|
21
|
+
:cyan => 6,
|
22
|
+
:white => 7
|
23
|
+
}
|
24
|
+
|
25
|
+
DEFAULT_COLORS = {
|
26
|
+
:name => :blue,
|
27
|
+
:neutral => :blue,
|
28
|
+
:good => :green,
|
29
|
+
:bad => :red,
|
30
|
+
:error => :magenta,
|
31
|
+
:unknown => :cyan,
|
32
|
+
:warning => :yellow,
|
33
|
+
:instance => :yellow,
|
34
|
+
:number => :green,
|
35
|
+
:prompt => :blue,
|
36
|
+
:yes => :green,
|
37
|
+
:no => :red
|
38
|
+
}
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# override with e.g. option(:color), or whatever toggle you use
|
43
|
+
def color_enabled?
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
# use colors?
|
48
|
+
def color?
|
49
|
+
color_enabled? && !WINDOWS && $stdout.tty?
|
50
|
+
end
|
51
|
+
|
52
|
+
# redefine to control the tag -> color settings
|
53
|
+
def user_colors
|
54
|
+
DEFAULT_COLORS
|
55
|
+
end
|
56
|
+
|
57
|
+
# colored text
|
58
|
+
#
|
59
|
+
# shouldn't use bright colors, as some color themes abuse
|
60
|
+
# the bright palette (I'm looking at you, Solarized)
|
61
|
+
def c(str, type)
|
62
|
+
return str unless color?
|
63
|
+
|
64
|
+
bright = false
|
65
|
+
color = user_colors[type]
|
66
|
+
if color.to_s =~ /bright-(.+)/
|
67
|
+
bright = true
|
68
|
+
color = $1.to_sym
|
69
|
+
end
|
70
|
+
|
71
|
+
return str unless color
|
72
|
+
|
73
|
+
code = "\e[#{bright ? 9 : 3}#{COLOR_CODES[color]}m"
|
74
|
+
"#{code}#{str.to_s.gsub("\e[0m", "\e[0m#{code}")}\e[0m"
|
75
|
+
end
|
76
|
+
|
77
|
+
# bold text
|
78
|
+
def b(str)
|
79
|
+
return str unless color?
|
80
|
+
|
81
|
+
code = "\e[1m"
|
82
|
+
"#{code}#{str.to_s.gsub("\e[0m", "\e[0m#{code}")}\e[0m"
|
83
|
+
end
|
84
|
+
|
85
|
+
# dim text
|
86
|
+
def d(str)
|
87
|
+
return str unless color?
|
88
|
+
|
89
|
+
code = "\e[2m"
|
90
|
+
"#{code}#{str.to_s.gsub("\e[0m", "\e[0m#{code}")}\e[0m"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require "interact/pretty"
|
2
|
+
|
3
|
+
module Interact
|
4
|
+
module Progress
|
5
|
+
include Pretty
|
6
|
+
|
7
|
+
module Dots
|
8
|
+
class << self
|
9
|
+
DOT_COUNT = 3
|
10
|
+
DOT_TICK = 0.15
|
11
|
+
|
12
|
+
def start!
|
13
|
+
@dots ||=
|
14
|
+
Thread.new do
|
15
|
+
before_sync = $stdout.sync
|
16
|
+
|
17
|
+
$stdout.sync = true
|
18
|
+
|
19
|
+
printed = false
|
20
|
+
i = 1
|
21
|
+
until @stop_dots
|
22
|
+
if printed
|
23
|
+
print "\b" * DOT_COUNT
|
24
|
+
end
|
25
|
+
|
26
|
+
print ("." * i).ljust(DOT_COUNT)
|
27
|
+
printed = true
|
28
|
+
|
29
|
+
if i == DOT_COUNT
|
30
|
+
i = 0
|
31
|
+
else
|
32
|
+
i += 1
|
33
|
+
end
|
34
|
+
|
35
|
+
sleep DOT_TICK
|
36
|
+
end
|
37
|
+
|
38
|
+
if printed
|
39
|
+
print "\b" * DOT_COUNT
|
40
|
+
print " " * DOT_COUNT
|
41
|
+
print "\b" * DOT_COUNT
|
42
|
+
end
|
43
|
+
|
44
|
+
$stdout.sync = before_sync
|
45
|
+
@stop_dots = nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def stop!
|
50
|
+
return unless @dots
|
51
|
+
return if @stop_dots
|
52
|
+
@stop_dots = true
|
53
|
+
@dots.join
|
54
|
+
@dots = nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Skipper
|
60
|
+
def initialize(&ret)
|
61
|
+
@return = ret
|
62
|
+
end
|
63
|
+
|
64
|
+
def skip(&callback)
|
65
|
+
@return.call("SKIPPED", :warning, callback)
|
66
|
+
end
|
67
|
+
|
68
|
+
def give_up(&callback)
|
69
|
+
@return.call("GAVE UP", :bad, callback)
|
70
|
+
end
|
71
|
+
|
72
|
+
def fail(&callback)
|
73
|
+
@return.call("FAILED", :error, callback)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# override to determine whether to show progress
|
78
|
+
def quiet?
|
79
|
+
false
|
80
|
+
end
|
81
|
+
|
82
|
+
def with_progress(message)
|
83
|
+
unless quiet?
|
84
|
+
print message
|
85
|
+
Dots.start!
|
86
|
+
end
|
87
|
+
|
88
|
+
skipper = Skipper.new do |status, color, callback|
|
89
|
+
unless quiet?
|
90
|
+
Dots.stop!
|
91
|
+
puts "... #{c(status, color)}"
|
92
|
+
end
|
93
|
+
|
94
|
+
return callback && callback.call
|
95
|
+
end
|
96
|
+
|
97
|
+
begin
|
98
|
+
res = yield skipper
|
99
|
+
unless quiet?
|
100
|
+
Dots.stop!
|
101
|
+
puts "... #{c("OK", :good)}"
|
102
|
+
end
|
103
|
+
res
|
104
|
+
rescue
|
105
|
+
unless quiet?
|
106
|
+
Dots.stop!
|
107
|
+
puts "... #{c("FAILED", :error)}"
|
108
|
+
end
|
109
|
+
|
110
|
+
raise
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/lib/interact/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interact
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Alex Suraci
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-11-28 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -60,6 +60,8 @@ files:
|
|
60
60
|
- README.md
|
61
61
|
- Rakefile
|
62
62
|
- lib/interact/interactive.rb
|
63
|
+
- lib/interact/pretty.rb
|
64
|
+
- lib/interact/progress.rb
|
63
65
|
- lib/interact/rewindable.rb
|
64
66
|
- lib/interact/version.rb
|
65
67
|
- lib/interact.rb
|