This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.coffee
273 lines (213 loc) · 7.67 KB
/
main.coffee
1
2
3
4
5
6
7
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
45
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# In-browser todo.txt REPL
# http://github.com/Leftium/todo.html
#
# written by John-Kim Murphy
#
#
# based on:
# https://github.com/larryng/coffeescript-repl
require [
'version',
'jquery',
'coffee-script',
'nodeutil',
'store',
'os',
'node_modules/todo.txt-node/lib/todo',
'shellwords',
], (
version,
$,
CoffeeScript,
nodeutil,
store,
os,
todo,
shellwords
) ->
$ ->
SAVED_CONSOLE_LOG = console.log
$output = $('#output')
$input = $('#input')
$prompt = $('#prompt')
$inputdiv = $('#inputdiv')
$inputl = $('#inputl')
$inputr = $('#inputr')
$inputcopy = $('#inputcopy')
escapeHTML = (s) ->
s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
class TodoREPL
DEFAULT_SETTINGS =
lastVariable: '$_'
maxLines: 500
maxDepth: 2
showHidden: false
colorize: true
constructor: (@output, @input, @prompt, settings={}) ->
@history = store.get 'history'
@historyi = -1
@saved = ''
@multiline = false
@settings = $.extend({}, DEFAULT_SETTINGS)
if localStorage and localStorage.settings
for k, v of JSON.parse(localStorage.settings)
@settings[k] = v
for k, v of settings
@settings[k] = v
@input.keydown @handleKeypress
resetSettings: ->
localStorage.clear()
saveSettings: ->
localStorage.settings = JSON.stringify($.extend({}, @settings))
print: (args...) =>
s = args.join(' ') or ' '
o = @output[0].innerHTML + s + '\n'
@output[0].innerHTML = o.split('\n')[[email protected]...].join('\n')
undefined
processSaved: =>
argv = shellwords.split(@saved)
if argv[0] in ['todo', 'td', 't']
@saved = ''
output = todo.run argv[1..]
else
try
if CoffeeScript.compile?
compiled = CoffeeScript.compile @saved
compiled = compiled[14...-17]
else
compiled = @saved # JavaScript
value = eval.call window, compiled
window[@settings.lastVariable] = value
output = nodeutil.inspect value, @settings.showHidden, @settings.maxDepth, @settings.colorize
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] isnt e.toString()
output = "#{e.toString()}\n\nStack trace:\n#{e.stack}"
output = output.replace /file.*\//g, ''
else
output = e.toString()
output = output.split('\n')[0]
@saved = ''
@print output
setPrompt: =>
s = if @multiline then '----' else 'todo'
@prompt.html "#{s}> "
addToHistory: (s) =>
@history = (h for h in @history when h isnt s)
@history.unshift s
store.set 'history', @history
@historyi = -1
addToSaved: (s) =>
@saved += if s[...-1] is '\\' then s[0...-1] else s
@saved += '\n'
@addToHistory s
clear: =>
@output[0].innerHTML = ''
undefined
handleKeypress: (e) =>
switch e.which
when 13 # enter
e.preventDefault()
input = @input.val()
@input.val ''
@print @prompt.html() + escapeHTML(input)
if input
@addToSaved input
if input[...-1] isnt '\\' and not @multiline
@processSaved()
when 27 # esc
e.preventDefault()
input = @input.val()
if input and @multiline and @saved
input = @input.val()
@input.val ''
@print @prompt.html() + escapeHTML(input)
@addToSaved input
@processSaved()
else if @multiline and @saved
@processSaved()
@multiline = not @multiline
@setPrompt()
when 38 # up
e.preventDefault()
if @historyi < @history.length-1
@historyi += 1
@input.val @history[@historyi]
when 40 # down
e.preventDefault()
if @historyi > 0
@historyi += -1
@input.val @history[@historyi]
when 76, 108 # L, l
if e.ctrlKey
e.preventDefault()
@clear()
resizeInput = (e) ->
width = $inputdiv.width() - $inputl.width()
content = $input.val()
content.replace /\n/g, '<br/>'
$inputcopy.html content
$inputcopy.width width
$input.width width
$input.height $inputcopy.height() + 2
scrollToBottom = ->
window.scrollTo 0, $prompt[0].offsetTop
init = ->
# bind other handlers
$input.keydown scrollToBottom
$(window).resize resizeInput
$input.keyup resizeInput
$input.change resizeInput
$('html').click (e) ->
if e.clientY > $input[0].offsetTop
$input.focus()
# instantiate our REPL
repl = new TodoREPL $output, $input, $prompt
# replace console.log
console.log = (args...) ->
SAVED_CONSOLE_LOG.apply console, args
repl.print args...
# expose repl as $$
window.$$ = repl
# make font visible
$('body').css('opacity', 1)
# initialize window
resizeInput()
$input.focus()
document.title = "todo.html v#{version}"
window.version = () ->
repl.print """
<b>todo.html by <a href=http://leftium.com>john-kim murphy</a></b>
v#{version} | based on <a href=http://todotxt.com>idea</a> by <a href=http://ginatrapani.org>gina trapani</a> | <a href=http://www.gnu.org/copyleft/gpl.html>gpl license</a> | <a href=https://github.com/Leftium/todo.html>fork me on github</a>!
"""
# help
window.help = ->
repl.print """
<strong>Features</strong>
<strong>========</strong>
+ Built-in JavaScript interpreter.
+ <strong>[Esc]</strong> toggles multiline mode.
+ <strong>[Up]/[Down] arrow</strong> flips through line history.
+ <strong>#{repl.settings.lastVariable}</strong> stores the last returned value.
+ Access the internals of this console through <strong>$$</strong>.
+ <strong>$$.clear()</strong> clears this console.
<strong>Settings</strong>
<strong>========</strong>
You can modify the behavior of this REPL by altering <strong>$$.settings</strong>:
+ <strong>lastVariable</strong> (#{repl.settings.lastVariable}): variable name in which last returned value is stored
+ <strong>maxLines</strong> (#{repl.settings.maxLines}): max line count of this console
+ <strong>maxDepth</strong> (#{repl.settings.maxDepth}): max depth in which to inspect outputted object
+ <strong>showHidden</strong> (#{repl.settings.showHidden}): flag to output hidden (not enumerable) properties of objects
+ <strong>colorize</strong> (#{repl.settings.colorize}): flag to colorize output (set to false if REPL is slow)
<strong>$$.saveSettings()</strong> will save settings to localStorage.
<strong>$$.resetSettings()</strong> will reset settings to default.
"""
os.ui.echo = repl.print
todo.init store.get('env') or {}, os.fs, os.ui, os.system
# print header
@version()
repl.print 'Welcome! `todo ls +basics` starts a product tour.'
init()