@@ -25,36 +25,26 @@ var Josh = Josh || {};
25
25
}
26
26
} ) ;
27
27
var _prompt = config . prompt || 'jsh$' ;
28
- var _shell_view_id = config . shell_view_id || '#shell-view' ;
29
- var _shell_panel_id = config . shell_panel_id || '#shell-panel' ;
30
- var _input_id = config . input_id || '#shell-cli' ;
31
- var _input_html = config . input_html || '<div id="shell-cli"><strong class="prompt"></strong> <span class="input"><span class="left"/><span class="cursor"/><span class="right"/></span></div>' ;
32
- var _search_html = config . search_html || '<div id="shell-cli">(reverse-i-search)`<span class="searchterm"></span>\': <span class="input"><span class="left"/><span class="cursor"/><span class="right"/></span></div>' ;
33
- var _suggest_html = config . suggest_html || '<div id="shell-suggest"></div>' ;
34
- var _suggest_id = config . suggest_id = "#shell-suggest" ;
28
+ var _shell_view_id = config . shell_view_id || 'shell-view' ;
29
+ var _shell_panel_id = config . shell_panel_id || 'shell-panel' ;
30
+ var _input_id = config . input_id || 'shell-cli' ;
35
31
var _blinktime = config . blinktime || 500 ;
36
32
var _history = config . history || new Josh . History ( ) ;
37
33
var _readline = config . readline || new Josh . ReadLine ( { history : _history , console : _console } ) ;
38
34
var _active = false ;
39
35
var _cursor_visible = false ;
40
- var _itemTemplate = _ . template ( "<div><%- i %> <%- cmd %></div>" ) ;
41
36
var _activationHandler ;
42
37
var _deactivationHandler ;
43
38
var _cmdHandlers = {
44
39
clear : {
45
40
exec : function ( cmd , args , callback ) {
46
- $ ( _input_id ) . parent ( ) . empty ( ) ;
41
+ $ ( id ( _input_id ) ) . parent ( ) . empty ( ) ;
47
42
callback ( ) ;
48
43
}
49
44
} ,
50
45
help : {
51
46
exec : function ( cmd , args , callback ) {
52
- var content = $ ( '<div><div><strong>Commands:</strong></div></div>' ) ;
53
- var itemTemplate = _ . template ( '<div> <%=command%></div>' ) ;
54
- _ . each ( commands ( ) , function ( command ) {
55
- content . append ( itemTemplate ( { command : command } ) )
56
- } ) ;
57
- callback ( content ) ;
47
+ callback ( self . templates . help ( { commands : commands ( ) } ) ) ;
58
48
}
59
49
} ,
60
50
history : {
@@ -64,17 +54,12 @@ var Josh = Josh || {};
64
54
callback ( ) ;
65
55
return ;
66
56
}
67
- var content = $ ( '<div></div>' ) ;
68
- _ . each ( _history . items ( ) , function ( cmd , i ) {
69
- content . append ( _itemTemplate ( { cmd : cmd , i : i } ) ) ;
70
- } ) ;
71
- callback ( content ) ;
57
+ callback ( self . templates . history ( { items : _history . items ( ) } ) ) ;
72
58
}
73
59
} ,
74
60
_default : {
75
61
exec : function ( cmd , args , callback ) {
76
- var content = _ . template ( '<div><strong>Unrecognized command: </strong><%=cmd%></div>' , { cmd : cmd } ) ;
77
- callback ( content ) ;
62
+ callback ( self . templates . bad_command ( { cmd : cmd } ) ) ;
78
63
} ,
79
64
completion : function ( cmd , arg , line , callback ) {
80
65
if ( ! arg ) {
@@ -97,11 +82,20 @@ var Josh = Josh || {};
97
82
// public methods
98
83
var self = {
99
84
commands : commands ,
85
+ templates : {
86
+ history : _ . template ( "<div><% _.each(items, function(cmd, i) { %><div><%- i %> <%- cmd %></div><% }); %></div>" ) ,
87
+ help : _ . template ( "<div><div><strong>Commands:</strong></div><% _.each(commands, function(cmd) { %><div> <%- cmd %></div><% }); %></div>" ) ,
88
+ bad_command : _ . template ( '<div><strong>Unrecognized command: </strong><%=cmd%></div>' ) ,
89
+ input_cmd : _ . template ( '<div id="<%- id %>"><strong class="prompt"></strong> <span class="input"><span class="left"/><span class="cursor"/><span class="right"/></span></div>' ) ,
90
+ input_search : _ . template ( '<div id="<%- id %>">(reverse-i-search)`<span class="searchterm"></span>\': <span class="input"><span class="left"/><span class="cursor"/><span class="right"/></span></div>' ) ,
91
+ suggest : _ . template ( "<div><% _.each(suggestions, function(suggestion) { %><div><%- suggestion %></div><% }); %></div>" )
92
+
93
+ } ,
100
94
isActive : function ( ) {
101
95
return _readline . isActive ( ) ;
102
96
} ,
103
97
activate : function ( ) {
104
- if ( $ ( _shell_view_id ) . length == 0 ) {
98
+ if ( $ ( id ( _shell_view_id ) ) . length == 0 ) {
105
99
_active = false ;
106
100
return ;
107
101
}
@@ -149,25 +143,25 @@ var Josh = Josh || {};
149
143
if ( _searchMatch ) {
150
144
cursorIdx = _searchMatch . cursoridx || 0 ;
151
145
text = _searchMatch . text || '' ;
152
- $ ( _input_id + ' .searchterm' ) . text ( _searchMatch . term ) ;
146
+ $ ( id ( _input_id ) + ' .searchterm' ) . text ( _searchMatch . term ) ;
153
147
}
154
148
var left = _ . escape ( text . substr ( 0 , cursorIdx ) ) . replace ( / / g, ' ' ) ;
155
149
var cursor = text . substr ( cursorIdx , 1 ) ;
156
150
var right = _ . escape ( text . substr ( cursorIdx + 1 ) ) . replace ( / / g, ' ' ) ;
157
- $ ( _input_id + ' .prompt' ) . text ( _prompt ) ;
158
- $ ( _input_id + ' .input .left' ) . html ( left ) ;
151
+ $ ( id ( _input_id ) + ' .prompt' ) . text ( _prompt ) ;
152
+ $ ( id ( _input_id ) + ' .input .left' ) . html ( left ) ;
159
153
if ( ! cursor ) {
160
- $ ( _input_id + ' .input .cursor' ) . html ( ' ' ) . css ( 'textDecoration' , 'underline' ) ;
154
+ $ ( id ( _input_id ) + ' .input .cursor' ) . html ( ' ' ) . css ( 'textDecoration' , 'underline' ) ;
161
155
} else {
162
- $ ( _input_id + ' .input .cursor' ) . text ( cursor ) . css ( 'textDecoration' , 'underline' ) ;
156
+ $ ( id ( _input_id ) + ' .input .cursor' ) . text ( cursor ) . css ( 'textDecoration' , 'underline' ) ;
163
157
}
164
- $ ( _input_id + ' .input .right' ) . html ( right ) ;
158
+ $ ( id ( _input_id ) + ' .input .right' ) . html ( right ) ;
165
159
_cursor_visible = true ;
166
160
self . scrollToBottom ( ) ;
167
161
_console . log ( 'rendered "' + text + '" w/ cursor at ' + cursorIdx ) ;
168
162
} ,
169
163
refresh : function ( ) {
170
- $ ( _input_id ) . replaceWith ( _input_html ) ;
164
+ $ ( id ( _input_id ) ) . replaceWith ( self . templates . input_cmd ( { id : _input_id } ) ) ;
171
165
self . render ( ) ;
172
166
_console . log ( 'refreshed ' + _input_id ) ;
173
167
@@ -223,6 +217,10 @@ var Josh = Josh || {};
223
217
}
224
218
} ;
225
219
220
+ function id ( id ) {
221
+ return "#" + id ;
222
+ }
223
+
226
224
function commands ( ) {
227
225
return _ . chain ( _cmdHandlers ) . keys ( ) . filter ( function ( x ) {
228
226
return x [ 0 ] != "_"
@@ -239,9 +237,9 @@ var Josh = Josh || {};
239
237
}
240
238
_cursor_visible = ! _cursor_visible ;
241
239
if ( _cursor_visible ) {
242
- $ ( _input_id + ' .input .cursor' ) . css ( 'textDecoration' , 'underline' ) ;
240
+ $ ( id ( _input_id ) + ' .input .cursor' ) . css ( 'textDecoration' , 'underline' ) ;
243
241
} else {
244
- $ ( _input_id + ' .input .cursor' ) . css ( 'textDecoration' , '' ) ;
242
+ $ ( id ( _input_id ) + ' .input .cursor' ) . css ( 'textDecoration' , '' ) ;
245
243
}
246
244
blinkCursor ( ) ;
247
245
} , _blinktime ) ;
@@ -259,11 +257,11 @@ var Josh = Josh || {};
259
257
260
258
function renderOutput ( output , callback ) {
261
259
if ( output ) {
262
- $ ( _input_id ) . after ( output ) ;
260
+ $ ( id ( _input_id ) ) . after ( output ) ;
263
261
}
264
- $ ( _input_id + ' .input .cursor' ) . css ( 'textDecoration' , '' ) ;
265
- $ ( _input_id ) . removeAttr ( 'id' ) ;
266
- $ ( _shell_view_id ) . append ( _input_html ) ;
262
+ $ ( id ( _input_id ) + ' .input .cursor' ) . css ( 'textDecoration' , '' ) ;
263
+ $ ( id ( _input_id ) ) . removeAttr ( 'id' ) ;
264
+ $ ( id ( _shell_view_id ) ) . append ( self . templates . input_cmd ( { id : _input_id } ) ) ;
267
265
if ( _promptHandler ) {
268
266
return _promptHandler ( function ( prompt ) {
269
267
self . setPrompt ( prompt ) ;
@@ -276,13 +274,13 @@ var Josh = Josh || {};
276
274
function activate ( ) {
277
275
_console . log ( "activating shell" ) ;
278
276
if ( ! _view ) {
279
- _view = $ ( _shell_view_id ) ;
277
+ _view = $ ( id ( _shell_view_id ) ) ;
280
278
}
281
279
if ( ! _panel ) {
282
- _panel = $ ( _shell_panel_id ) ;
280
+ _panel = $ ( id ( _shell_panel_id ) ) ;
283
281
}
284
- if ( $ ( _input_id ) . length == 0 ) {
285
- _view . append ( _input_html ) ;
282
+ if ( $ ( id ( _input_id ) ) . length == 0 ) {
283
+ _view . append ( self . templates . input_cmd ( { id : _input_id } ) ) ;
286
284
}
287
285
self . refresh ( ) ;
288
286
_active = true ;
@@ -323,11 +321,11 @@ var Josh = Josh || {};
323
321
} ) ;
324
322
} ) ;
325
323
_readline . onSearchStart ( function ( ) {
326
- $ ( _input_id ) . replaceWith ( _search_html ) ;
324
+ $ ( id ( _input_id ) ) . replaceWith ( self . templates . input_search ( { id : _input_id } ) ) ;
327
325
_console . log ( 'started search' ) ;
328
326
} ) ;
329
327
_readline . onSearchEnd ( function ( ) {
330
- $ ( _input_id ) . replaceWith ( _input_html ) ;
328
+ $ ( id ( _input_id ) ) . replaceWith ( self . templates . input_cmd ( { id : _input_id } ) ) ;
331
329
_searchMatch = null ;
332
330
self . render ( ) ;
333
331
_console . log ( "ended search" ) ;
@@ -376,12 +374,7 @@ var Josh = Josh || {};
376
374
return callback ( ) ;
377
375
}
378
376
if ( match . suggestions && match . suggestions . length > 1 ) {
379
- var suggestion = $ ( _suggest_html ) ;
380
- for ( var i = 0 ; i < match . suggestions . length ; i ++ ) {
381
- _console . log ( "suggestion: " + match . suggestions [ i ] ) ;
382
- suggestion . append ( $ ( "<div></div>" ) . text ( match . suggestions [ i ] ) ) ;
383
- }
384
- return renderOutput ( suggestion , function ( ) {
377
+ return renderOutput ( self . templates . suggest ( { suggestions : match . suggestions } ) , function ( ) {
385
378
callback ( match . completion ) ;
386
379
} ) ;
387
380
}
0 commit comments