Module: Wirb

Defined in:
lib/wirb.rb,
lib/wirb/version.rb,
lib/wirb/inspector.rb,
lib/wirb/tokenizer.rb,
lib/wirb/schema_builder.rb

Defined Under Namespace

Modules: SchemaBuilder, Tokenizer

Constant Summary collapse

VERSION =
'2.1.2'.freeze
INSPECTOR =
proc{ |value|
  if defined?(value.inspect)
    Wirb.colorize_result_with_timeout(value.inspect)
  else
    "(Object doesn't support #inspect)"
  end
}

Class Method Summary collapse

Class Method Details

.colorize_result(string, _deprecated = nil) ⇒ Object


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

def colorize_result(string, _deprecated = nil)
  check = ''
  colorful = ''
  begin
    Tokenizer.run(string).each{ |kind, token|
      check << token
      colorful << Paint[token, *Array( schema[kind] )]
    }
  rescue
    p $!, $!.backtrace[0] if $VERBOSE
  end

  # always display the correct inspect string!
  check == string ? colorful : string
end

.colorize_result_with_timeout(string) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wirb.rb', line 78

def colorize_result_with_timeout(string)
  if timeout.to_i == 0
    colorize_result(string)
  else
    Timeout.timeout(timeout) do
      colorize_result(string)
    end
  end
rescue Timeout::Error
  string
end

.load_schema(name) ⇒ Object


31
32
33
# File 'lib/wirb.rb', line 31

def load_schema(name)
  @schema = SchemaBuilder.load_schema_from_yaml(name)
end

.original_inspect_modeObject


35
36
37
# File 'lib/wirb.rb', line 35

def original_inspect_mode
  @original_inspect_mode
end

.running?Boolean

Returns:

  • (Boolean)

14
15
16
17
# File 'lib/wirb.rb', line 14

def running?
  defined?(IRB) && IRB.CurrentContext &&
  IRB.CurrentContext.inspect_mode == :wirb
end

.schemaObject


23
24
25
# File 'lib/wirb.rb', line 23

def schema
  @schema ||= SchemaBuilder.load_schema_from_yaml
end

.schema=(val) ⇒ Object


27
28
29
# File 'lib/wirb.rb', line 27

def schema=(val)
  @schema = val
end

.startObject Also known as: activate, enable


39
40
41
42
43
44
45
46
47
48
# File 'lib/wirb.rb', line 39

def start
  require_relative 'wirb/irb'
  @original_inspect_mode =
    IRB.conf[:INSPECT_MODE] ||
    IRB.CurrentContext && IRB.CurrentContext.inspect_mode ||
    true
  IRB.conf[:INSPECT_MODE] = :wirb
  IRB.CurrentContext.inspect_mode = :wirb if IRB.CurrentContext
  true
end

.stopObject Also known as: deactivate, disable


52
53
54
55
56
57
58
# File 'lib/wirb.rb', line 52

def stop
  if running?
    IRB.conf[:INSPECT_MODE] = @original_inspect_mode
    IRB.CurrentContext.inspect_mode = @original_inspect_mode if IRB.CurrentContext
    true
  end
end

.timeoutObject


19
20
21
# File 'lib/wirb.rb', line 19

def timeout
  @timeout ||= 3
end