Permalink
Browse files

hello world

  • Loading branch information...
0 parents commit ba98ed8b95cd4f965f08eaaea56162755d41f7ea @larryng committed May 12, 2012
Showing with 280 additions and 0 deletions.
  1. +13 −0 LICENSE
  2. +83 −0 index.html
  3. +8 −0 js/coffee-script.js
  4. +176 −0 js/main.js
13 LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2012, Larry Ng <ng.larry@gmail.com>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
83 index.html
@@ -0,0 +1,83 @@
+<!DOCTYPE html>
+<html>
+ <title>In-browser CoffeeScript REPL</title>
+ <head>
+ <style>
+
+ pre {
+ margin: 0;
+ padding: 0;
+ }
+
+ body {
+ background-color: #222;
+ color: #fff;
+ font-size: 1em;
+ }
+
+ #container {
+ width: 100%;
+ height: 100%;
+ font-family: monospace;
+ }
+
+ #output {
+ word-wrap: break-word;
+ }
+
+ #inputdiv {
+ width: 100%;
+ }
+
+ #inputl {
+ display: inline;
+ }
+
+ #inputr {
+ display: inline;
+ }
+
+ #input {
+ border: none;
+ outline: none;
+ background-color: #222;
+ color: #fff;
+ font-family: monospace;
+ margin: 0;
+ padding: 0;
+ font-size: 1em;
+ resize: none;
+ position: absolute;
+ overflow: hidden;
+ }
+
+ #inputcopy {
+ display: none;
+ white-space: pre-wrap;
+ min-height: 2em;
+ padding: 0;
+ margin: 0;
+ word-wrap: break-word;
+ }
+
+ </style>
+ </head>
+ <body>
+ <div id="container">
+ <div id="output"></div>
+ <div id="inputdiv">
+ <div id="inputl">
+ <code id="prompt">coffee&gt;&nbsp;</code>
+ </div>
+ <div id="inputr">
+ <textarea id="input"></textarea>
+ <div id="inputcopy"></div>
+ </div>
+ </div>
+ </div>
+
+ <script src="js/coffee-script.js"></script>
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <script src="js/main.js"></script>
+ </body>
+</html>
8 js/coffee-script.js
8 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
176 js/main.js
@@ -0,0 +1,176 @@
+/*
+ * In-browser CoffeeScript REPL
+ *
+ * written by Larry Ng
+ */
+
+$(function () {
+
+ var $output = $('#output');
+ var $input = $('#input');
+ var $prompt = $('#prompt');
+ var $inputdiv = $('#inputdiv');
+ var $inputl = $('#inputl');
+ var $inputr = $('#inputr');
+ var $inputcopy = $('#inputcopy');
+
+ var HISTORY = [];
+ var HISTORY_I = -1;
+ var SAVED = "";
+ var MULTILINE = false;
+
+ var print = function (s) {
+ s = s || " ";
+ $output[0].innerHTML += '<pre>' + s + '</pre>';
+ }
+
+ var grabInput = function () {
+ var tmp = $input.val();
+ $input.val('');
+ return tmp;
+ }
+
+ var processSaved = function () {
+ try {
+ compiled = CoffeeScript.compile(SAVED);
+ compiled = compiled.slice(14, compiled.length-17);
+ value = eval.call(window, compiled);
+ window['$_'] = value;
+ output = String(value);
+ } catch (e) {
+ if (e.stack) {
+ output = e.stack;
+
+ // FF doesn't have Error.toString() as the first line of Error.stack
+ // while Chrome does.
+ if (output.split('\n')[0] !== e.toString()) {
+ output = e.toString() + '\n' + e.stack;
+ }
+ } else {
+ output = e.toString();
+ }
+ }
+ SAVED = "";
+ print(output);
+ }
+
+ var setPrompt = function (multi) {
+ var s = multi ? "------" : "coffee";
+ $prompt.html(s + '&gt;&nbsp;');
+ }
+
+ var addToSaved = function (s) {
+ if (s[s.length-1] === '\\') {
+ SAVED += s.substring(0, s.length-1);
+ } else {
+ SAVED += s;
+ }
+ addToHistory(s);
+ SAVED += '\n';
+ }
+
+ var addToHistory = function (s) {
+ HISTORY.unshift(s);
+ HISTORY_I = -1;
+ }
+
+ var resizeInput = function (e) {
+ // using the invisible div trick
+ // see: http://www.impressivewebs.com/textarea-auto-resize/
+
+ var width = $inputdiv.width() - $inputl.width();
+ var content = $input.val();
+ content.replace(/\n/g, '<br/>');
+ $inputcopy.html(content);
+
+ $inputcopy.width(width);
+ $input.width(width);
+ $input.height($inputcopy.height());
+ }
+
+ var scrollToBottom = function () {
+ window.scrollTo(0, $prompt[0].offsetTop);
+ }
+
+ var inputKeypress = function (e) {
+ var compiled;
+ var input;
+ var value;
+ var output;
+
+ if (e.which === 13) {
+ e.preventDefault();
+ input = grabInput();
+
+ print($prompt.html() + input);
+
+ if (input) {
+ addToSaved(input);
+ if (input[input.length-1] !== '\\' && !MULTILINE) {
+ processSaved();
+ }
+ }
+ } else if (e.which === 27) {
+ e.preventDefault();
+ input = $input.val();
+
+ if (input) {
+ if (MULTILINE) {
+ if (SAVED) {
+ input = grabInput();
+
+ print($prompt.html() + input);
+ addToSaved(input);
+ processSaved();
+ }
+ }
+ } else {
+ if (MULTILINE) {
+ if (SAVED) {
+ processSaved();
+ }
+ }
+ }
+
+ MULTILINE = !MULTILINE;
+ setPrompt(MULTILINE);
+ } else if (e.which === 38) {
+ e.preventDefault();
+
+ if (HISTORY_I < HISTORY.length-1) {
+ HISTORY_I += 1;
+ $input.val(HISTORY[HISTORY_I]);
+ }
+ } else if (e.which === 40) {
+ e.preventDefault();
+
+ if (HISTORY_I > 0) {
+ HISTORY_I += -1;
+ $input.val(HISTORY[HISTORY_I]);
+ }
+ }
+
+ scrollToBottom();
+ };
+
+ var printKeyPressed = function (e) {
+ print(e.which);
+ }
+
+ // bind handlers
+ $input.keydown(inputKeypress);
+ $(window).resize(resizeInput);
+ $input.keyup(resizeInput);
+ $input.change(resizeInput);
+
+ // initialize
+ resizeInput();
+ $input.focus();
+
+ // header
+ print("// CoffeeScript v" + CoffeeScript.VERSION + " REPL")
+ print("//");
+ print("// Press Esc to toggle multiline mode.");
+ print();
+
+})

0 comments on commit ba98ed8

Please sign in to comment.