Class: RubyRich::Console
- Inherits:
-
Object
- Object
- RubyRich::Console
- Defined in:
- lib/ruby_rich/console.rb
Constant Summary collapse
- ESCAPE_SEQUENCES =
{ # 方向键 '[A' => :up, '[B' => :down, '[C' => :right, '[D' => :left, # 功能键 'OP' => :f1, 'OQ' => :f2, 'OR' => :f3, 'OS' => :f4, '[15~' => :f5, '[17~' => :f6, '[18~' => :f7, '[19~' => :f8, '[20~' => :f9, '[21~' => :f10, '[23~' => :f11, '[24~' => :f12, # 添加媒体键示例 '[1~' => :home, '[4~' => :end, # 添加 macOS 功能键 '[25~' => :audio_mute, # 其他 '[5~' => :page_up, '[6~' => :page_down, '[H' => :home, '[F' => :end, '[2~' => :insert, '[3~' => :delete }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #add_line(text) ⇒ Object
- #clear ⇒ Object
- #get_key(input: $stdin) ⇒ Object
-
#initialize ⇒ Console
constructor
A new instance of Console.
- #log(message, *objects, sep: ' ', end_char: "\n") ⇒ Object
- #print(*objects, sep: ' ', end_char: "\n") ⇒ Object
- #render ⇒ Object
- #rule(title: nil, characters: '#', style: 'bold') ⇒ Object
- #set_layout(spacing: 1, align: :left) ⇒ Object
- #style(name, **attributes) ⇒ Object
- #update_line(index, text) ⇒ Object
Constructor Details
#initialize ⇒ Console
Returns a new instance of Console.
24 25 26 27 28 29 |
# File 'lib/ruby_rich/console.rb', line 24 def initialize @lines = [] @buffer = [] @layout = { spacing: 1, align: :left } @styles = {} end |
Class Method Details
.clear ⇒ Object
72 73 74 |
# File 'lib/ruby_rich/console.rb', line 72 def self.clear system('clear') end |
.raw ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/ruby_rich/console.rb', line 64 def self.raw old_state = `stty -g` system('stty raw -echo -icanon isig') rescue nil yield ensure system("stty #{old_state}") rescue nil end |
Instance Method Details
#add_line(text) ⇒ Object
110 111 112 |
# File 'lib/ruby_rich/console.rb', line 110 def add_line(text) @lines << text end |
#clear ⇒ Object
114 115 116 |
# File 'lib/ruby_rich/console.rb', line 114 def clear Kernel.print "\e[H\e[2J" end |
#get_key(input: $stdin) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ruby_rich/console.rb', line 76 def get_key(input: $stdin) input.raw(intr: true) do |io| char = io.getch # 优先处理回车键(ASCII 13 = \r,ASCII 10 = \n) if char == "\r" || char == "\n" return :enter end # 单独处理 Tab 键(ASCII 9) if char == "\t" return :tab elsif char.ord == 0x07F return :backspace elsif char == "\e" # 检测到转义序列 sequence = '' begin while (c = io.read_nonblock(1)) sequence << c end rescue IO::WaitReadable retry if IO.select([io], nil, nil, 0.01) rescue EOFError end return ESCAPE_SEQUENCES[sequence] || :escape # 处理 Ctrl 组合键(排除 Tab 和回车) elsif char.ord.between?(1, 8) || char.ord.between?(10, 26) ctrl_char = (char.ord + 64).chr.downcase return :"ctrl_#{ctrl_char}" else char end end end |
#log(message, *objects, sep: ' ', end_char: "\n") ⇒ Object
46 47 48 49 50 51 |
# File 'lib/ruby_rich/console.rb', line 46 def log(, *objects, sep: ' ', end_char: "\n") = Time.now.strftime("%Y-%m-%d %H:%M:%S") = "[#{}] LOG: #{} #{objects.map(&:to_s).join(sep)}" add_line() render end |
#print(*objects, sep: ' ', end_char: "\n") ⇒ Object
40 41 42 43 44 |
# File 'lib/ruby_rich/console.rb', line 40 def print(*objects, sep: ' ', end_char: "\n") line_text = objects.map(&:to_s).join(sep) add_line(line_text) render end |
#render ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby_rich/console.rb', line 118 def render clear @lines.each_with_index do |line, index| formatted_line = format_line(line) @buffer << formatted_line Kernel.puts formatted_line Kernel.puts "\n" * @layout[:spacing] if index < @lines.size - 1 end end |
#rule(title: nil, characters: '#', style: 'bold') ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruby_rich/console.rb', line 53 def rule(title: nil, characters: '#', style: 'bold') rule_line = characters * 80 if title formatted_title = " #{title} ".center(80, characters) add_line(formatted_title) else add_line(rule_line) end render end |
#set_layout(spacing: 1, align: :left) ⇒ Object
31 32 33 34 |
# File 'lib/ruby_rich/console.rb', line 31 def set_layout(spacing: 1, align: :left) @layout[:spacing] = spacing @layout[:align] = align end |
#style(name, **attributes) ⇒ Object
36 37 38 |
# File 'lib/ruby_rich/console.rb', line 36 def style(name, **attributes) @styles[name] = attributes end |
#update_line(index, text) ⇒ Object
128 129 130 131 132 |
# File 'lib/ruby_rich/console.rb', line 128 def update_line(index, text) return unless index.between?(0, @lines.size - 1) @lines[index] = text render end |