Class: RubyRich::Console

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeConsole

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

.clearObject



72
73
74
# File 'lib/ruby_rich/console.rb', line 72

def self.clear
  system('clear')
end

.rawObject



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



115
116
117
# File 'lib/ruby_rich/console.rb', line 115

def add_line(text)
  @lines << text
end

#clearObject



119
120
121
# File 'lib/ruby_rich/console.rb', line 119

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
109
110
111
112
113
# 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"
      # 检查是否有后续输入(粘贴内容会有多个字符)
      has_more = IO.select([io], nil, nil, 0)
      return has_more ? {:name => :string, :value => char} : {:name=>:enter}
    end
    # 单独处理 Tab 键(ASCII 9)
    if char == "\t"
      return {:name=>:tab}
    elsif char.ord == 0x07F
      return {:name=>: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
      if sequence.empty?
        return {:name => :escape}
      else
        return {:name => ESCAPE_SEQUENCES[sequence]} || {:name => :escape}
      end
    # 处理 Ctrl 组合键(排除 Tab 和回车)
    elsif char.ord.between?(1, 8) || char.ord.between?(10, 26)
      ctrl_char = (char.ord + 64).chr.downcase
      return {:name =>"ctrl_#{ctrl_char}".to_sym}
    else
      {:name => :string, :value => 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(message, *objects, sep: ' ', end_char: "\n")
  timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  log_message = "[#{timestamp}] LOG: #{message} #{objects.map(&:to_s).join(sep)}"
  add_line(log_message)
  render
end


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

#renderObject



123
124
125
126
127
128
129
130
131
# File 'lib/ruby_rich/console.rb', line 123

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



133
134
135
136
137
# File 'lib/ruby_rich/console.rb', line 133

def update_line(index, text)
  return unless index.between?(0, @lines.size - 1)
  @lines[index] = text
  render
end