Class: Hackmac::Graph::Display::Cell

Inherits:
Struct
  • Object
show all
Defined in:
lib/hackmac/graph/display.rb

Overview

A cell representation for terminal display with character, color, background color, and styling attributes

The Cell class encapsulates the properties of a single character cell in a terminal display, including its visual characteristics such as the displayed character, text color, background color, and styling attributes. It provides methods to compare cells and convert them to their string representation with ANSI escape codes for terminal rendering.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#charObject

Returns the value of attribute char

Returns:

  • (Object)

    the current value of char



27
28
29
# File 'lib/hackmac/graph/display.rb', line 27

def char
  @char
end

#colorObject

Returns the value of attribute color

Returns:

  • (Object)

    the current value of color



27
28
29
# File 'lib/hackmac/graph/display.rb', line 27

def color
  @color
end

#on_colorObject

Returns the value of attribute on_color

Returns:

  • (Object)

    the current value of on_color



27
28
29
# File 'lib/hackmac/graph/display.rb', line 27

def on_color
  @on_color
end

#stylesObject

Returns the value of attribute styles

Returns:

  • (Object)

    the current value of styles



27
28
29
# File 'lib/hackmac/graph/display.rb', line 27

def styles
  @styles
end

Instance Method Details

#==(other) ⇒ Boolean

The == method compares two Cell objects for equality by their internal array representation

This method checks if the current Cell instance is equal to another Cell instance by comparing their underlying array representations returned by to_a

Parameters:

Returns:

  • (Boolean)

    true if both cells have identical char, color, on_color, and styles values, false otherwise



40
41
42
# File 'lib/hackmac/graph/display.rb', line 40

def ==(other)
  to_a == other.to_a
end

#to_sString

The to_s method converts a Cell object into its string representation with ANSI styling

This method constructs a formatted string that includes the cell’s character along with its color, background color, and text style attributes using ANSI escape sequences. The resulting string is suitable for display in terminal environments that support ANSI colors.

Returns:

  • (String)

    a formatted string containing the cell’s visual representation with appropriate ANSI styling codes for terminal rendering



54
55
56
57
58
59
60
61
# File 'lib/hackmac/graph/display.rb', line 54

def to_s
  result = +''
  result << ANSI.color(color)
  result << ANSI.on_color(on_color)
  styles.each { |s| result << ANSI.send(s) }
  result << char
  result << ANSI.reset
end