Class: CLI::UI::Spinner::SpinGroup::Task

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cli/ui/spinner/spin_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initialize(title, final_glyph:, merged_output:, duplicate_output_to:, work_queue:, &block) ⇒ Task

Returns a new instance of Task.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cli/ui/spinner/spin_group.rb', line 131

def initialize(title, final_glyph:, merged_output:, duplicate_output_to:, work_queue:, &block)
  @title = title
  @final_glyph = final_glyph
  @always_full_render = title =~ Formatter::SCAN_WIDGET
  @future = work_queue.enqueue do
    cap = CLI::UI::StdoutRouter::Capture.new(
      merged_output: merged_output, duplicate_output_to: duplicate_output_to,
    ) { block.call(self) }
    begin
      cap.run
    ensure
      @stdout = cap.stdout
      @stderr = cap.stderr
    end
  end

  @m = Mutex.new
  @force_full_render = false
  @done = false
  @exception = nil
  @success = false
end

Instance Attribute Details

#doneObject (readonly)

Returns the value of attribute done.



108
109
110
# File 'lib/cli/ui/spinner/spin_group.rb', line 108

def done
  @done
end

#exceptionObject (readonly)

Returns the value of attribute exception.



111
112
113
# File 'lib/cli/ui/spinner/spin_group.rb', line 111

def exception
  @exception
end

#stderrObject (readonly)

Returns the value of attribute stderr.



102
103
104
# File 'lib/cli/ui/spinner/spin_group.rb', line 102

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



102
103
104
# File 'lib/cli/ui/spinner/spin_group.rb', line 102

def stdout
  @stdout
end

#successObject (readonly)

Returns the value of attribute success.



105
106
107
# File 'lib/cli/ui/spinner/spin_group.rb', line 105

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.



102
103
104
# File 'lib/cli/ui/spinner/spin_group.rb', line 102

def title
  @title
end

Instance Method Details

#checkObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cli/ui/spinner/spin_group.rb', line 162

def check
  return true if @done
  return false unless @future.completed?

  @done = true
  begin
    result = @future.value
    @success = true
    @success = false if result == TASK_FAILED
  rescue => exc
    @exception = exc
    @success = false
  end

  @on_done&.call(self)

  @done
end

#on_done(&block) ⇒ Object



155
156
157
# File 'lib/cli/ui/spinner/spin_group.rb', line 155

def on_done(&block)
  @on_done = block
end

#render(index, force = true, width: CLI::UI::Terminal.width) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cli/ui/spinner/spin_group.rb', line 200

def render(index, force = true, width: CLI::UI::Terminal.width)
  @m.synchronize do
    if !CLI::UI.enable_cursor? || force || @always_full_render || @force_full_render
      full_render(index, width)
    else
      partial_render(index)
    end
  ensure
    @force_full_render = false
  end
end

#update_title(new_title) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/cli/ui/spinner/spin_group.rb', line 219

def update_title(new_title)
  @m.synchronize do
    @always_full_render = new_title =~ Formatter::SCAN_WIDGET
    @title = new_title
    @force_full_render = true
  end
end