forked from eldersantos/community
/
Console.coffee
101 lines (80 loc) · 3.12 KB
/
Console.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
###
Copyright (c) 2002-2011 "Neo Technology,"
Network Engine for Objects in Lund AB [http://neotechnology.com]
This file is part of Neo4j.
Neo4j is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
###
define ['lib/backbone'], () ->
class Console extends Backbone.Model
defaults :
lines : []
history : []
historyIndex : 0
showPrompt : false
prompt : ""
initialize : (opts) =>
@server = opts.server
@lang = opts.lang
@setStatement "init()"
@eval false, false
# Set the current statement (== current prompt input)
setStatement : (str, opts={}) =>
@set {prompt : str}, opts
# Evaluate the current statement line
# (the current prompt input)
eval : (showStatement=true, includeInHistory=true, prepend = @lang) =>
statement = @get 'prompt'
@set {"showPrompt":false, prompt:""}, {silent:true}
if showStatement
@pushLines [statement], prepend + "> "
if includeInHistory and statement is not ''
@pushHistory statement
@executeStatement statement
executeStatement : (statement) ->
@server.manage.console.exec statement, @lang, @parseEvalResult
prevHistory : =>
history = @get "history"
historyIndex = @get "historyIndex"
if history.length > historyIndex
historyIndex++
historyItem = history[history.length - historyIndex]
@set {historyIndex:historyIndex, prompt:historyItem}
nextHistory : =>
history = @get "history"
historyIndex = @get "historyIndex"
if historyIndex > 1
historyIndex--
historyItem = history[history.length - historyIndex]
@set {historyIndex:historyIndex, prompt:historyItem}
else if historyIndex is 1
historyIndex--
@set {historyIndex:historyIndex, prompt:""}
else
@set {prompt:""}
parseEvalResult : (result) =>
if _(result).isString() && result.length > 0
if result.substr(-1) is "\n"
result = result.substr(0, result.length - 1)
result = result.split "\n"
else
result = []
@set {"showPrompt":true},{silent:true}
@pushLines result
pushHistory : (statement) =>
if statement.length > 0
history = @get("history")
if history.length is 0 or history[history.length-1] != statement
@set({history : history.concat([statement]), historyIndex:0}, {silent:true})
pushLines : (lines, prepend="==> ") =>
lines = for line in lines
prepend + line
@set("lines" : @get("lines").concat(lines))