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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, &block) ⇒ Task

Initializes a new Task This is managed entirely internally by SpinGroup

Attributes

  • title - Title of the task

  • block - Block for the task, will be provided with an instance of the spinner



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cli/ui/spinner/spin_group.rb', line 41

def initialize(title, &block)
  @title = title
  @always_full_render = title =~ Formatter::SCAN_WIDGET
  @thread = Thread.new do
    cap = CLI::UI::StdoutRouter::Capture.new(self, with_frame_inset: false, &block)
    begin
      cap.run
    ensure
      @stdout = cap.stdout
      @stderr = cap.stderr
    end
  end

  @force_full_render = false
  @done      = false
  @exception = nil
  @success   = false
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



31
32
33
# File 'lib/cli/ui/spinner/spin_group.rb', line 31

def exception
  @exception
end

#stderrObject (readonly)

Returns the value of attribute stderr.



31
32
33
# File 'lib/cli/ui/spinner/spin_group.rb', line 31

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



31
32
33
# File 'lib/cli/ui/spinner/spin_group.rb', line 31

def stdout
  @stdout
end

#successObject (readonly)

Returns the value of attribute success.



31
32
33
# File 'lib/cli/ui/spinner/spin_group.rb', line 31

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.



31
32
33
# File 'lib/cli/ui/spinner/spin_group.rb', line 31

def title
  @title
end

Instance Method Details

#checkObject

Checks if a task is finished



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

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

Re-renders the task if required:

We try to be as lazy as possible in re-rendering the full line. The spinner rune will change on each render for the most part, but the body text will rarely have changed. If the body text has changed, we set @force_full_render.

Further, if the title string includes any CLI::UI::Widgets, we assume that it may change from render to render, since those evaluate more dynamically than the rest of our format codes, which are just text formatters. This is controlled by @always_full_render.

Attributes

  • index - index of the task

  • force - force rerender of the task

  • width - current terminal width to format for



97
98
99
100
101
102
103
104
105
# File 'lib/cli/ui/spinner/spin_group.rb', line 97

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

#update_title(new_title) ⇒ Object

Update the spinner title

Attributes

  • title - title to change the spinner to



113
114
115
116
117
# File 'lib/cli/ui/spinner/spin_group.rb', line 113

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