Class: ANSI

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/python/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.



23
24
25
# File 'lib/dsl/python/ansi.rb', line 23

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

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



21
22
23
# File 'lib/dsl/python/ansi.rb', line 21

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



21
22
23
# File 'lib/dsl/python/ansi.rb', line 21

def width
  @width
end

Class Method Details

.clear_screenObject



43
44
45
# File 'lib/dsl/python/ansi.rb', line 43

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

.move_cursor(row, col) ⇒ Object



47
48
49
# File 'lib/dsl/python/ansi.rb', line 47

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

.pressed_keyObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dsl/python/ansi.rb', line 61

def self.pressed_key
  char = begin
    STDIN.read_nonblock(3)
  rescue
    nil
  end
  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


55
56
57
58
59
# File 'lib/dsl/python/ansi.rb', line 55

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

.reset_cursorObject



51
52
53
# File 'lib/dsl/python/ansi.rb', line 51

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

.set_cooked_modeObject



84
85
86
87
# File 'lib/dsl/python/ansi.rb', line 84

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

.set_raw_modeObject



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

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

Instance Method Details

#green(text) ⇒ Object



27
28
29
# File 'lib/dsl/python/ansi.rb', line 27

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


39
40
41
# File 'lib/dsl/python/ansi.rb', line 39

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

#red(text) ⇒ Object



31
32
33
# File 'lib/dsl/python/ansi.rb', line 31

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

#yellow(text) ⇒ Object



35
36
37
# File 'lib/dsl/python/ansi.rb', line 35

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