Module: ANSI
- Included in:
- Code
- Defined in:
- lib/ansi.rb,
lib/sc-ansi.rb,
lib/ansi/code.rb,
lib/ansi/color.rb,
lib/ansi/match.rb,
lib/ansi/vt100.rb,
lib/ansi/cursor.rb,
lib/ansi/display.rb
Overview
This file is here as a way to differentiate between the sc-ansi gem and any other ANSI-related gem you may be harboring. If this is not true in your case, feel free to require ‘ansi’ directly. The other reason for this file is to take some confusion out of loading the gem from a Rails project, as Rails will automatically load a file whose name matches the gem. Yay.
Defined Under Namespace
Modules: Color, Cursor, Display, VT100 Classes: Code, Match
Class Method Summary collapse
-
.alias_code(code, *names, &block) ⇒ Object
Aliases a specific code with the given names.
-
.arities ⇒ Object
The arity of the block that was used to define a particular method.
- .codes ⇒ Object
-
.define_with_extension(*names, &block) ⇒ Object
Defines the escape sequence, and then delegates to it from within String.
- .dynamically_defined_methods ⇒ Object
-
.generate_sequence(method_name) ⇒ Object
Dynamically generates an escape sequence for the specified method name.
-
.recognize(str) ⇒ Object
Attempts to find an ANSI escape sequence which matches the specified string.
-
.reset! ⇒ Object
Causes ANSI to unload all generated methods and wipe out the #codes index, and then reload its internal files.
- .test_sequence(arity, &block) ⇒ Object
Class Method Details
.alias_code(code, *names, &block) ⇒ Object
Aliases a specific code with the given names. This way you don’t need to redefine a new constant, so performance is improved a little bit. Note that the code is expected to be an actual instance of ANSI::Code, and not an arbitrary string.
125 126 127 128 |
# File 'lib/ansi.rb', line 125 def alias_code(code, *names, &block) code.add_methods! *names delegate_names_for(code, *names, &block) end |
.arities ⇒ Object
The arity of the block that was used to define a particular method.
66 67 68 |
# File 'lib/ansi.rb', line 66 def arities @arities ||= {} end |
.codes ⇒ Object
41 42 43 |
# File 'lib/ansi.rb', line 41 def codes @codes ||= [] end |
.define_with_extension(*names, &block) ⇒ Object
Defines the escape sequence, and then delegates to it from within String. This makes it possible to do things like “this is red”.red and so forth. Note that to keep a clean namespace, we’ll undef and unalias all of this at the end of the Color module.
61 62 63 64 65 66 67 |
# File 'lib/ansi/color.rb', line 61 def define_with_extension(*names, &block) #:nodoc: _define(*names, &block) names.flatten.each do |name| String.class_eval { define_method(name) { ANSI.send(name) { self } } } Symbol.class_eval { define_method(name) { self.to_s.send(name) } } end end |
.dynamically_defined_methods ⇒ Object
61 62 63 |
# File 'lib/ansi.rb', line 61 def dynamically_defined_methods @dynamically_defined_methods ||= [] end |
.generate_sequence(method_name) ⇒ Object
Dynamically generates an escape sequence for the specified method name. Arguments are replaced with “?”.
72 73 74 |
# File 'lib/ansi.rb', line 72 def generate_sequence(method_name) test_sequence(arities[method_name]) { |*args| send(method_name, *args) } end |
.recognize(str) ⇒ Object
Attempts to find an ANSI escape sequence which matches the specified string. The string is expected to use the notation “?” for each parameter instead of a real value. For instance, the sequence which would return MOVE_UP would look like: “e[?A” instead of “e[1A”
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ansi.rb', line 96 def recognize(str) match = nil String::ANSI_ESCAPE_SEQUENCE_RX =~ str if $~ && args = $~[2].split(";") codes.uniq.each do |code| _args = args.dup begin result = code.generate(*_args) rescue TypeError => err if err. =~ /\(expected Proc\)/ && !_args.empty? _args.pop retry end next end if result == str match ||= ANSI::Match.new(_args) match << code end end end return match if match raise "ANSI sequence not found: #{str.inspect}" end |
.reset! ⇒ Object
Causes ANSI to unload all generated methods and wipe out the #codes index, and then reload its internal files. The codes stored in #codes themselves are unaffected, though they will float into oblivion and be processed by the garbage collector if you haven’t captured them in some way.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ansi.rb', line 49 def reset! codes.clear dynamically_defined_methods.each { |c| undef_method(c) } dynamically_defined_methods.clear arities.clear load File.join(File.dirname(__FILE__), "ansi/cursor.rb") load File.join(File.dirname(__FILE__), "ansi/display.rb") load File.join(File.dirname(__FILE__), "ansi/color.rb") load File.join(File.dirname(__FILE__), "ansi/vt100.rb") end |
.test_sequence(arity, &block) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ansi.rb', line 76 def test_sequence(arity, &block) args = [] arity.times { args << "{?}" } if arity begin return block.call(*args) rescue TypeError => err if err. =~ /\(expected Proc\)/ args.pop args << nil retry else raise err end end end |