Module: REPLUtils

Defined in:
opal/stdlib/opal-replutils.rb

Defined Under Namespace

Classes: ColorPrinter

Class Method Summary collapse

Class Method Details

.eval_and_print(func, mode, colorize, binding = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'opal/stdlib/opal-replutils.rb', line 46

def eval_and_print(func, mode, colorize, binding = nil)
  printer = if colorize
              ->(i) do
                ColorPrinter.default(i)
              rescue => e
                ColorPrinter.colorize(Opal.inspect(i))
              end
            else
              ->(i) do
                out = []
                PP.pp(i, out)
                out.join
              rescue
                Opal.inspect(i)
              end
            end

  %x{
    var $_result = binding === nil ? eval(func) : binding.$js_eval(func);

    if (mode == 'silent') return nil;

    if ($_result === null) {
      return "=> null";
    }
    else if (typeof $_result === 'undefined') {
      return "=> undefined";
    }
    else if (typeof $_result.$$class === 'undefined') {
      try {
        var json = JSON.stringify($_result, null, 2);
        if (!colorize) json = #{ColorPrinter.colorize(`json`)}
        return "=> " + $_result.toString() + " => " + json;
      }
      catch(e) {
        return "=> " + $_result.toString();
      }
    }
    else {
      if (mode == 'ls') {
        return #{ls(`$_result`, colorize)};
      }
      else {
        var pretty = #{printer.call(`$_result`)};
        // Is it multiline? If yes, add a linebreak
        if (pretty.match(/\n.*?\n/)) pretty = "\n" + pretty;
        return "=> " + pretty;
      }
    }
  }
rescue Exception => e # rubocop:disable Lint/RescueException
  e.full_message(highlight: true)
end

.js_replObject



100
101
102
103
104
105
106
107
108
# File 'opal/stdlib/opal-replutils.rb', line 100

def js_repl
  while (line = gets)
    input = JSON.parse(line)

    out = eval_and_print(input[:code], input[:mode], input[:colors])
    puts out if out
    puts '<<<ready>>>'
  end
end

.ls(object, colorize) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'opal/stdlib/opal-replutils.rb', line 8

def ls(object, colorize)
  methods = imethods = object.methods
  ancestors = object.class.ancestors
  constants = []
  ivs = object.instance_variables
  cvs = []

  if [Class, Module].include? object.class
    imethods = object.instance_methods
    ancestors = object.ancestors
    constants = object.constants
    cvs = object.class_variables
  end

  if colorize
    blue      = ->(i) { "\e[1;34m#{i}\e[0m" }
    dark_blue = ->(i) { "\e[34m#{i}\e[0m" }
  else
    blue = dark_blue = ->(i) { i }
  end

  out = ''
  out = "#{blue['class variables']}: #{cvs.map { |i| dark_blue[i] }.sort.join('  ')}\n" + out unless cvs.empty?
  out = "#{blue['instance variables']}: #{ivs.map { |i| dark_blue[i] }.sort.join('  ')}\n" + out unless ivs.empty?
  ancestors.each do |a|
    im = a.instance_methods(false)
    meths = (im & imethods)
    methods -= meths
    imethods -= meths
    next if meths.empty? || [Object, BasicObject, Kernel, PP::ObjectMixin].include?(a)
    out = "#{blue["#{a.name}#methods"]}: #{meths.sort.join('  ')}\n" + out
  end
  methods &= object.methods(false)
  out = "#{blue['self.methods']}: #{methods.sort.join('  ')}\n" + out unless methods.empty?
  out = "#{blue['constants']}: #{constants.map { |i| dark_blue[i] }.sort.join('  ')}\n" + out unless constants.empty?
  out
end