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.


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

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.


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

def exception
  @exception
end

#stderrObject (readonly)

Returns the value of attribute stderr.


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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.


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

def stdout
  @stdout
end

#successObject (readonly)

Returns the value of attribute success.


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

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.


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

def title
  @title
end

Instance Method Details

#checkObject


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

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

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


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

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


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

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