class String
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
Constants
- COLOR
string colors. @see www.termsys.demon.co.uk/vtansi.htm ANSI/VT100 Control sequences.
- STYLE
string styles. see ANSI/VT100 Control sequences.
Attributes
Public Instance Methods
string background color.
@example background color
"mystring".background_color :black # keywords "mystring".background_color 123 # 256 colors "mystring".background_color '#000' # true color
@param name [Object] the background color name @return [String] the formatting string
# 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
a code syntax highlighter.
@example Code
code_str = <<-EOF class A def b puts 'c' end end EOF code_str.code 'ruby'
@see github.com/jneen/rouge rouge @param name [String] the code name @return [String] the format code string with color
# File lib/term/code.rb, line 21 def code(name) Rouge.highlight(self, name.to_s, 'terminal256') end
string color.
@example color
"mystring".color :black # keywords "mystring".color 123 # 256 colors "mystring".color '#000' # true color
@param name [Object] the color name @return [String] the formatting string
# File lib/term/color.rb, line 213 def color(name) set_256_color(name) || set_rbg_color(name) || set_keyword_color(name) || self end
set the font.
@note using `artii` gem.
typing `artii -l` to see all fonts.
@example
puts "term".font('big')
@param name [String] the font name @return [String] string with the font style.
# 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
# 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
get qrcode.
@return [String] qrcode
# File lib/term/qrcode.rb, line 12 def qrcode RQRCode::QRCode.new(self).as_ansi(quiet_zone_size: 0) end
reset all attributes.
@return [String] formatting string
# File lib/term/style.rb, line 11 def reset "\e[0m#{self}\e[0m" end
Private Instance Methods
print confirm.
# File lib/term/confirm.rb, line 21 def print_confirm(confirm, run) IO.to @begin_confirm_pos if @res = confirm print '✔'.red print ' ✖' if run else print '✔ ' if run print '✖'.red end @confirm_loop = run end
process confirm input confirm event.
# File lib/term/confirm.rb, line 34 def process_confirm_input print_confirm(true, true) @confirm_loop = true while @confirm_loop input = IO.input process_confirm_mouse_click input process_confirm_keyboard_input input[:key] end end
process confirm keyboard confirm event.
# File lib/term/confirm.rb, line 56 def process_confirm_keyboard_input(key) case key when :enter then print_confirm(@res, false) when :Y, :y then print_confirm(true, false) when :N, :n then print_confirm(false, false) when :tab, :down, :left, :space, :up, :right then print_confirm(!@res, true) end end
process confirm mouse confirm event.
# File lib/term/confirm.rb, line 45 def process_confirm_mouse_click(input) if (input[:type] == :left_pressed) && (input[:pos][:y] == @begin_confirm_pos[:y]) if input[:pos][:x] == @begin_confirm_pos[:x] print_confirm(true, false) elsif input[:pos][:x] == @begin_confirm_pos[:x] + 2 print_confirm(false, false) end end end
set 256 color.
# File lib/term/color.rb, line 234 def set_256_color(name, num = 38) color_index = name.to_i "\e[#{num};5;#{color_index}m#{self}\e[0m" if color_index.between? 1, 256 end
set keyword color.
# File lib/term/color.rb, line 248 def set_keyword_color(name, suffix = '') color_name = name.to_s.to_sym send "#{color_name}#{suffix}" if COLOR.key? color_name end
set rgb color.
# File lib/term/color.rb, line 240 def set_rbg_color(name, num = 38) color_name = name.to_s if /^#(?<r>\h{1,2})(?<g>\h{1,2})(?<b>\h{1,2})$/ =~ color_name "\e[#{num};2;#{r.to_i(16)};#{g.to_i(16)};#{b.to_i(16)}m#{self}\e[0m" end end