Class: RubyRich::Status

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

Defined Under Namespace

Classes: Monitor, StatusBoard

Constant Summary collapse

INDICATORS =

状态指示器类型

{
  # 简单状态
  success: { symbol: '✅', color: "\e[92m", text: 'Success' },
  error: { symbol: '❌', color: "\e[91m", text: 'Error' },
  warning: { symbol: '⚠️', color: "\e[93m", text: 'Warning' },
  info: { symbol: 'ℹ️', color: "\e[94m", text: 'Info' },
  
  # 进度状态
  pending: { symbol: '⏳', color: "\e[93m", text: 'Pending' },
  running: { symbol: '🏃', color: "\e[94m", text: 'Running' },
  completed: { symbol: '✅', color: "\e[92m", text: 'Completed' },
  failed: { symbol: '💥', color: "\e[91m", text: 'Failed' },
  
  # 系统状态
  online: { symbol: '🟢', color: "\e[92m", text: 'Online' },
  offline: { symbol: '🔴', color: "\e[91m", text: 'Offline' },
  maintenance: { symbol: '🔧', color: "\e[93m", text: 'Maintenance' },
  
  # 安全状态
  secure: { symbol: '🔒', color: "\e[92m", text: 'Secure' },
  insecure: { symbol: '🔓', color: "\e[91m", text: 'Insecure' },
  
  # 等级状态
  low: { symbol: '🔵', color: "\e[94m", text: 'Low' },
  medium: { symbol: '🟡', color: "\e[93m", text: 'Medium' },
  high: { symbol: '🔴', color: "\e[91m", text: 'High' },
  critical: { symbol: '💀', color: "\e[95m", text: 'Critical' }
}.freeze
SPINNER_FRAMES =

加载动画帧

{
  dots: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'],
  line: ['|', '/', '-', '\\'],
  arrow: ['←', '↖', '↑', '↗', '→', '↘', '↓', '↙'],
  bounce: ['⠁', '⠂', '⠄', '⠂'],
  pulse: ['●', '◐', '◑', '◒', '◓', '◔', '◕', '○'],
  clock: ['🕐', '🕑', '🕒', '🕓', '🕔', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚', '🕛']
}.freeze

Class Method Summary collapse

Class Method Details

.indicator(type, text: nil, show_text: true, colorize: true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_rich/status.rb', line 43

def self.indicator(type, text: nil, show_text: true, colorize: true)
  config = INDICATORS[type.to_sym]
  return "Unknown status: #{type}" unless config
  
  symbol = config[:symbol]
  color = colorize ? config[:color] : ""
  reset = colorize ? "\e[0m" : ""
  status_text = text || config[:text]
  
  if show_text
    "#{color}#{symbol} #{status_text}#{reset}"
  else
    "#{color}#{symbol}#{reset}"
  end
end

.progress_bar(current, total, width: 30, style: :filled) ⇒ Object

静态进度条



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
# File 'lib/ruby_rich/status.rb', line 83

def self.progress_bar(current, total, width: 30, style: :filled)
  percentage = (current.to_f / total * 100).round(1)
  filled_width = (current.to_f / total * width).round
  
  case style
  when :filled
    filled = '█' * filled_width
    empty = '░' * (width - filled_width)
    bar = "#{filled}#{empty}"
  when :blocks
    filled = '■' * filled_width
    empty = '□' * (width - filled_width)
    bar = "#{filled}#{empty}"
  when :dots
    filled = '●' * filled_width
    empty = '○' * (width - filled_width)
    bar = "#{filled}#{empty}"
  else
    filled = '=' * filled_width
    empty = '-' * (width - filled_width)
    bar = "#{filled}#{empty}"
  end
  
  "\e[92m[#{bar}]\e[0m #{percentage}% (#{current}/#{total})"
end

.spinner(type: :dots, text: 'Loading...', delay: 0.1) ⇒ Object



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

def self.spinner(type: :dots, text: 'Loading...', delay: 0.1)
  frames = SPINNER_FRAMES[type.to_sym] || SPINNER_FRAMES[:dots]
  
  Thread.new do
    frame_index = 0
    loop do
      print "\r\e[K\e[94m#{frames[frame_index]}\e[0m #{text}"
      $stdout.flush
      sleep delay
      frame_index = (frame_index + 1) % frames.length
    end
  end
end

.stop_spinner(final_message: nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/ruby_rich/status.rb', line 73

def self.stop_spinner(final_message: nil)
  if final_message
    print "\r\e[K#{final_message}\n"
  else
    print "\r\e[K"
  end
  $stdout.flush
end