class Crayon::Text

Defined in:

crayon/text.cr

Constant Summary

ANSI_END = "\e[0;m"

ANSI escape codes for the end of the string

ANSI_START = "\e[%s%s%sm"

ANSI escape codes for the start of the string

Instance Method Summary

Instance Method Detail

def black(str : String | Char | Nil = nil) : self | String #

[View source]
def blackbg(str : String | Char | Nil = nil) : self | String #

[View source]
def blink(str : String | Char | Nil = nil) : self | String #

[View source]
def blue(str : String | Char | Nil = nil) : self | String #

[View source]
def bluebg(str : String | Char | Nil = nil) : self | String #

[View source]
def bold(str : String | Char | Nil = nil) : self | String #

[View source]
def cyan(str : String | Char | Nil = nil) : self | String #

[View source]
def cyanbg(str : String | Char | Nil = nil) : self | String #

[View source]
def encode(str : String | Char) : String #

Add ANSI escape codes to a string


[View source]
def green(str : String | Char | Nil = nil) : self | String #

[View source]
def greenbg(str : String | Char | Nil = nil) : self | String #

[View source]
def inverse(str : String | Char | Nil = nil) : self | String #

[View source]
def italic(str : String | Char | Nil = nil) : self | String #

[View source]
def magenta(str : String | Char | Nil = nil) : self | String #

[View source]
def magentabg(str : String | Char | Nil = nil) : self | String #

[View source]
def red(str : String | Char | Nil = nil) : self | String #

[View source]
def redbg(str : String | Char | Nil = nil) : self | String #

[View source]
def render(str : String | Char) : String #

Render a string with ANSI escape codes added


[View source]
def repeat(char : Char | String, len : Int32) : String #

Create a string by repeating the char character len times

crayon = Crayon::Text.new
puts crayon.repeat('a', 10)
=> "aaaaaaaaaa"

[View source]
def set_bg_color(color : Color, str : String | Char | Nil = nil) : self | String #

Set the current background color

If a string is passed as a second argument, it is encoded with the color applied and returned

crayon = Crayon::Text.new
puts crayon.set_bg_color(Color::Black, "string")
# => "\033[0;40mstring\033[0;m"

If no second argument is passed, the instance is returned, allowing methods to be chained

crayon = Crayon::Text.new
puts crayon.set_bg_color(Color::Black).set_color(Color::Red, "string")
# => "\033[0;40;31mstring\033[0;m"

[View source]
def set_color(color : Color, str : String | Char | Nil = nil) : self | String #

Set the current color

If a string is passed as a second argument, it is encoded with the color applied and returned

crayon = Crayon::Text.new
puts crayon.set_color(Color::Black, "string")
# => "\033[0;30mstring\033[0;m"

If no second argument is passed, the instance is returned, allowing methods to be chained

crayon = Crayon::Text.new
puts crayon.set_color(Color::Black).set_bg_color(Color::Red, "string")
# => "\033[0;41;30mstring\033[0;m"

[View source]
def set_style(style : Style, str : String | Char | Nil = nil) : self | String #

Set the current style

If a string is passed as a second argument, it is encoded with the color applied and returned

crayon = Crayon::Text.new
puts crayon.set_style(Style::Bold, "string")
# => "\033[1;mstring\033[0;m"

If no second argument is passed, the instance is returned, allowing methods to be chained

crayon = Crayon::Text.new
puts crayon.set_style(Style::Bold).set_color(Color::Red, "string")
# => "\033[0;41;30mstring\033[0;m"

[View source]
def strikethrough(str : String | Char | Nil = nil) : self | String #

[View source]
def strip(str : String | Char) : String #

Strip ANSI encoding from a string

crayon = Crayon::Text.new
str    = "\033[0;33mA styled string\033[0;m"
puts crayon.strip "str"
=> "A styled string"

[View source]
def underline(str : String | Char | Nil = nil) : self | String #

[View source]
def white(str : String | Char | Nil = nil) : self | String #

[View source]
def whitebg(str : String | Char | Nil = nil) : self | String #

[View source]
def yellow(str : String | Char | Nil = nil) : self | String #

[View source]
def yellowbg(str : String | Char | Nil = nil) : self | String #

[View source]