Class: RubyRich::RichText

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/text.rb

Constant Summary collapse

@@theme =

默认主题

{
  error: { color: :red, bold: true },
  success: { color: :green, bold: true },
  info: { color: :cyan },
  warning: { color: :yellow, bold: true }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, style: nil) ⇒ RichText

Returns a new instance of RichText.



15
16
17
18
19
# File 'lib/ruby_rich/text.rb', line 15

def initialize(text, style: nil)
  @text = text
  @styles = []
  apply_theme(style) if style
end

Class Method Details

.set_theme(new_theme) ⇒ Object



11
12
13
# File 'lib/ruby_rich/text.rb', line 11

def self.set_theme(new_theme)
  @@theme.merge!(new_theme)
end

Instance Method Details

#renderObject



36
37
38
# File 'lib/ruby_rich/text.rb', line 36

def render
  "#{@styles.join}#{@text}#{AnsiCode.reset}"
end

#style(color: :white, font_bright: false, background: nil, background_bright: false, bold: false, italic: false, underline: false, underline_style: nil, strikethrough: false, overline: false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_rich/text.rb', line 21

def style(color: :white, 
  font_bright: false, 
  background: nil, 
  background_bright: false,
  bold: false, 
  italic: false,
  underline: false,
  underline_style: nil,
  strikethrough: false,
  overline: false
  )
  @styles << AnsiCode.font(color, font_bright, background, background_bright, bold, italic, underline, underline_style, strikethrough, overline)
  self
end