Module: CF::Dots

Included in:
Interactive
Defined in:
lib/cf/cli/dots.rb

Defined Under Namespace

Classes: Skipper

Constant Summary collapse

DOT_COUNT =
3
DOT_TICK =
0.15
COLOR_CODES =
{
  :black => 0,
  :red => 1,
  :green => 2,
  :yellow => 3,
  :blue => 4,
  :magenta => 5,
  :cyan => 6,
  :white => 7
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.b(str) ⇒ Object

bold text



82
83
84
85
# File 'lib/cf/cli/dots.rb', line 82

def b(str)
  return str unless color?
  "\e[1m#{str}\e[0m"
end

.c(str, color) ⇒ Object

colored text

shouldn’t use bright colors, as some color themes abuse the bright palette (I’m looking at you, Solarized)



75
76
77
78
# File 'lib/cf/cli/dots.rb', line 75

def c(str, color)
  return str unless color?
  "\e[3#{COLOR_CODES[color]}m#{str}\e[0m"
end

Instance Method Details

#color?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cf/cli/dots.rb', line 56

def color?
  $stdout.tty?
end

#dots!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cf/cli/dots.rb', line 88

def dots!
  @dots ||=
    Thread.new do
      before_sync = $stdout.sync

      $stdout.sync = true

      printed = false
      i = 1
      until @stop_dots
        if printed
          print "\b" * DOT_COUNT
        end

        print ("." * i).ljust(DOT_COUNT)
        printed = true

        if i == DOT_COUNT
          i = 0
        else
          i += 1
        end

        sleep DOT_TICK
      end

      if printed
        print "\b" * DOT_COUNT
        print " " * DOT_COUNT
        print "\b" * DOT_COUNT
      end

      $stdout.sync = before_sync
      @stop_dots = nil
    end
end

#stop_dots!Object



125
126
127
128
129
130
131
# File 'lib/cf/cli/dots.rb', line 125

def stop_dots!
  return unless @dots
  return if @stop_dots
  @stop_dots = true
  @dots.join
  @dots = nil
end

#with_progress(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cf/cli/dots.rb', line 24

def with_progress(message)
  unless simple_output?
    print message
    dots!
  end

  skipper = Skipper.new do |status, color, callback|
    unless simple_output?
      stop_dots!
      puts "... #{c(status, color)}"
    end

    return callback && callback.call
  end

  begin
    res = yield skipper
    unless simple_output?
      stop_dots!
      puts "... #{c("OK", :green)}"
    end
    res
  rescue
    unless simple_output?
      stop_dots!
      puts "... #{c("FAILED", :red)}"
    end

    raise
  end
end