]> git.r.bdr.sh - rbdr/dotfiles/blob
9f069775015f26ab9d3f8f2c14518c3136740a38
[rbdr/dotfiles] /
1 // Generated by CoffeeScript 1.8.0
2 (function() {
3 var CoffeeScript, addHistory, addMultilineHandler, fs, getCommandId, merge, nodeREPL, path, replDefaults, updateSyntaxError, vm, _ref;
4
5 fs = require('fs');
6
7 path = require('path');
8
9 vm = require('vm');
10
11 nodeREPL = require('repl');
12
13 CoffeeScript = require('./coffee-script');
14
15 _ref = require('./helpers'), merge = _ref.merge, updateSyntaxError = _ref.updateSyntaxError;
16
17 replDefaults = {
18 prompt: 'coffee> ',
19 historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,
20 historyMaxInputSize: 10240,
21 "eval": function(input, context, filename, cb) {
22 var Assign, Block, Literal, Value, ast, err, js, result, _ref1;
23 input = input.replace(/\uFF00/g, '\n');
24 input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
25 _ref1 = require('./nodes'), Block = _ref1.Block, Assign = _ref1.Assign, Value = _ref1.Value, Literal = _ref1.Literal;
26 try {
27 ast = CoffeeScript.nodes(input);
28 ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);
29 js = ast.compile({
30 bare: true,
31 locals: Object.keys(context)
32 });
33 result = context === global ? vm.runInThisContext(js, filename) : vm.runInContext(js, context, filename);
34 return cb(null, result);
35 } catch (_error) {
36 err = _error;
37 updateSyntaxError(err, input);
38 return cb(err);
39 }
40 }
41 };
42
43 addMultilineHandler = function(repl) {
44 var inputStream, multiline, nodeLineListener, origPrompt, outputStream, rli, _ref1;
45 rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
46 origPrompt = (_ref1 = repl._prompt) != null ? _ref1 : repl.prompt;
47 multiline = {
48 enabled: false,
49 initialPrompt: origPrompt.replace(/^[^> ]*/, function(x) {
50 return x.replace(/./g, '-');
51 }),
52 prompt: origPrompt.replace(/^[^> ]*>?/, function(x) {
53 return x.replace(/./g, '.');
54 }),
55 buffer: ''
56 };
57 nodeLineListener = rli.listeners('line')[0];
58 rli.removeListener('line', nodeLineListener);
59 rli.on('line', function(cmd) {
60 if (multiline.enabled) {
61 multiline.buffer += "" + cmd + "\n";
62 rli.setPrompt(multiline.prompt);
63 rli.prompt(true);
64 } else {
65 rli.setPrompt(origPrompt);
66 nodeLineListener(cmd);
67 }
68 });
69 return inputStream.on('keypress', function(char, key) {
70 if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
71 return;
72 }
73 if (multiline.enabled) {
74 if (!multiline.buffer.match(/\n/)) {
75 multiline.enabled = !multiline.enabled;
76 rli.setPrompt(origPrompt);
77 rli.prompt(true);
78 return;
79 }
80 if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
81 return;
82 }
83 multiline.enabled = !multiline.enabled;
84 rli.line = '';
85 rli.cursor = 0;
86 rli.output.cursorTo(0);
87 rli.output.clearLine(1);
88 multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
89 rli.emit('line', multiline.buffer);
90 multiline.buffer = '';
91 } else {
92 multiline.enabled = !multiline.enabled;
93 rli.setPrompt(multiline.initialPrompt);
94 rli.prompt(true);
95 }
96 });
97 };
98
99 addHistory = function(repl, filename, maxSize) {
100 var buffer, fd, lastLine, readFd, size, stat;
101 lastLine = null;
102 try {
103 stat = fs.statSync(filename);
104 size = Math.min(maxSize, stat.size);
105 readFd = fs.openSync(filename, 'r');
106 buffer = new Buffer(size);
107 fs.readSync(readFd, buffer, 0, size, stat.size - size);
108 repl.rli.history = buffer.toString().split('\n').reverse();
109 if (stat.size > maxSize) {
110 repl.rli.history.pop();
111 }
112 if (repl.rli.history[0] === '') {
113 repl.rli.history.shift();
114 }
115 repl.rli.historyIndex = -1;
116 lastLine = repl.rli.history[0];
117 } catch (_error) {}
118 fd = fs.openSync(filename, 'a');
119 repl.rli.addListener('line', function(code) {
120 if (code && code.length && code !== '.history' && lastLine !== code) {
121 fs.write(fd, "" + code + "\n");
122 return lastLine = code;
123 }
124 });
125 repl.rli.on('exit', function() {
126 return fs.close(fd);
127 });
128 return repl.commands[getCommandId(repl, 'history')] = {
129 help: 'Show command history',
130 action: function() {
131 repl.outputStream.write("" + (repl.rli.history.slice(0).reverse().join('\n')) + "\n");
132 return repl.displayPrompt();
133 }
134 };
135 };
136
137 getCommandId = function(repl, commandName) {
138 var commandsHaveLeadingDot;
139 commandsHaveLeadingDot = repl.commands['.help'] != null;
140 if (commandsHaveLeadingDot) {
141 return "." + commandName;
142 } else {
143 return commandName;
144 }
145 };
146
147 module.exports = {
148 start: function(opts) {
149 var build, major, minor, repl, _ref1;
150 if (opts == null) {
151 opts = {};
152 }
153 _ref1 = process.versions.node.split('.').map(function(n) {
154 return parseInt(n);
155 }), major = _ref1[0], minor = _ref1[1], build = _ref1[2];
156 if (major === 0 && minor < 8) {
157 console.warn("Node 0.8.0+ required for CoffeeScript REPL");
158 process.exit(1);
159 }
160 CoffeeScript.register();
161 process.argv = ['coffee'].concat(process.argv.slice(2));
162 opts = merge(replDefaults, opts);
163 repl = nodeREPL.start(opts);
164 repl.on('exit', function() {
165 return repl.outputStream.write('\n');
166 });
167 addMultilineHandler(repl);
168 if (opts.historyFile) {
169 addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
170 }
171 repl.commands[getCommandId(repl, 'load')].help = 'Load code from a file into this REPL session';
172 return repl;
173 }
174 };
175
176 }).call(this);