Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/term/code.rb,
lib/term/font.rb,
lib/term/color.rb,
lib/term/style.rb,
lib/term/qrcode.rb,
lib/term/confirm.rb

Overview

Author:

Constant Summary collapse

COLOR =

string colors.

{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  light_gray: 37,
  default_color: 39,
  dark_gray: 90,
  light_red: 91,
  light_green: 92,
  light_yellow: 93,
  light_blue: 94,
  light_magenta: 95,
  light_cyan: 96,
  white: 97
}.freeze
STYLE =

string styles. see ANSI/VT100 Control sequences.

{
  bold: 1,
  dim: 2,
  italicized: 3,
  underlined: 4,
  blink: 5,
  inverse: 7,
  hidden: 8,
  crossed_out: 9
}.freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.fontsObject

Returns the value of attribute fonts.



8
9
10
# File 'lib/term/font.rb', line 8

def fonts
  @fonts
end

Instance Method Details

#background_color(name) ⇒ String

string background color.

Examples:

background color

"mystring".background_color :black # keywords
"mystring".background_color 123 # 256 colors
"mystring".background_color '#000' # true color

Parameters:

  • name (Object)

    the background color name

Returns:

  • (String)

    the formatting string



226
227
228
229
# File 'lib/term/color.rb', line 226

def background_color(name)
  set_256_color(name, 48) || set_rbg_color(name, 48) ||
    set_keyword_color(name, '_background') || self
end

#blackString

black string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 27

#black_backgroundString

black background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 32

Note:

works in the tty and XIO.

blink string.

does not work with most of the terminal emulators

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 72

#blueString

blue string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 67

#blue_backgroundString

blue background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 72

#boldString

blod string.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 28

#code(name) ⇒ String

a code syntax highlighter.

Examples:

Code

code_str = <<-EOF
class A
  def b
    puts 'c'
  end
end
EOF
code_str.code 'ruby'

Parameters:

  • name (String)

    the code name

Returns:

  • (String)

    the format code string with color

See Also:



21
22
23
# File 'lib/term/code.rb', line 21

def code(name)
  Rouge.highlight(self, name.to_s, 'terminal256')
end

#color(name) ⇒ String

string color.

Examples:

color

"mystring".color :black # keywords
"mystring".color 123 # 256 colors
"mystring".color '#000' # true color

Parameters:

  • name (Object)

    the color name

Returns:

  • (String)

    the formatting string



213
214
215
216
# File 'lib/term/color.rb', line 213

def color(name)
  set_256_color(name) || set_rbg_color(name) ||
    set_keyword_color(name) || self
end

#crossed_outString

crossed out string.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 108

#cyanString

cyan string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 87

#cyan_backgroundString

cyan background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 92

#dark_grayString

dark gray string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 117

#dark_gray_backgroundString

dark gray background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 122

#default_colorString

default color string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 107

#default_color_backgroundString

default color background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 112

#dimString

dim string.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 39

#font(name) ⇒ String

Note:

using ‘artii` gem. typing `artii -l` to see all fonts.

set the font.

Examples:

puts "term".font('big')

Parameters:

  • name (String)

    the font name

Returns:

  • (String)

    string with the font style.



19
20
21
22
# File 'lib/term/font.rb', line 19

def font(name)
  font_builder = self.class.fonts[name.to_s] ||= Artii::Base.new(font: name)
  font_builder.output self
end

#greenString

green string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 47

#green_backgroundString

green background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 52

#hiddenString

Note:

usefull for passwords.

hidden string.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 96

#inverseString

nvert the foreground and background colors.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 85

#italicizedString

italicized string.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 50

#light_blueString

light blue string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 157

#light_blue_backgroundString

light blue background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 162

#light_cyanString

light cyan string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 177

#light_cyan_backgroundString

light cyan background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 182

#light_grayString

light gray string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 97

#light_gray_backgroundString

light gray background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 102

#light_greenString

light green string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 137

#light_green_backgroundString

light green background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 142

#light_magentaString

light magenta string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 167

#light_magenta_backgroundString

light magenta background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 172

#light_redString

light red string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 127

#light_red_backgroundString

light red background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 132

#light_yellowString

light yellow string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 147

#light_yellow_backgroundString

light yellow background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 152

#magentaString

magenta string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 77

#magenta_backgroundString

magenta background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 82

#ok?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/term/confirm.rb', line 10

def ok?
  print "#{self}? ".green
  @begin_confirm_pos = IO.hide.pos
  process_confirm_input
  IO.show
  @res
end

#qrcodeString

get qrcode.

Returns:



12
13
14
# File 'lib/term/qrcode.rb', line 12

def qrcode
  RQRCode::QRCode.new(self).as_ansi(quiet_zone_size: 0)
end

#redString

red string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 37

#red_backgroundString

red background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 42

#resetString

reset all attributes.

Returns:

  • (String)

    formatting string



11
12
13
# File 'lib/term/style.rb', line 11

def reset
  "\e[0m#{self}\e[0m"
end

reset blink string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 79

#reset_boldString

reset bold string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 33

#reset_crossed_outString

reset crossed out string.

Returns:

  • (String)

    formatting string

See Also:



118
119
120
121
122
123
124
125
# File 'lib/term/style.rb', line 118

STYLE.each do |name, value|
  define_method(name) do
    "\e[#{value}m#{self}\e[0m"
  end
  define_method("reset_#{name}") do
    "\e[#{10 + value}m#{self}\e[0m"
  end
end

#reset_dimString

reset dim string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 44

#reset_hiddenString

reset hidden string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 102

#reset_inverseString

reset inverse string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 90

#reset_italicizedString

reset italicized string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 55

#reset_underlinedString

reset underlined string.

Returns:

  • (String)

    formatting string

See Also:



# File 'lib/term/style.rb', line 66

#underlinedString

underlined string.

Returns:

  • (String)

    formatting string



# File 'lib/term/style.rb', line 61

#whiteString

white string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 187

#white_backgroundString

white background string.

Returns:

  • (String)

    the formatting string



196
197
198
199
200
201
202
203
# File 'lib/term/color.rb', line 196

COLOR.each do |name, value|
  define_method(name) do
    "\e[#{value}m#{self}\e[0m"
  end
  define_method("#{name}_background") do
    "\e[#{10 + value}m#{self}\e[0m"
  end
end

#yellowString

yellow string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 57

#yellow_backgroundString

yellow background string.

Returns:

  • (String)

    the formatting string



# File 'lib/term/color.rb', line 62