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, to: $stdout) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cli/ui/frame.rb', line 231

def close(text, color: nil, elapsed: nil, frame_style: nil, to: $stdout)
  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
    to.print(prefix.chop)
    to.puts(frame_style.close(text.to_s, color: close_color, right_text: elapsed_string))
  end
end

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cli/ui/frame.rb', line 181

def divider(text, color: nil, frame_style: nil, to: $stdout)
  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
    to.print(prefix.chop)
    to.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, to: $stdout, &block) ⇒ Object



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
141
142
143
144
# File 'lib/cli/ui/frame.rb', line 91

def open(
  text,
  color: DEFAULT_FRAME_COLOR,
  failure_text: nil,
  success_text: nil,
  timing: block_given?,
  frame_style: self.frame_style,
  to: $stdout,
  &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
    to.print(prefix.chop)
    to.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, to: to)
    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, to: to)
      else
        close(failure_text, color: :red, elapsed: t_diff, to: to)
      end
    end
  end
end

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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/cli/ui/frame.rb', line 252

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



272
273
274
275
276
277
278
# File 'lib/cli/ui/frame.rb', line 272

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



291
292
293
294
295
296
297
# File 'lib/cli/ui/frame.rb', line 291

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