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 27b5681

Browse filesBrowse files
committed
working on per cmd completion infrastructure
1 parent e9ea29f commit 27b5681
Copy full SHA for 27b5681

File tree

Expand file treeCollapse file tree

5 files changed

+188
-86
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+188
-86
lines changed

‎index.html

Copy file name to clipboardExpand all lines: index.html
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@
9898
if(!parts || parts.length == 0) {
9999
return callback(current);
100100
}
101-
if(parts[0] == ".") {
102-
// do nothing
101+
if(!parts[0]) {
102+
current = root;
103+
} else if(parts[0] == ".") {
104+
// carry on
103105
} else if(parts[0] == "..") {
104106
current = current.parent;
105107
} else {
@@ -123,7 +125,7 @@
123125
var shell = Shell({history: history});
124126
var pathhandler = Josh.PathHandler();
125127
pathhandler.current = root;
126-
pathhandler.attach(shell);
128+
//pathhandler.attach(shell);
127129
pathhandler.getNode = function(path, callback) {
128130
if(!path) {
129131
return callback();

‎js/github.js

Copy file name to clipboardExpand all lines: js/github.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
var content = $('<div></div>');
9898
var itemTemplate = _.template("<div><%- i %>&nbsp;<%- cmd %></div>");
9999
_.each(_history.items(), function(cmd, i) {
100-
content.append(itemTemplate({cmd: cmd, i: i}));
100+
content.append(itemTemplate({exec: cmd, i: i}));
101101
});
102102
$(input_id).after(content);
103103
}

‎js/pathhandler.js

Copy file name to clipboardExpand all lines: js/pathhandler.js
+24-8Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ var Josh = Josh || {};
44
var self = {
55
attach: function(shell) {
66
shell.onCompletion(completionHandler);
7-
shell.setCommandHandler("ls", ls);
8-
shell.setCommandHandler("pwd", pwd);
9-
shell.setCommandHandler("cd", cd);
10-
shell.setCommandHandler("_default", exec);
7+
shell.setCommandHandler("ls", self.handlers.ls);
8+
shell.setCommandHandler("pwd", self.handlers.pwd);
9+
shell.setCommandHandler("cd", self.handlers.cd);
10+
shell.setCommandHandler("_default", self.handlers.exec);
1111
shell.setPrompt(self.getPrompt());
1212
},
1313
handlers: {
14-
ls: ls,
15-
exec: exec,
16-
cd: cd,
17-
pwd: pwd
14+
ls: {
15+
exec: ls,
16+
completion: pathCompletionHandler
17+
},
18+
exec: {
19+
exec: exec,
20+
completion: pathCompletionHandler
21+
},
22+
cd: {
23+
exec: cd,
24+
completion: pathCompletionHandler
25+
},
26+
pwd: {
27+
exec: pwd,
28+
completion: pathCompletionHandler
29+
}
1830
},
1931
templates: {
2032
not_found: _.template("<div><%=cmd%>: <%=path%>: No such file or directory</div>"),
@@ -50,6 +62,10 @@ var Josh = Josh || {};
5062
current: null
5163
};
5264

65+
function pathCompletionHandler() {
66+
67+
}
68+
5369
function exec(cmd, args, callback) {
5470
self.withPrompt(callback);
5571
}

‎js/readline.js

Copy file name to clipboardExpand all lines: js/readline.js
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,12 @@
394394
}
395395

396396
function refresh() {
397-
if(_completionActive) {
398-
_completionActive = false;
399-
if(_onCompletion) {
400-
_onCompletion();
401-
}
402-
}
397+
// if(_completionActive) {
398+
// _completionActive = false;
399+
// if(_onCompletion) {
400+
// _onCompletion();
401+
// }
402+
// }
403403
if(_onChange) {
404404
_onChange(self.getLine());
405405
}

‎js/shell.js

Copy file name to clipboardExpand all lines: js/shell.js
+152-68Lines changed: 152 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,54 @@
1717
var _active = false;
1818
var _cursor_visible = false;
1919
var _suggestion;
20+
var _itemTemplate = _.template("<div><%- i %>&nbsp;<%- cmd %></div>");
2021
var _cmdHandlers = {
21-
clear: function(cmd, args, callback) {
22-
$(_input_id).parent().empty();
23-
callback();
22+
clear: {
23+
exec: function(cmd, args, callback) {
24+
$(_input_id).parent().empty();
25+
callback();
26+
}
2427
},
25-
help: function(cmd, args, callback) {
26-
var content = $('<div><div><strong>Commands:</strong></div></div>');
27-
var itemTemplate = _.template('<div>&nbsp;<%=command%></div>');
28-
_.each(commands(), function(command) {
29-
content.append(itemTemplate({command: command}))
30-
});
31-
callback(content);
28+
help: {
29+
exec: function(cmd, args, callback) {
30+
var content = $('<div><div><strong>Commands:</strong></div></div>');
31+
var itemTemplate = _.template('<div>&nbsp;<%=command%></div>');
32+
_.each(commands(), function(command) {
33+
content.append(itemTemplate({command: command}))
34+
});
35+
callback(content);
36+
},
37+
completion: function(cmd, arg, line, callback) {
38+
callback(self.bestMatch(arg, self.commands()))
39+
}
3240
},
33-
history: function(cmd, args, callback) {
34-
if(args[0] == "-c") {
35-
_history.clear();
36-
callback();
37-
return;
41+
history: {
42+
exec: function(cmd, args, callback) {
43+
if(args[0] == "-c") {
44+
_history.clear();
45+
callback();
46+
return;
47+
}
48+
var content = $('<div></div>');
49+
_.each(_history.items(), function(cmd, i) {
50+
content.append(_itemTemplate({cmd: cmd, i: i}));
51+
});
52+
callback(content);
53+
}
54+
},
55+
_unknown: {
56+
exec: function(cmd, args, callback) {
57+
var content = _.template('<div><strong>Unrecognized command:&nbsp;</strong><%=cmd%></div>', {cmd: cmd});
58+
callback(content);
59+
},
60+
completion: function(cmd, arg, line, callback) {
61+
callback(self.bestMatch(arg, self.commands()))
3862
}
39-
var content = $('<div></div>');
40-
var itemTemplate = _.template("<div><%- i %>&nbsp;<%- cmd %></div>");
41-
_.each(_history.items(), function(cmd, i) {
42-
content.append(itemTemplate({cmd: cmd, i: i}));
43-
});
44-
callback(content);
4563
},
46-
_unknown: function(cmd, args, callback) {
47-
var content = _.template('<div><strong>Unrecognized command:&nbsp;</strong><%=cmd%></div>', {cmd: cmd});
48-
callback(content);
64+
_none: {
65+
completion: function(cmd, arg, line, callback) {
66+
callback(self.bestMatch(arg, self.commands()))
67+
}
4968
}
5069
};
5170
var _line = {
@@ -84,6 +103,9 @@
84103
setCommandHandler: function(cmd, cmdHandler) {
85104
_cmdHandlers[cmd] = cmdHandler;
86105
},
106+
getCommandHandler: function(cmd) {
107+
return _cmdHandlers[cmd];
108+
},
87109
setPrompt: function(prompt) {
88110
_prompt = prompt;
89111
if(!_active) {
@@ -97,35 +119,6 @@
97119
onDeactivate: function(completionHandler) {
98120
_readline.onDeactivate(completionHandler);
99121
},
100-
onCompletion: function(completionHandler) {
101-
_readline.onCompletion(function(line, callback) {
102-
if(_suggestion) {
103-
$(_suggest_id).remove();
104-
_suggestion = null;
105-
}
106-
if(!line) {
107-
return;
108-
}
109-
completionHandler(line, function(completion) {
110-
console.log("completion: " + completion)
111-
if(!completion) {
112-
callback();
113-
return;
114-
}
115-
if(completion.suggestions) {
116-
_suggestion = $(_suggest_html);
117-
for(var i = 0; i < completion.suggestions.length; i++) {
118-
console.log("suggestion: " + completion.suggestions[i]);
119-
_suggestion.append($("<div></div>").text(completion.suggestions[i]));
120-
}
121-
console.log(_suggestion);
122-
$(_input_id).after(_suggestion);
123-
}
124-
self.scrollToBottom();
125-
callback(completion.result);
126-
});
127-
});
128-
},
129122
render: function() {
130123
var text = _line.text || '';
131124
var cursorIdx = _line.cursor || 0;
@@ -158,6 +151,52 @@
158151
scrollToBottom: function() {
159152
//_panel.scrollTop(_shell.height());
160153
_panel.animate({scrollTop: _view.height()}, 0);
154+
},
155+
bestMatch: function(partial, possible) {
156+
var result = {
157+
completion: null,
158+
suggestions: []
159+
};
160+
if(!possible || possible.length == 0) {
161+
return result;
162+
}
163+
var common = '';
164+
if(!partial) {
165+
if(possible.length == 1) {
166+
result.completion = possible[0];
167+
result.suggestions = possible;
168+
return result;
169+
}
170+
if(!_.every(possible, function(x) {
171+
return possible[0][0] == x[0]
172+
})) {
173+
result.suggestions = possible;
174+
return result;
175+
}
176+
common = possible[0][0];
177+
}
178+
for(var i = 0; i < possible.length; i++) {
179+
var option = possible[i];
180+
if(option.slice(0, partial.length) == partial) {
181+
result.suggestions.push(option);
182+
if(!common) {
183+
common = option;
184+
console.log("initial common:" + common);
185+
} else if(option.slice(0, common.length) != common) {
186+
console.log("find common stem for '" + common + "' and '" + option + "'");
187+
var j = partial.length;
188+
while(j < common.length && j < option.length) {
189+
if(common[j] != option[j]) {
190+
common = common.substr(0, j);
191+
break;
192+
}
193+
j++;
194+
}
195+
}
196+
}
197+
}
198+
result.completion = common.substr(partial.length);
199+
return result;
161200
}
162201
};
163202

@@ -186,15 +225,34 @@
186225
}
187226

188227
function split(str) {
189-
var parts = str.split(/\s+/);
190-
if(parts.length > 0 && !parts[parts.length - 1]) {
191-
parts.pop();
228+
return _.filter(str.split(/\s+/), function(x) {
229+
return x;
230+
});
231+
}
232+
233+
function getHandler(cmd) {
234+
var handler;
235+
if(!cmd) {
236+
handler = _cmdHandlers._none;
237+
} else {
238+
handler = _cmdHandlers[cmd];
192239
}
193-
return parts;
240+
if(!handler) {
241+
handler = _cmdHandlers._default;
242+
if(!handler) {
243+
handler = _cmdHandlers._unknown;
244+
}
245+
}
246+
return handler;
194247
}
195248

196249
// init
197250
_readline.onChange(function(line) {
251+
if(_suggestion) {
252+
$(_suggest_id).remove();
253+
_suggestion = null;
254+
self.scrollToBottom();
255+
}
198256
_line = line;
199257
self.render();
200258
});
@@ -217,15 +275,8 @@
217275
var parts = split(cmdtext);
218276
var cmd = parts[0];
219277
var args = parts.slice(1);
220-
var handler = _cmdHandlers[cmd];
221-
if(!handler) {
222-
handler = _cmdHandlers['_default'];
223-
if(!handler) {
224-
handler = _cmdHandlers['_unknown'];
225-
}
226-
}
227-
return handler(cmd, args, function(output, prompt, cmdtext) {
228-
console.log("finished command " + cmd);
278+
var handler = getHandler(cmd);
279+
return handler.exec(cmd, args, function(output, prompt, cmdtext) {
229280
if(output) {
230281
$(_input_id).after(output);
231282
}
@@ -238,10 +289,43 @@
238289
callback(cmdtext);
239290
});
240291
});
292+
_readline.onCompletion(function(line, callback) {
293+
if(_suggestion) {
294+
$(_suggest_id).remove();
295+
_suggestion = null;
296+
}
297+
if(!line) {
298+
return callback();
299+
}
300+
var cmd = _.first(split(line.text));
301+
console.log("getting completion handler for "+cmd);
302+
var handler = getHandler(cmd);
303+
if(!handler.completion) {
304+
return callback();
305+
}
306+
console.log("calling completion handler for "+cmd);
307+
var partial = line.text.substr(0, line.cursor);
308+
var arg = _.last(split(partial));
309+
return handler.completion(cmd, arg, line, function(match) {
310+
console.log("completion: " + JSON.stringify(match));
311+
if(!match) {
312+
return callback();
313+
}
314+
if(match.suggestions) {
315+
_suggestion = $(_suggest_html);
316+
for(var i = 0; i < match.suggestions.length; i++) {
317+
console.log("suggestion: " + match.suggestions[i]);
318+
_suggestion.append($("<div></div>").text(match.suggestions[i]));
319+
}
320+
$(_input_id).after(_suggestion);
321+
}
322+
self.scrollToBottom();
323+
return callback(match.completion);
324+
});
325+
});
241326
_readline.onEOT(self.deactivate);
242327
_readline.onCancel(self.deactivate);
243328
return self;
244329
}
245330
;
246-
})
247-
(jQuery, _, document, window);
331+
})(jQuery, _, document, window);

0 commit comments

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