Class: Dex::UI::Spinner::SpinGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/dex/ui/spinner.rb

Defined Under Namespace

Classes: Task

Instance Method Summary collapse

Constructor Details

#initializeSpinGroup

Returns a new instance of SpinGroup.



22
23
24
25
26
# File 'lib/dex/ui/spinner.rb', line 22

def initialize
  @m = Mutex.new
  @consumed_lines = 0
  @tasks = []
end

Instance Method Details

#add(title, &block) ⇒ Object



96
97
98
99
100
# File 'lib/dex/ui/spinner.rb', line 96

def add(title, &block)
  @m.synchronize do
    @tasks << Task.new(title, &block)
  end
end

#debriefObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dex/ui/spinner.rb', line 138

def debrief
  @m.synchronize do
    @tasks.each do |task|
      next if task.success

      e = task.exception
      out = task.stdout
      err = task.stderr

      Dex::UI::Frame.open('Task Failed: ' + task.title, color: :red) do
        if e
          puts"#{e.class}: #{e.message}"
          puts "\tfrom #{e.backtrace.join("\n\tfrom ")}"
        end

        Dex::UI::Frame.divider('STDOUT')
        out = "(empty)" if out.strip.empty?
        puts out

        Dex::UI::Frame.divider('STDERR')
        err = "(empty)" if err.strip.empty?
        puts err
      end
    end
    @tasks.all?(&:success)
  end
end

#waitObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dex/ui/spinner.rb', line 102

def wait
  idx = 0

  loop do
    all_done = true

    @m.synchronize do
      Dex::UI.raw do
        @tasks.each.with_index do |task, int_index|
          nat_index = int_index + 1
          task_done = task.check
          all_done = false unless task_done

          if nat_index > @consumed_lines
            print(task.render(idx, true) + "\n")
            @consumed_lines += 1
          else
            offset = @consumed_lines - int_index
            move_to   = Dex::UI::ANSI.cursor_up(offset) + "\r"
            move_from = "\r" + Dex::UI::ANSI.cursor_down(offset)

            print(move_to + task.render(idx, idx.zero?) + move_from)
          end
        end
      end
    end

    break if all_done

    idx = (idx + 1) % GLYPHS.size
    sleep(PERIOD)
  end

  debrief
end