Class: FTT::Text
- Inherits:
-
Object
- Object
- FTT::Text
- Defined in:
- lib/fancy_terminal_text/text.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#bold ⇒ Text
Bolds the text.
- #centre(length) ⇒ Object
- #colour(colour_name) ⇒ Object
-
#initialize(string) ⇒ Text
constructor
A new instance of Text.
-
#italic ⇒ Text
Italicises the text.
- #left_pad(length) ⇒ Object
- #rainbow ⇒ Object
- #right_pad(length) ⇒ Object
- #to_s ⇒ Object
-
#underline ⇒ Text
Underlines the text.
-
#visual_length ⇒ Integer
The length of the string in terms of how much space it actually occupies on screen, without things like control characters.
Constructor Details
#initialize(string) ⇒ Text
Returns a new instance of Text.
10 11 12 |
# File 'lib/fancy_terminal_text/text.rb', line 10 def initialize(string) @string = string end |
Class Method Details
.from_parts(*segments) ⇒ Object
5 6 7 |
# File 'lib/fancy_terminal_text/text.rb', line 5 def self.from_parts(*segments) new(segments.collect(&:to_s).join) end |
Instance Method Details
#bold ⇒ Text
Bolds the text.
30 31 32 33 |
# File 'lib/fancy_terminal_text/text.rb', line 30 def bold clean = @string.gsub("\033[1m", "").gsub("\033[0m", "") self.class.new("\033[1m#{clean}\033[0m") end |
#centre(length) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fancy_terminal_text/text.rb', line 84 def centre(length) difference = [length - visual_length, 0].max return self if difference.zero? left = difference / 2 right = difference - left new_string = (' ' * left) << @string << (' ' * right) self.class.new(new_string) end |
#colour(colour_name) ⇒ Object
96 97 98 99 100 |
# File 'lib/fancy_terminal_text/text.rb', line 96 def colour(colour_name) raise ArgumentError.new, "Colour name must be one of #{String.colors}" unless String.colors.include?(colour_name) self.class.new(@string.colorize(colour_name)) end |
#italic ⇒ Text
Italicises the text.
38 39 40 41 |
# File 'lib/fancy_terminal_text/text.rb', line 38 def italic clean = @string.gsub("\e[3m", "").gsub("\e[0m", "") self.class.new("\e[3m#{clean}\e[0m") end |
#left_pad(length) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/fancy_terminal_text/text.rb', line 68 def left_pad(length) if @string.length > visual_length length = @string.length + (length - visual_length) end self.class.new(@string.rjust(length)) end |
#rainbow ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fancy_terminal_text/text.rb', line 51 def rainbow new_string = "" FTT::Colours::RAINBOW.cycle.each_with_index do |colour, index| character = @string[index] break if character.nil? if character =~ /[^[:print:]]/ new_string << character else new_string << "\e[#{colour}m#{character}\e[0m" end end FTT::Text.new(new_string) end |
#right_pad(length) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/fancy_terminal_text/text.rb', line 76 def right_pad(length) if @string.length > visual_length length = @string.length + (length - visual_length) end self.class.new(@string.ljust(length)) end |
#to_s ⇒ Object
14 15 16 |
# File 'lib/fancy_terminal_text/text.rb', line 14 def to_s @string end |
#underline ⇒ Text
Underlines the text.
46 47 48 49 |
# File 'lib/fancy_terminal_text/text.rb', line 46 def underline clean = @string.gsub("\e[4m", "").gsub("\e[0m", "") self.class.new("\e[4m#{clean}\e[0m") end |
#visual_length ⇒ Integer
The length of the string in terms of how much space it actually occupies on screen, without things like control characters.
22 23 24 25 |
# File 'lib/fancy_terminal_text/text.rb', line 22 def visual_length # Regex shamelessly taken from https://stackoverflow.com/a/56206076/16164934 @string.gsub(/\e\[[^\x40-\x7E]*[\x40-\x7E]/, "").length end |