Class: ANSI

Inherits:
Object
  • Object
show all
Defined in:
lib/diamante/ansi.rb

Overview

Códigos ANSI de color: Color Código Uso en Ruby Rojo e[31m puts “e[31mEste texto es rojoe[0m” Verde e[32m puts “e[32mEste texto es verdee[0m” Amarillo e[33m puts “e[33mEste texto es amarilloe[0m” Reset e[0m (Obligatorio para volver al color normal)

Constant Summary collapse

COLORS =
{
  red: "\e[31m",
  green: "\e[32m",
  yellow: "\e[33m"
}
RESET =
"\e[0m"
CLEAR_SCREEN =
"\e[2J\e[H"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeANSI

Returns a new instance of ANSI.



22
23
24
# File 'lib/diamante/ansi.rb', line 22

def initialize
  @height, @width = `stty size`.split.map { _1.to_i }
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



20
21
22
# File 'lib/diamante/ansi.rb', line 20

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



20
21
22
# File 'lib/diamante/ansi.rb', line 20

def width
  @width
end

Class Method Details

.clear_screenObject



42
43
44
# File 'lib/diamante/ansi.rb', line 42

def self.clear_screen
  print "\e[2J\e[H"
end

.move_cursor(row, col) ⇒ Object



46
47
48
# File 'lib/diamante/ansi.rb', line 46

def self.move_cursor(row, col)
  print "\033[#{row};#{col}H"
end

.pressed_keyObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/diamante/ansi.rb', line 60

def self.pressed_key
  char = STDIN.read_nonblock(3) rescue nil
  return nil unless char

  case char
  when "\e[A" then :up
  when "\e[B" then :down
  when "\e[C" then :right
  when "\e[D" then :left
  when "q"    then :quit
  else char
  end
end


54
55
56
57
58
# File 'lib/diamante/ansi.rb', line 54

def self.print_text_at(row, col, text) 
  print "\033[#{row + 1};#{col}H"
  print text
  print "\e[0m"
end

.reset_cursorObject



50
51
52
# File 'lib/diamante/ansi.rb', line 50

def self.reset_cursor
  print "\e[0m"
end

.set_cooked_modeObject



79
80
81
82
# File 'lib/diamante/ansi.rb', line 79

def self.set_cooked_mode
  STDIN.cooked!
  STDIN.echo = true
end

.set_raw_modeObject



74
75
76
77
# File 'lib/diamante/ansi.rb', line 74

def self.set_raw_mode
  STDIN.echo = false
  STDIN.raw!
end

Instance Method Details

#green(text) ⇒ Object



26
27
28
# File 'lib/diamante/ansi.rb', line 26

def green(text)
  print_with_color(:green, text)
end


38
39
40
# File 'lib/diamante/ansi.rb', line 38

def print_with_color(color, text)
  "#{COLORS[color]}#{text}#{RESET}"
end

#red(text) ⇒ Object



30
31
32
# File 'lib/diamante/ansi.rb', line 30

def red(text)
  print_with_color(:red, text)
end

#yellow(text) ⇒ Object



34
35
36
# File 'lib/diamante/ansi.rb', line 34

def yellow(text)
  print_with_color(:yellow, text)
end