Skip to content

Commit 8277a28

Browse files
committed
Add filtering
1 parent add1c84 commit 8277a28

File tree

4 files changed

+209
-62
lines changed

4 files changed

+209
-62
lines changed

static/commands.coffee

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
commands = window.commands = {}
2-
31
class CommandSet
42

53
constructor: ->
@@ -23,19 +21,17 @@ class CommandSet
2321
add: (name, func) ->
2422
this.commands[name] = func
2523

26-
addFilter: (type, name, matchFunc, obj) ->
27-
if not (type of this.filters)
28-
this.filters[type] = {}
29-
if not (name of this.filters[type])
30-
this.filters[type][name] = []
31-
this.filters[type][name].push([matchFunc, obj])
24+
addFilter: (name, matchFunc, obj) ->
25+
if not (name of this.filters)
26+
this.filters[name] = []
27+
this.filters[name].push([matchFunc, obj])
3228

33-
match: (type, name, thing) ->
29+
match: (name, thing) ->
3430
result = []
3531
for matchName in [name, '*']
36-
if this.filters[type][matchName]
37-
for matchFunc, obj in this.filters[type][matchName]
38-
if not matchFunc or matchFunc(thing)
32+
if this.filters[matchName]
33+
for [matchFunc, obj] in this.filters[matchName]
34+
if (not matchFunc?) or matchFunc(thing)
3935
result.push(obj)
4036
return result
4137

@@ -80,18 +76,45 @@ commandSet.add '', (command, outputer, console) ->
8076
outputer(code: 0)
8177

8278

83-
commandSet.addFilter 'output', 'ls', null, {
79+
commandSet.addFilter 'ls', null, {
8480
complete: false,
8581
filterStdout: (callback, command, data) ->
8682
lines = data.split(/\n/)
87-
result = $('<span></span>')
8883
for line in lines
89-
a = $('<a></a>')
90-
a.attr('href', command.cwd + '/' + line)
91-
a.text(line)
92-
a.addClass('file')
93-
result.append(a)
94-
callback(result)
84+
if not line
85+
continue
86+
result = $('<span class="file"></span>')
87+
parts = parseLs(line)
88+
result.attr(parts)
89+
result.text(parts.filename)
90+
callback(result)
91+
callback($('<br>'))
92+
changeCommand: (command) ->
93+
command.args.push('-l')
9594
}
9695

96+
getMatch = (regex, line) ->
97+
m = regex.exec(line)
98+
if m
99+
rest = line.substr(m.index + m[0].length)
100+
rest = rest.replace(/^\s+/, '')
101+
return [m[0], rest]
102+
else
103+
return [null, line]
104+
105+
parseLs = (line) ->
106+
results = {}
107+
[results.perms, rest] = getMatch(/[drwx-]+/, line)
108+
[huh, rest] = getMatch(/\d+/, rest)
109+
[results.user, rest] = getMatch(/[a-zA-Z][a-zA-Z0-9_-]*/, rest)
110+
[results.group, rest] = getMatch(/[a-zA-Z][a-zA-Z0-9_-]*/, rest)
111+
[results.size, rest] = getMatch(/[0-9]+[MKG]?/, rest)
112+
[results.date, rest] = getMatch(/\d\d\d\d-\d\d-\d\d/, rest)
113+
[results.time, rest] = getMatch(/\d\d:\d\d/, rest)
114+
if rest.search(/->/) != -1
115+
m = /^(.*)\s+->\s+(.*)$/.exec(rest)
116+
results.symlink = m[1]
117+
rest = m[2]
118+
results.filename = rest
119+
return results
97120

static/commands.js

Lines changed: 57 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/site.coffee

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ $( () ->
1010
mainConsole.focus()
1111
)
1212

13+
$('.file').live('click', expandFile);
14+
1315
)
1416

1517
processEnv = null
@@ -187,7 +189,8 @@ class Console
187189
return result
188190

189191
clearConsole: ->
190-
$('.command-set, .incomplete-command-set', this.el).remove()
192+
#$('.command-set, .incomplete-command-set', this.el).remove()
193+
$('.output', this.el).html('')
191194
this.scroller.reinitialise()
192195
this.scroller.scrollToBottom()
193196
this.persist()
@@ -225,6 +228,8 @@ class Console
225228
# alt-R
226229
return false
227230

231+
commandRegistry: {}
232+
228233
runInputCommand: ->
229234
inputEl = $('input.input', this.el)
230235
input = inputEl.val()
@@ -248,6 +253,13 @@ class Console
248253
this.writeEl(div)
249254
parts = node.toArgsNoInterpolate()
250255
command = new Command(parts[0], parts[1...], this.cwd(), this.env())
256+
this.commandRegistry[sym] = command
257+
filters = commandSet.match(command.command, command)
258+
if filters.length
259+
for filter in filters
260+
if filter.changeCommand
261+
## FIXME: should be asyncable
262+
filter.changeCommand(command)
251263
command.runCommand(
252264
callback: this.boundReceiver,
253265
id: this.callbackId + '.' + sym,
@@ -314,6 +326,7 @@ class Console
314326
p.save('history', this.history)
315327
p.save('cwd', this.cwd())
316328
p.save('env', this.env())
329+
p.save('genSym', genSym.counter)
317330

318331
restore: ->
319332
p = this.persister
@@ -324,29 +337,57 @@ class Console
324337
this.scroller.reinitialise()
325338
this.scroller.scrollToBottom()
326339
this.persistRestored = true
340+
c = p.get('genSym', 0)
341+
if c > genSym.counter
342+
genSym.counter = c
327343

328344
dataReceived: (id, data) ->
329345
console.log 'data', id, data
346+
command = this.commandRegistry[id]
347+
if command
348+
filters = commandSet.match(command.command, command)
349+
else
350+
filters = []
330351
if data.stdout? or data.stderr?
331352
if data.stdout?
332353
cls = 'stdout'
333354
else
334355
cls = 'stderr'
335-
this.write(data.stdout || data.stderr, cls, id)
356+
matched = false
357+
if filters.length
358+
for filter in filters
359+
## FIXME: check for .complete
360+
if data.stdout and filter.filterStdout
361+
filter.filterStdout(((el) =>
362+
this.writeEl(el, id)
363+
),
364+
command,
365+
data.stdout)
366+
matched = true
367+
break
368+
if data.stderr and filter.filterStderr
369+
filter.filterStderr(((el) =>
370+
this.writeEl(el)
371+
),
372+
command,
373+
data.stderr)
374+
matched = true
375+
break
376+
if not matched
377+
this.write(data.stdout || data.stderr, cls, id)
336378
# Ignore the other stuff
337379
if data.code? and id
338380
el = $('#' + id, this.el)
339381
el.removeClass('incomplete-command-set')
340382
el.addClass('command-set')
341383
el = $('.cmd', el)
342-
if el.attr('title').search(/pid/) != -1
384+
if (el.attr('title') || '').search(/pid/) != -1
343385
title = el.attr('title')
344386
title = title.replace(/pid\:\s*\d+\s*/, '')
345387
el.attr('title', title)
346388
this.persistSoon()
347389
if data.pid? and id
348390
el = $('.cmd', $('#' + id, this.el))
349-
console.log 'el', el
350391
el.attr(title: 'pid: ' + data.pid + ' ' + el.attr('title'))
351392

352393
expandWildcard = (callback, pattern, cwd, env) ->
@@ -368,7 +409,6 @@ expandWildcard = (callback, pattern, cwd, env) ->
368409
command.getOutput(
369410
((stdout, stderr) ->
370411
files = stdout.split('\u0000')
371-
console.log 'result', stdout, files
372412
doc = new Node('span')
373413
for file in files
374414
if file.substr(0, 2) == './'
@@ -425,3 +465,7 @@ class Persister
425465
return JSON.parse(v)
426466
else
427467
return defaultValue
468+
469+
expandFile = (event) ->
470+
## FIXME: expand to show some more info
471+
$(event.target).css("background-color": "#f00")

0 commit comments

Comments
 (0)