Class: BrowserIrb::MainController

Inherits:
Volt::ModelController
  • Object
show all
Defined in:
app/browser_irb/controllers/main_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MainController

Returns a new instance of MainController.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/browser_irb/controllers/main_controller.rb', line 11

def initialize(*args)
  super

  @indented = false
  # Setup ESC keybinding
  `
  $(document).keyup(function(e) {
    if (e.keyCode == 27) {
      #{toggle_console}
    }
  });

  self.main_node = $('<div class="terminal-area">').appendTo('body');
  #{@term} = self.main_node.jqconsole(false, 'volt> ', '...');
  `

  restore_history

  prompt

end

Instance Method Details

#command(command, callback) ⇒ Object



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
# File 'app/browser_irb/controllers/main_controller.rb', line 61

def command(command, callback)
  if command.present?
    CommandTask.run(command).then do |code|
      if code == '...continue...'
        indent = @indented ? 0 : 2
        @indented = true
        `callback(indent);`
      else
        @indent = false
        `callback(false);`
        begin
          # Run the code returned from the server
          result = `eval(code)`
          `self.term.Write('=> ' + #{result.inspect} + "\n", 'line');`
        rescue => e
          `self.term.Write(#{e.inspect} + "\n", 'error');`
        end
      end

      stash_history
      prompt
    end.fail do |err|
      @indent = false
      `self.term.Write(err, 'error')`

      `callback(false);`
      stash_history
      prompt
    end
  else
    prompt
  end
end

#promptObject



50
51
52
53
54
55
56
57
58
59
# File 'app/browser_irb/controllers/main_controller.rb', line 50

def prompt
  `
  self.term.Prompt(true, function(input) {
    //self.$command(input);
  }, function (input, cb) {
    self.$command(input, cb);
    return false;
  }, true);
  `
end

#restore_historyObject



104
105
106
107
108
109
110
111
112
113
114
# File 'app/browser_irb/controllers/main_controller.rb', line 104

def restore_history
  `
  var data = sessionStorage.getItem('irbhistory');

  if (data) {
    data = JSON.parse(data);

    self.term.SetHistory(data);
  }
  `
end

#stash_historyObject



95
96
97
98
99
100
101
102
# File 'app/browser_irb/controllers/main_controller.rb', line 95

def stash_history
  `
  var history = self.term.GetHistory();

  history = history.slice(0, 50);
  sessionStorage.setItem('irbhistory', JSON.stringify(history));
  `
end

#toggle_consoleObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/browser_irb/controllers/main_controller.rb', line 33

def toggle_console

  `if ($('body').is('.terminal-open')) {`
    `$('body').removeClass('terminal-open');`
    `$('.terminal-area').hide();`
    $stdout.write_proc = `typeof(process) === 'object' ? function(s){process.stdout.write(s)} : function(s){console.log(s)}`
    $stderr.write_proc = `typeof(process) === 'object' ? function(s){process.stderr.write(s)} : function(s){console.warn(s)}`
  `} else {`
    `$('body').addClass('terminal-open');`
    `$('.terminal-area').show();`
    $stdout.write_proc = proc {|str| `#{@term}.Write(str, 'line')` }
    $stderr.write_proc = proc {|str| `#{@term}.Write(str, 'error')` }

    `#{@term}.Focus();`
  `}`
end