Module: Dex::UI::Frame

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

Defined Under Namespace

Modules: FrameStack

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dex/ui/frame.rb', line 62

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

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

.divider(text, color: nil) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/dex/ui/frame.rb', line 75

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

  Dex::UI.raw do
    puts edge(text, color: (color || item), first: Dex::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
# File 'lib/dex/ui/frame.rb', line 15

def open(
  text,
  color: DEFAULT_FRAME_COLOR,
  failure_text: nil,
  success_text: nil,
  timing:       nil
)
  color = Dex::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
  Dex::UI.raw do
    puts edge(text, color: color, first: Dex::UI::Box::Heavy::TL)
  end
  FrameStack.push(color)

  return unless block_given?

  begin
    success = false
    success = yield
  rescue
    t_diff = timing ? (Time.now.to_f - t_start) : nil
    close(failure_text, color: :red, elapsed: t_diff)
    raise
  else
    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
    success
  end
end

.prefix(color: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dex/ui/frame.rb', line 85

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

.with_frame_color_override(color) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/dex/ui/frame.rb', line 99

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