Module: Dev::UI::Frame

Defined in:
lib/dev/ui/frame.rb

Defined Under Namespace

Modules: FrameStack

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

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



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dev/ui/frame.rb', line 67

def close(text, color: DEFAULT_FRAME_COLOR, elapsed: nil)
  color = Dev::UI.resolve_color(color)

  FrameStack.pop
  kwargs = {}
  if elapsed
    kwargs[:right_text] = "(#{elapsed.round(2)}s)"
  end
  Dev::UI.raw do
    puts edge(text, color: color, first: Dev::UI::Box::Heavy::BL, **kwargs)
  end
end

.divider(text, color: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/dev/ui/frame.rb', line 80

def divider(text, color: nil)
  color = Dev::UI.resolve_color(color)
  item  = Dev::UI.resolve_color(FrameStack.pop)

  Dev::UI.raw do
    puts edge(text, color: (color || item), first: Dev::UI::Box::Heavy::DIV)
  end
  FrameStack.push(item)
end

.open(text, color: DEFAULT_FRAME_COLOR, failure_text: nil, success_text: nil, timing: nil) ⇒ Object

Can be invoked in two ways: block and blockless In block form, the frame is closed automatically when the block returns In blockless form, caller MUST call Frame.close when the frame is

logically done.

blockless form is strongly discouraged in cases where block form can be

made to work.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dev/ui/frame.rb', line 15

def open(
  text,
  color: DEFAULT_FRAME_COLOR,
  failure_text: nil,
  success_text: nil,
  timing:       nil
)
  color = Dev::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.nil?
      raise ArgumentError, "timing is not compatible with blockless invocation"
    end
  end

  timing = true if timing.nil?

  t_start = Time.now.to_f
  Dev::UI.raw do
    puts edge(text, color: color, first: Dev::UI::Box::Heavy::TL)
  end
  FrameStack.push(color)

  return unless block_given?

  closed = false
  begin
    success = false
    success = yield
  rescue Exception
    closed = true
    t_diff = timing ? (Time.now.to_f - t_start) : nil
    close(failure_text, color: :red, elapsed: t_diff)
    raise
  else
    success
  ensure
    unless closed
      t_diff = timing ? (Time.now.to_f - t_start) : nil
      if 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: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dev/ui/frame.rb', line 90

def prefix(color: nil)
  pfx = String.new
  items = FrameStack.items
  items[0..-2].each do |item|
    pfx << Dev::UI.resolve_color(item).code << Dev::UI::Box::Heavy::VERT
  end
  if item = items.last
    c = Thread.current[:devui_frame_color_override] || color || item
    pfx << Dev::UI.resolve_color(c).code \
      << Dev::UI::Box::Heavy::VERT << ' ' << Dev::UI::Color::RESET.code
  end
  pfx
end

.prefix_widthObject



112
113
114
115
# File 'lib/dev/ui/frame.rb', line 112

def prefix_width
  w = FrameStack.items.size
  w.zero? ? 0 : w + 1
end

.with_frame_color_override(color) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/dev/ui/frame.rb', line 104

def with_frame_color_override(color)
  prev = Thread.current[:devui_frame_color_override]
  Thread.current[:devui_frame_color_override] = color
  yield
ensure
  Thread.current[:devui_frame_color_override] = prev
end