Class: RubyRich::RichText

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

Constant Summary collapse

MARKUP_PATTERNS =

Rich markup 标记映射

{
  # Basic colors
  /\[red\](.*?)\[\/red\]/m => proc { |text| "\e[31m#{text}\e[0m" },
  /\[green\](.*?)\[\/green\]/m => proc { |text| "\e[32m#{text}\e[0m" },
  /\[yellow\](.*?)\[\/yellow\]/m => proc { |text| "\e[33m#{text}\e[0m" },
  /\[blue\](.*?)\[\/blue\]/m => proc { |text| "\e[34m#{text}\e[0m" },
  /\[magenta\](.*?)\[\/magenta\]/m => proc { |text| "\e[35m#{text}\e[0m" },
  /\[cyan\](.*?)\[\/cyan\]/m => proc { |text| "\e[36m#{text}\e[0m" },
  /\[white\](.*?)\[\/white\]/m => proc { |text| "\e[37m#{text}\e[0m" },
  /\[black\](.*?)\[\/black\]/m => proc { |text| "\e[30m#{text}\e[0m" },
  
  # Bright colors
  /\[bright_red\](.*?)\[\/bright_red\]/m => proc { |text| "\e[91m#{text}\e[0m" },
  /\[bright_green\](.*?)\[\/bright_green\]/m => proc { |text| "\e[92m#{text}\e[0m" },
  /\[bright_yellow\](.*?)\[\/bright_yellow\]/m => proc { |text| "\e[93m#{text}\e[0m" },
  /\[bright_blue\](.*?)\[\/bright_blue\]/m => proc { |text| "\e[94m#{text}\e[0m" },
  /\[bright_magenta\](.*?)\[\/bright_magenta\]/m => proc { |text| "\e[95m#{text}\e[0m" },
  /\[bright_cyan\](.*?)\[\/bright_cyan\]/m => proc { |text| "\e[96m#{text}\e[0m" },
  /\[bright_white\](.*?)\[\/bright_white\]/m => proc { |text| "\e[97m#{text}\e[0m" },
  
  # Text styles
  /\[bold\](.*?)\[\/bold\]/m => proc { |text| "\e[1m#{text}\e[22m" },
  /\[dim\](.*?)\[\/dim\]/m => proc { |text| "\e[2m#{text}\e[22m" },
  /\[italic\](.*?)\[\/italic\]/m => proc { |text| "\e[3m#{text}\e[23m" },
  /\[underline\](.*?)\[\/underline\]/m => proc { |text| "\e[4m#{text}\e[24m" },
  /\[blink\](.*?)\[\/blink\]/m => proc { |text| "\e[5m#{text}\e[25m" },
  /\[reverse\](.*?)\[\/reverse\]/m => proc { |text| "\e[7m#{text}\e[27m" },
  /\[strikethrough\](.*?)\[\/strikethrough\]/m => proc { |text| "\e[9m#{text}\e[29m" },
  
  # Combined styles
  /\[bold\s+(\w+)\](.*?)\[\/bold\s+\1\]/m => proc do |text, color_match|
    color_code = color_to_ansi(color_match)
    "\e[1m#{color_code}#{text}\e[0m"
  end
}.freeze
@@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.



52
53
54
55
56
# File 'lib/ruby_rich/text.rb', line 52

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

Class Method Details

.markup(text) ⇒ Object

处理 Rich markup 标记语言



79
80
81
# File 'lib/ruby_rich/text.rb', line 79

def self.markup(text)
  new(text).render_markup
end

.set_theme(new_theme) ⇒ Object



48
49
50
# File 'lib/ruby_rich/text.rb', line 48

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

Instance Method Details

#renderObject



73
74
75
76
# File 'lib/ruby_rich/text.rb', line 73

def render
  processed_text = process_markup(@text)
  "#{@styles.join}#{processed_text}#{AnsiCode.reset}"
end

#render_markupObject



83
84
85
# File 'lib/ruby_rich/text.rb', line 83

def render_markup
  process_markup(@text)
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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_rich/text.rb', line 58

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