module CLI::UI::Frame::FrameStack

Constants

COLOR_ENVVAR
STYLE_ENVVAR

Public Class Methods

items() click to toggle source

Fetch all items off the frame stack

# 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
pop() click to toggle source

Removes and returns the last stack item off the stack

# 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) click to toggle source

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

# 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

Private Class Methods

serialize(items) click to toggle source

Serializes the item stack into two ENV variables.

This is done to preserve backward compatibility with earlier versions of cli/ui. This ensures that any code that relied upon previous stack behavior should continue to work.

# File lib/cli/ui/frame/frame_stack.rb, line 82
def serialize(items)
  colors = []
  styles = []

  items.each do |item|
    colors << item.color.name
    styles << item.frame_style.name
  end

  ENV[COLOR_ENVVAR] = colors.join(':')
  ENV[STYLE_ENVVAR] = styles.join(':')
end