Module: CLI::UI::Frame

Extended by:
T::Sig
Defined in:
lib/cli/ui/frame.rb,
lib/cli/ui/frame/frame_stack.rb,
lib/cli/ui/frame/frame_style.rb,
lib/cli/ui/frame/frame_style/box.rb,
lib/cli/ui/frame/frame_style/bracket.rb

Defined Under Namespace

Modules: FrameStack, FrameStyle Classes: UnnestedFrameException

Constant Summary collapse

DEFAULT_FRAME_COLOR =
CLI::UI.resolve_color(:cyan)

Class Method Summary collapse

Methods included from T::Sig

sig

Class Method Details

.close(text, color: nil, elapsed: nil, frame_style: nil) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cli/ui/frame.rb', line 215

def close(text, color: nil, elapsed: nil, frame_style: nil)
  fs_item = FrameStack.pop
  raise UnnestedFrameException, 'No frame nesting to unnest' unless fs_item

  close_color = CLI::UI.resolve_color(color || fs_item.color)
  frame_style = CLI::UI.resolve_style(frame_style || fs_item.frame_style)
  elapsed_string = elapsed ? "(#{elapsed.round(2)}s)" : nil

  CLI::UI.raw do
    print(prefix.chop)
    puts frame_style.close(text.to_s, color: close_color, right_text: elapsed_string)
  end
end

.divider(text, color: nil, frame_style: nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cli/ui/frame.rb', line 168

def divider(text, color: nil, frame_style: nil)
  fs_item = FrameStack.pop
  raise UnnestedFrameException, 'No frame nesting to unnest' unless fs_item

  divider_color = CLI::UI.resolve_color(color || fs_item.color)
  frame_style = CLI::UI.resolve_style(frame_style || fs_item.frame_style)

  CLI::UI.raw do
    print(prefix.chop)
    puts frame_style.divider(text.to_s, color: divider_color)
  end

  FrameStack.push(fs_item)
end

.frame_styleObject



19
20
21
# File 'lib/cli/ui/frame.rb', line 19

def frame_style
  @frame_style ||= FrameStyle::Box
end

.frame_style=(frame_style) ⇒ Object



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

def frame_style=(frame_style)
  @frame_style = CLI::UI.resolve_style(frame_style)
end

.open(text, color: DEFAULT_FRAME_COLOR, failure_text: nil, success_text: nil, timing: block_given?, , frame_style: self.frame_style, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cli/ui/frame.rb', line 88

def open(
  text,
  color: DEFAULT_FRAME_COLOR,
  failure_text: nil,
  success_text: nil,
  timing: block_given?,
  frame_style: self.frame_style,
  &block
)
  frame_style = CLI::UI.resolve_style(frame_style)
  color = CLI::UI.resolve_color(color)

  unless block_given?
    if failure_text
      raise ArgumentError, 'failure_text is not compatible with blockless invocation'
    elsif success_text
      raise ArgumentError, 'success_text is not compatible with blockless invocation'
    elsif timing
      raise ArgumentError, 'timing is not compatible with blockless invocation'
    end
  end

  t_start = Time.now
  CLI::UI.raw do
    print(prefix.chop)
    puts frame_style.start(text, color: color)
  end
  FrameStack.push(color: color, style: frame_style)

  return unless block_given?

  closed = false
  begin
    success = false
    success = yield
  rescue
    closed = true
    t_diff = elapsed(t_start, timing)
    close(failure_text, color: :red, elapsed: t_diff)
    raise
  else
    success
  ensure
    unless closed
      t_diff = elapsed(t_start, timing)
      if T.unsafe(success) != false
        close(success_text, color: color, elapsed: t_diff)
      else
        close(failure_text, color: :red, elapsed: t_diff)
      end
    end
  end
end

.prefix(color: Thread.current[:cliui_frame_color_override]) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cli/ui/frame.rb', line 236

def prefix(color: Thread.current[:cliui_frame_color_override])
  +''.tap do |output|
    items = FrameStack.items

    items[0..-2].to_a.each do |item|
      output << item.color.code << item.frame_style.prefix
    end

    if (item = items.last)
      final_color = color || item.color
      output << CLI::UI.resolve_color(final_color).code \
        << item.frame_style.prefix \
        << ' ' \
        << CLI::UI::Color::RESET.code
    end
  end
end

.prefix_widthObject



256
257
258
259
260
261
262
# File 'lib/cli/ui/frame.rb', line 256

def prefix_width
  w = FrameStack.items.reduce(0) do |width, item|
    width + item.frame_style.prefix_width
  end

  w.zero? ? w : w + 1
end

.with_frame_color_override(color, &block) ⇒ Object



275
276
277
278
279
280
281
# File 'lib/cli/ui/frame.rb', line 275

def with_frame_color_override(color, &block)
  prev = Thread.current[:cliui_frame_color_override]
  Thread.current[:cliui_frame_color_override] = color
  yield
ensure
  Thread.current[:cliui_frame_color_override] = prev
end