Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6b4f703

Browse filesBrowse files
committed
moved all shell html to shell.templates
1 parent 5342e83 commit 6b4f703
Copy full SHA for 6b4f703

File tree

Expand file treeCollapse file tree

1 file changed

+41
-48
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+41
-48
lines changed

‎js/shell.js

Copy file name to clipboardExpand all lines: js/shell.js
+41-48Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,26 @@ var Josh = Josh || {};
2525
}
2626
});
2727
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>&nbsp;<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>\':&nbsp;<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';
3531
var _blinktime = config.blinktime || 500;
3632
var _history = config.history || new Josh.History();
3733
var _readline = config.readline || new Josh.ReadLine({history: _history, console: _console});
3834
var _active = false;
3935
var _cursor_visible = false;
40-
var _itemTemplate = _.template("<div><%- i %>&nbsp;<%- cmd %></div>");
4136
var _activationHandler;
4237
var _deactivationHandler;
4338
var _cmdHandlers = {
4439
clear: {
4540
exec: function(cmd, args, callback) {
46-
$(_input_id).parent().empty();
41+
$(id(_input_id)).parent().empty();
4742
callback();
4843
}
4944
},
5045
help: {
5146
exec: function(cmd, args, callback) {
52-
var content = $('<div><div><strong>Commands:</strong></div></div>');
53-
var itemTemplate = _.template('<div>&nbsp;<%=command%></div>');
54-
_.each(commands(), function(command) {
55-
content.append(itemTemplate({command: command}))
56-
});
57-
callback(content);
47+
callback(self.templates.help({commands: commands()}));
5848
}
5949
},
6050
history: {
@@ -64,17 +54,12 @@ var Josh = Josh || {};
6454
callback();
6555
return;
6656
}
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()}));
7258
}
7359
},
7460
_default: {
7561
exec: function(cmd, args, callback) {
76-
var content = _.template('<div><strong>Unrecognized command:&nbsp;</strong><%=cmd%></div>', {cmd: cmd});
77-
callback(content);
62+
callback(self.templates.bad_command({cmd: cmd}));
7863
},
7964
completion: function(cmd, arg, line, callback) {
8065
if(!arg) {
@@ -97,11 +82,20 @@ var Josh = Josh || {};
9782
// public methods
9883
var self = {
9984
commands: commands,
85+
templates: {
86+
history: _.template("<div><% _.each(items, function(cmd, i) { %><div><%- i %>&nbsp;<%- cmd %></div><% }); %></div>"),
87+
help: _.template("<div><div><strong>Commands:</strong></div><% _.each(commands, function(cmd) { %><div>&nbsp;<%- cmd %></div><% }); %></div>"),
88+
bad_command: _.template('<div><strong>Unrecognized command:&nbsp;</strong><%=cmd%></div>'),
89+
input_cmd: _.template('<div id="<%- id %>"><strong class="prompt"></strong>&nbsp;<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>\':&nbsp;<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+
},
10094
isActive: function() {
10195
return _readline.isActive();
10296
},
10397
activate: function() {
104-
if($(_shell_view_id).length == 0) {
98+
if($(id(_shell_view_id)).length == 0) {
10599
_active = false;
106100
return;
107101
}
@@ -149,25 +143,25 @@ var Josh = Josh || {};
149143
if(_searchMatch) {
150144
cursorIdx = _searchMatch.cursoridx || 0;
151145
text = _searchMatch.text || '';
152-
$(_input_id + ' .searchterm').text(_searchMatch.term);
146+
$(id(_input_id) + ' .searchterm').text(_searchMatch.term);
153147
}
154148
var left = _.escape(text.substr(0, cursorIdx)).replace(/ /g, '&nbsp;');
155149
var cursor = text.substr(cursorIdx, 1);
156150
var right = _.escape(text.substr(cursorIdx + 1)).replace(/ /g, '&nbsp;');
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);
159153
if(!cursor) {
160-
$(_input_id + ' .input .cursor').html('&nbsp;').css('textDecoration', 'underline');
154+
$(id(_input_id) + ' .input .cursor').html('&nbsp;').css('textDecoration', 'underline');
161155
} else {
162-
$(_input_id + ' .input .cursor').text(cursor).css('textDecoration', 'underline');
156+
$(id(_input_id) + ' .input .cursor').text(cursor).css('textDecoration', 'underline');
163157
}
164-
$(_input_id + ' .input .right').html(right);
158+
$(id(_input_id) + ' .input .right').html(right);
165159
_cursor_visible = true;
166160
self.scrollToBottom();
167161
_console.log('rendered "' + text + '" w/ cursor at ' + cursorIdx);
168162
},
169163
refresh: function() {
170-
$(_input_id).replaceWith(_input_html);
164+
$(id(_input_id)).replaceWith(self.templates.input_cmd({id:_input_id}));
171165
self.render();
172166
_console.log('refreshed ' + _input_id);
173167

@@ -223,6 +217,10 @@ var Josh = Josh || {};
223217
}
224218
};
225219

220+
function id(id) {
221+
return "#"+id;
222+
}
223+
226224
function commands() {
227225
return _.chain(_cmdHandlers).keys().filter(function(x) {
228226
return x[0] != "_"
@@ -239,9 +237,9 @@ var Josh = Josh || {};
239237
}
240238
_cursor_visible = !_cursor_visible;
241239
if(_cursor_visible) {
242-
$(_input_id + ' .input .cursor').css('textDecoration', 'underline');
240+
$(id(_input_id) + ' .input .cursor').css('textDecoration', 'underline');
243241
} else {
244-
$(_input_id + ' .input .cursor').css('textDecoration', '');
242+
$(id(_input_id) + ' .input .cursor').css('textDecoration', '');
245243
}
246244
blinkCursor();
247245
}, _blinktime);
@@ -259,11 +257,11 @@ var Josh = Josh || {};
259257

260258
function renderOutput(output, callback) {
261259
if(output) {
262-
$(_input_id).after(output);
260+
$(id(_input_id)).after(output);
263261
}
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}));
267265
if(_promptHandler) {
268266
return _promptHandler(function(prompt) {
269267
self.setPrompt(prompt);
@@ -276,13 +274,13 @@ var Josh = Josh || {};
276274
function activate() {
277275
_console.log("activating shell");
278276
if(!_view) {
279-
_view = $(_shell_view_id);
277+
_view = $(id(_shell_view_id));
280278
}
281279
if(!_panel) {
282-
_panel = $(_shell_panel_id);
280+
_panel = $(id(_shell_panel_id));
283281
}
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}));
286284
}
287285
self.refresh();
288286
_active = true;
@@ -323,11 +321,11 @@ var Josh = Josh || {};
323321
});
324322
});
325323
_readline.onSearchStart(function() {
326-
$(_input_id).replaceWith(_search_html);
324+
$(id(_input_id)).replaceWith(self.templates.input_search({id:_input_id}));
327325
_console.log('started search');
328326
});
329327
_readline.onSearchEnd(function() {
330-
$(_input_id).replaceWith(_input_html);
328+
$(id(_input_id)).replaceWith(self.templates.input_cmd({id:_input_id}));
331329
_searchMatch = null;
332330
self.render();
333331
_console.log("ended search");
@@ -376,12 +374,7 @@ var Josh = Josh || {};
376374
return callback();
377375
}
378376
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() {
385378
callback(match.completion);
386379
});
387380
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.