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

Returns a new instance of Task.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cli/ui/spinner/spin_group.rb', line 61

def initialize(title, &block)
  @title = title
  @always_full_render = title =~ Formatter::SCAN_WIDGET
  @thread = Thread.new do
    cap = CLI::UI::StdoutRouter::Capture.new(with_frame_inset: false) { 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.



50
51
52
# File 'lib/cli/ui/spinner/spin_group.rb', line 50

def exception
  @exception
end

#stderrObject (readonly)

Returns the value of attribute stderr.



44
45
46
# File 'lib/cli/ui/spinner/spin_group.rb', line 44

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



44
45
46
# File 'lib/cli/ui/spinner/spin_group.rb', line 44

def stdout
  @stdout
end

#successObject (readonly)

Returns the value of attribute success.



47
48
49
# File 'lib/cli/ui/spinner/spin_group.rb', line 47

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.



44
45
46
# File 'lib/cli/ui/spinner/spin_group.rb', line 44

def title
  @title
end

Instance Method Details

#checkObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cli/ui/spinner/spin_group.rb', line 84

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



148
149
150
# File 'lib/cli/ui/spinner/spin_group.rb', line 148

def interrupt
  @thread.raise(Interrupt)
end

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



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cli/ui/spinner/spin_group.rb', line 120

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



139
140
141
142
143
144
145
# File 'lib/cli/ui/spinner/spin_group.rb', line 139

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