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:, &block) ⇒ Task

Returns a new instance of Task.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cli/ui/spinner/spin_group.rb', line 104

def initialize(title, final_glyph:, merged_output:, duplicate_output_to:, &block)
  @title = title
  @final_glyph = final_glyph
  @always_full_render = title =~ Formatter::SCAN_WIDGET
  @thread = Thread.new 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

#exceptionObject (readonly)

Returns the value of attribute exception.



85
86
87
# File 'lib/cli/ui/spinner/spin_group.rb', line 85

def exception
  @exception
end

#stderrObject (readonly)

Returns the value of attribute stderr.



79
80
81
# File 'lib/cli/ui/spinner/spin_group.rb', line 79

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



79
80
81
# File 'lib/cli/ui/spinner/spin_group.rb', line 79

def stdout
  @stdout
end

#successObject (readonly)

Returns the value of attribute success.



82
83
84
# File 'lib/cli/ui/spinner/spin_group.rb', line 82

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.



79
80
81
# File 'lib/cli/ui/spinner/spin_group.rb', line 79

def title
  @title
end

Instance Method Details

#checkObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cli/ui/spinner/spin_group.rb', line 130

def check
  return true if @done
  return false if @thread.alive?

  @done = true
  begin
    status = @thread.join.status
    @success = (status == false)
    @success = false if @thread.value == TASK_FAILED
  rescue => exc
    @exception = exc
    @success = false
  end

  @done
end

#interruptObject



194
195
196
# File 'lib/cli/ui/spinner/spin_group.rb', line 194

def interrupt
  @thread.raise(Interrupt)
end

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



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cli/ui/spinner/spin_group.rb', line 166

def render(index, force = true, width: CLI::UI::Terminal.width)
  @m.synchronize do
    if 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



185
186
187
188
189
190
191
# File 'lib/cli/ui/spinner/spin_group.rb', line 185

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