Module: CLI::UI::Frame::FrameStack

Defined in:
lib/cli/ui/frame/frame_stack.rb

Defined Under Namespace

Classes: StackItem

Constant Summary collapse

COLOR_ENVVAR =
'CLI_FRAME_STACK'
STYLE_ENVVAR =
'CLI_STYLE_STACK'

Class Method Summary collapse

Class Method Details

.itemsObject

Fetch all items off the frame stack


19
20
21
22
23
24
25
26
# File 'lib/cli/ui/frame/frame_stack.rb', line 19

def items
  colors = ENV.fetch(COLOR_ENVVAR, '').split(':').map(&:to_sym)
  styles = ENV.fetch(STYLE_ENVVAR, '').split(':').map(&:to_sym)

  colors.length.times.map do |i|
    StackItem.new(colors[i], styles[i] || Frame.frame_style)
  end
end

.popObject

Removes and returns the last stack item off the stack


66
67
68
69
70
71
72
73
# File 'lib/cli/ui/frame/frame_stack.rb', line 66

def pop
  curr = items
  ret = curr.pop

  serialize(curr)

  ret.nil? ? nil : ret
end

.push(item = nil, color: nil, style: nil) ⇒ Object

Push a new item onto the frame stack.

Either an item or a :color/:style pair should be pushed onto the stack.

Attributes

  • item a StackItem to push onto the stack. Defaults to nil

Options

  • :color the color of the new stack item. Defaults to nil

  • :style the style of the new stack item. Defaults to nil

Raises

If both an item and a color/style pair are given, raises an ArgumentError If the given item is not a StackItem, raises an ArgumentError


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cli/ui/frame/frame_stack.rb', line 46

def push(item = nil, color: nil, style: nil)
  unless item.nil?
    unless item.is_a?(StackItem)
      raise ArgumentError, 'item must be a StackItem'
    end

    unless color.nil? && style.nil?
      raise ArgumentError, 'Must give one of item or color: and style:'
    end
  end

  item ||= StackItem.new(color, style)

  curr = items
  curr << item

  serialize(curr)
end