class CursesMenu::CursesRow

Definition of a row that stores for each cell the string and color information to be displayed

Public Class Methods

new(cells, separator: ' ') click to toggle source

Constructor

Parameters
  • cells (Hash< Symbol, Hash<Symbol,Object> >): For each cell id (ordered), the cell info:

    • text (String): Text associated to this cell

    • color_pair (Integer): Associated color pair [optional]

    • begin_with (String): String to prepend to the text [default: '']

    • end_with (String): String to append to the text [default: '']

    • fixed_size (Integer): Number of characters this cell will take, or nil if no limit. [default: nil]

    • justify (Symbol): Text justification (only used when fixed_size is not nil). Values can be: [default: :left]

      • left: Left justified

      • right: Right justified

    • pad (String): Text to be used to pad the cell content (only used when fixed_size is not nil) [default: ' ']

  • separator (String): Separator used between cells [default: ' ']

# File lib/curses_menu/curses_row.rb, line 20
def initialize(cells, separator: ' ')
  @cells = cells
  @separator = separator
end

Public Instance Methods

cells_order(cells, unknown_cells: nil) click to toggle source

Change the cells order

Parameters
  • cells (Array<Symbol>): The ordered list of cells to filter

  • unknown_cells (String or Hash<Symbol,Object>): Content to put in unknown cells (as a String or properties like in initialize), or nil to not add them. [default: nil]

# File lib/curses_menu/curses_row.rb, line 30
def cells_order(cells, unknown_cells: nil)
  new_cells = {}
  cells.each do |cell_id|
    if @cells.key?(cell_id)
      new_cells[cell_id] = @cells[cell_id]
    elsif !unknown_cells.nil?
      new_cells[cell_id] = unknown_cells.is_a?(String) ? { text: unknown_cells } : unknown_cells
    end
  end
  @cells = new_cells
end
change_cells(cells) click to toggle source

Change properties of a set of cells

Parameters
  • cells (Hash<Symbol, Hash<Symbol,Object> >): The cells properties to change, per cell id. Possible properties are the ones given in the initialize method.

# File lib/curses_menu/curses_row.rb, line 46
def change_cells(cells)
  cells.each do |cell_id, cell_info|
    raise "Unknown cell #{cell_id}" unless @cells.key?(cell_id)

    @cells[cell_id].merge!(cell_info)
    @cells[cell_id].delete(:cache_rendered_text)
  end
end
print_on(window, from: 0, to: nil, default_color_pair: COLORS_LINE, force_color_pair: nil, pad: nil, add_nl: true, single_line: false) click to toggle source

Print this row into a window

Parameters
  • window (Window): Curses window to print on

  • from (Integer): From index to be displayed [default: 0]

  • to (Integer): To index to be displayed [default: total size]

  • default_color_pair (Integer): Default color pair to use if no color information is provided [default: COLORS_LINE]

  • force_color_pair (Integer): Force color pair to use, or nil to not force [default: nil]

  • pad (String or nil): Pad the line to the row extent with the given string, or nil for no padding. [default: nil]

  • add_nl (Boolean): If true, then add a new line at the end [default: true]

  • single_line (Boolean): If true, then make sure the print does not exceed the line [default: false]

size(cells: @cells.keys) click to toggle source

Get the size of the total string of such row.

Parameters
  • cells (Array<Symbol>): The list of cells to consider for the size [default: @cells.keys]

Result
  • Integer: Row size

# File lib/curses_menu/curses_row.rb, line 61
def size(cells: @cells.keys)
  result = @separator.size * (cells.size - 1)
  cells.each do |cell_id|
    result += cell_text(cell_id).size
  end
  result
end

Private Instance Methods

cell_text(cell_id) click to toggle source

Get a cell's text. Cache it to not compute it several times.

Parameters
  • cell_id (Symbol): Cell id to get text for

Result
  • String: The cell's text

# File lib/curses_menu/curses_row.rb, line 120
def cell_text(cell_id)
  unless @cells[cell_id].key?(:cache_rendered_text)
    begin_str = "#{@cells[cell_id][:begin_with] || ''}#{@cells[cell_id][:text]}"
    end_str = @cells[cell_id][:end_with] || ''
    @cells[cell_id][:cache_rendered_text] =
      if @cells[cell_id][:fixed_size]
        text = "#{begin_str[0..@cells[cell_id][:fixed_size] - end_str.size - 1]}#{end_str}"
        remaining_size = @cells[cell_id][:fixed_size] - text.size
        if remaining_size.positive?
          padding = ((@cells[cell_id][:pad] || ' ') * remaining_size)[0..remaining_size - 1]
          justify = @cells[cell_id][:justify] || :left
          case justify
          when :left
            "#{text}#{padding}"
          when :right
            "#{padding}#{text}"
          else
            raise "Unknown justify decorator: #{justify}"
          end
        else
          text[0..@cells[cell_id][:fixed_size] - 1]
        end
      else
        "#{begin_str}#{end_str}"
      end
  end
  @cells[cell_id][:cache_rendered_text]
end