Class: Repofetch::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/repofetch/theme.rb

Overview

Provides a theme for styling output.

Constant Summary collapse

DEFAULT_STYLES =
{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
  on_black: 40,
  on_red: 41,
  on_green: 42,
  on_yellow: 43,
  on_blue: 44,
  on_magenta: 45,
  on_cyan: 46,
  on_white: 47,
  bold: 1,
  underline: 4,
  reset: 0,
  default: 0
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(styles = {}) ⇒ Theme

Initializes a new theme.

Parameters:

  • styles (Hash) (defaults to: {})

    A Hash of styles to use. Is merged with the default styles.



34
35
36
# File 'lib/repofetch/theme.rb', line 34

def initialize(styles = {})
  @styles = DEFAULT_STYLES.merge(styles)
end

Instance Attribute Details

#stylesObject (readonly)

Returns the value of attribute styles.



29
30
31
# File 'lib/repofetch/theme.rb', line 29

def styles
  @styles
end

Instance Method Details

#format(name, text) ⇒ Object

Formats a string with ANSI escape sequences.



44
45
46
# File 'lib/repofetch/theme.rb', line 44

def format(name, text)
  "#{style(name)}#{text}#{style(:reset)}"
end

#style(name) ⇒ Object

Gets the ANSI escape sequence for a style.



39
40
41
# File 'lib/repofetch/theme.rb', line 39

def style(name)
  "\e[#{styles[name]}m"
end

#to_hObject

Returns a Hash with ANSI escape sequences for each style.



49
50
51
# File 'lib/repofetch/theme.rb', line 49

def to_h
  styles.transform_values { |value| "\e[#{value}m" }
end