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


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

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

  @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


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

def exception
  @exception
end

#stderrObject (readonly)

Returns the value of attribute stderr


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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout


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

def stdout
  @stdout
end

#successObject (readonly)

Returns the value of attribute success


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

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title


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

def title
  @title
end

Instance Method Details

#checkObject

Checks if a task is finished


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 64

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


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cli/ui/spinner/spin_group.rb', line 99

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

Update the spinner title

Attributes

  • title - title to change the spinner to


117
118
119
120
121
122
123
# File 'lib/cli/ui/spinner/spin_group.rb', line 117

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