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
32
33
# 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

  $stdout.write_proc = proc {|str| `#{@term}.Write(str, 'line')` }
  $stderr.write_proc = proc {|str| `#{@term}.Write(str, 'error')` }
end

Instance Method Details

#command(command, callback) ⇒ Object



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

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



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

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

#restore_historyObject



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

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

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

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

#stash_historyObject



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

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

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

#toggle_consoleObject



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

def toggle_console
  `
  if ($('body').is('.terminal-open')) {
    $('body').removeClass('terminal-open');
    $('.terminal-area').hide();
  } else {
    $('body').addClass('terminal-open');
    $('.terminal-area').show();
    #{@term}.Focus();
  }
  `
end