Class: Sai::ANSI::SequencedString
- Inherits:
-
Object
- Object
- Sai::ANSI::SequencedString
- 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
Defined Under Namespace
Classes: Segment
Instance Method Summary collapse
-
#+(other) ⇒ SequencedString
Combine a sequenced string with another object.
-
#==(other) ⇒ Boolean
Compare the SequencedString to another object.
-
#[](index) ⇒ Segment?
Fetch a segment by index.
-
#each ⇒ Enumerator
Iterate over each segment.
-
#initialize(string) ⇒ SequencedString
constructor
private
Initialize a new instance of SequencedString.
-
#map ⇒ Array
Map over segments.
-
#size ⇒ Integer
Number of segments.
-
#stripped ⇒ String
Return just the raw text content with no ANSI sequences.
-
#to_s ⇒ String
(also: #to_str)
Return the fully reconstructed string with all ANSI sequences (foreground, background, style).
-
#without_background ⇒ SequencedString
Return a string with everything except background color sequences removed.
-
#without_color ⇒ SequencedString
Return a string containing style sequences but no foreground or background colors.
-
#without_foreground ⇒ SequencedString
Return a string with everything except foreground color sequences removed.
-
#without_style(*styles) ⇒ SequencedString
Return a string with specified styles removed.
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
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.new(**) # steep:ignore InsufficientKeywordArguments end end |
Instance Method Details
#+(other) ⇒ SequencedString
Combine a sequenced string with another object
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
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
87 88 89 |
# File 'lib/sai/ansi/sequenced_string.rb', line 87 def [](index) @segments[index] end |
#each ⇒ Enumerator
Iterate over each segment
47 |
# File 'lib/sai/ansi/sequenced_string.rb', line 47 def_delegators :@segments, :each, :empty?, :map, :size |
#map ⇒ Array
Map over segments
47 |
# File 'lib/sai/ansi/sequenced_string.rb', line 47 def_delegators :@segments, :each, :empty?, :map, :size |
#size ⇒ Integer
Number of segments
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
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)
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
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
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
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
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 |