module Amun::Helpers::Colors

Colors is responsible for registering new colors pairs (foreground and background) associate the pair with a name, then use that pair for the next text printed on screen, also contains styles constants could be passed to use to print bold or underline text for example

it's important to understand that the number of pairs that coul'd be registered is limited to a number the current terminal specify, so registering, more color pairs than allowed will result in a RuntimeError.

color values could be taken from the xterm-256 color schema from here: upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg

Constants

ALTCHARSET
BOLD
CHARTEXT
COLOR
COLORS
DEFAULT_COLOR
DIM
HORIZONTAL
INVIS
LEFT
LOW
NORMAL
PROTECT
REVERSE
STANDOUT
TOP
UNDERLINE
VERTICAL

Public Instance Methods

print(curses_window, *strings) click to toggle source

print string in Curses window in the choosen color and style

register(name, foreground, background) click to toggle source

register or update a color pair with foreground and background colors, this method will raise RuntimeError if we exceeded the limit of color pairs allowed by the terminal

name(Symbol)

name of pair, could be used with the use method

foreground(Number)

foreground color in current terminal color schema

background(Number)

background color in current terminal color schema

# File lib/amun/helpers/colors.rb, line 55
def register(name, foreground, background)
  if !COLORS.key?(name) && COLORS.size >= Curses.color_pairs - 1
    raise ColorLimitExceeded, "Can't register color: #{name}, max: #{Curses.color_pairs}"
  end

  Curses.init_pair(COLORS[name], foreground, background)
end
register_default(name, foreground, background) click to toggle source

works like register but doesn't override your color if it's already registered, this is a better method for any module to use to define colors register should be used for redefining a color pair values

# File lib/amun/helpers/colors.rb, line 72
def register_default(name, foreground, background)
  return if registered? name
  register(name, foreground, background)
end
registered?(name) click to toggle source

check if a color pair is registered or not, returns true if registered

# File lib/amun/helpers/colors.rb, line 64
def registered?(name)
  COLORS.key? name
end
use(curses_window, name, type = NORMAL) click to toggle source

use color pair for the next printed text

window(Curses Window)

that we need to change it's colors

name(Symbol)

a color pair name registered before with register

type(Colors::Constant)

a text style constant

defined in Colors, that manipulate the text style (Bold, Underline, Invert colors)

# File lib/amun/helpers/colors.rb, line 83
def use(curses_window, name, type = NORMAL)
  index = COLORS.key?(name) ? COLORS[name] : 0
  curses_window.attron(Curses.color_pair(index) | type)
end