Class: Sai::ANSI::SequencedString

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/sai/ansi/sequenced_string.rb

Overview

A representation of a ANSI encoded string and its individual segments

Author:

Since:

  • 0.3.0

Defined Under Namespace

Classes: Segment

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ SequencedString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new instance of SequencedString

Parameters:

  • string (String) —

    the sequenced string to Segment

Author:

Since:

  • 0.3.0



66
67
68
69
70
# File 'lib/sai/ansi/sequenced_string.rb', line 66

def initialize(string)
  @segments = ANSI::SequenceProcessor.process(string).map do |segment_options|
    Segment.new(**segment_options) # steep:ignore InsufficientKeywordArguments
  end
end

Instance Method Details

#+(other) ⇒ SequencedString

Combine a sequenced string with another object

Examples:

sequenced_string = SequencedString.new("\e[31mred\e[0m")
sequenced_string + " is a color" #=> "\e[31mred\e[0m is a color"

Parameters:

  • other (Object) —

    the object to combine with

Returns:

Author:

Since:

  • 0.3.0



126
127
128
129
# File 'lib/sai/ansi/sequenced_string.rb', line 126

def +(other)
  string = to_s + other.to_s
  self.class.new(string)
end

#==(other) ⇒ Boolean

Compare the SequencedString to another object

Examples:

string = "\e[31mred\e[0m"
SequencedString.new(string) == string #=> true

Parameters:

  • other (Object) —

    the object to compare to

Returns:

  • (Boolean) —

    true if the SequencedString is equal to the other object, false otherwise

Author:

Since:

  • 0.3.0



106
107
108
109
# File 'lib/sai/ansi/sequenced_string.rb', line 106

def ==(other)
  (other.is_a?(self.class) && to_s == other.to_s) ||
    (other.is_a?(String) && to_s == self.class.new(other).to_s)
end

#[](index) ⇒ Segment?

Fetch a segment by index

Examples:

string = SequencedString.new("\e[31mred\e[0m")
string[0] #=> #<SequencedString::Segment:0x00007f9b3b8b3e10>

Parameters:

  • index (Integer) —

    the index of the segment to fetch

Returns:

  • (Segment, nil) —

    the segment at the index

Author:

Since:

  • 0.3.0



87
88
89
# File 'lib/sai/ansi/sequenced_string.rb', line 87

def [](index)
  @segments[index]
end

#each ⇒ Enumerator

Iterate over each segment

Returns:

  • (Enumerator) —

    the Enumerator

Author:

Since:

  • 0.3.0



47
# File 'lib/sai/ansi/sequenced_string.rb', line 47

def_delegators :@segments, :each, :empty?, :map, :size

#map ⇒ Array

Map over segments

Returns:

  • (Array) —

    the segments to map over

Author:

Since:

  • 0.3.0



47
# File 'lib/sai/ansi/sequenced_string.rb', line 47

def_delegators :@segments, :each, :empty?, :map, :size

#size ⇒ Integer

Number of segments

Returns:

  • (Integer) —

    the number of segments

Author:

Since:

  • 0.3.0



47
# File 'lib/sai/ansi/sequenced_string.rb', line 47

def_delegators :@segments, :each, :empty?, :map, :size

#stripped ⇒ String

Return just the raw text content with no ANSI sequences

Examples:

string = SequencedString.new("Normal \e[31mred\e[0m")
string.stripped #=> "Normal red"

Returns:

  • (String) —

    the concatenation of all segment text without color or style

Author:

Since:

  • 0.3.0



143
144
145
# File 'lib/sai/ansi/sequenced_string.rb', line 143

def stripped
  map(&:text).join
end

#to_s ⇒ String Also known as: to_str

Return the fully reconstructed string with all ANSI sequences (foreground, background, style)

Examples:

string = SequencedString.new("\e[31mred\e[0m")
string.to_s #=> "\e[31mred\e[0m"

Returns:

  • (String)

Author:

Since:

  • 0.3.0



159
160
161
# File 'lib/sai/ansi/sequenced_string.rb', line 159

def to_s
  build_string
end

#without_background ⇒ SequencedString

Return a string with everything except background color sequences removed

Examples:

Remove all background colors

string = SequencedString.new("\e[41mBack\e[0m \e[1mBold\e[0m")
string.without_background #=> "\e[1mBold\e[0m"

Returns:

Author:

Since:

  • 0.3.0



177
178
179
# File 'lib/sai/ansi/sequenced_string.rb', line 177

def without_background
  self.class.new(build_string(skip_background: true))
end

#without_color ⇒ SequencedString

Return a string containing style sequences but no foreground or background colors

Examples:

Remove all colors

string = SequencedString.new("\e[31mred\e[0m \e[1mbold\e[0m")
string.without_color #=> "\e[1mbold\e[0m"

Returns:

Author:

Since:

  • 0.3.0



194
195
196
# File 'lib/sai/ansi/sequenced_string.rb', line 194

def without_color
  self.class.new(build_string(skip_background: true, skip_foreground: true))
end

#without_foreground ⇒ SequencedString

Return a string with everything except foreground color sequences removed

Examples:

Remove all foreground colors

string = SequencedString.new("\e[41mBack\e[0m \e[1mBold\e[0m")
string.without_foreground #=> "\e[41mBack\e[0m \e[1mBold\e[0m"

Returns:

Author:

Since:

  • 0.3.0



211
212
213
# File 'lib/sai/ansi/sequenced_string.rb', line 211

def without_foreground
  self.class.new(build_string(skip_foreground: true))
end

#without_style(*styles) ⇒ SequencedString

Return a string with specified styles removed

Examples:

Remove all styles

string = SequencedString.new("\e[31mred\e[0m \e[1mbold\e[0m")
string.without_style #=> "\e[31mred\e[0m"

Remove specific style

string = SequencedString.new("\e[1;4mBold and Underlined\e[0m")
string.without_style(:bold) #=> "\e[4mUnderlined\e[0m"

Parameters:

  • styles (Array<Symbol>) —

    specific styles to remove (default: all)

Returns:

Author:

Since:

  • 0.3.0



233
234
235
236
# File 'lib/sai/ansi/sequenced_string.rb', line 233

def without_style(*styles)
  skipped_styles = styles.empty? ? ANSI::STYLES.keys : styles.map(&:to_sym)
  self.class.new(build_string(skip_styles: skipped_styles))
end