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 57df794

Browse filesBrowse files
committed
working on github demo
1 parent 874fae5 commit 57df794
Copy full SHA for 57df794

File tree

Expand file treeCollapse file tree

5 files changed

+311
-69
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+311
-69
lines changed

‎github.html

Copy file name to clipboard
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<meta charset="utf-8">
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
5+
<title>Shell testbed</title>
6+
7+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
8+
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
9+
<script src="js/readline.js"></script>
10+
<script src="js/shell.js"></script>
11+
<script src="js/github.js"></script>
12+
<style type="text/css">
13+
#mt-console {
14+
display: none;
15+
height: 400px;
16+
width: 100%;
17+
opacity: 0.9;
18+
background-color: #002f05;
19+
color: #00fe00;
20+
position: fixed;
21+
padding: 20px 20px 20px 20px;
22+
top: 0;
23+
z-index: 1000;
24+
overflow: scroll;
25+
}</style>
26+
<script type="text/javascript">
27+
var history = new ReadLine.History();
28+
var shell = new GitHubShell();
29+
$(document).ready(function() {
30+
var console = $('#gh-console');
31+
function showConsole() {
32+
console.slideDown();
33+
console.focus();
34+
}
35+
function hideConsole() {
36+
console.slideUp();
37+
console.blur();
38+
}
39+
$(document).on('keyup', function(e) {
40+
if(e.keyCode && e.keyCode === 192 && e.shiftKey) {
41+
showConsole();
42+
} else if(e.keyCode && e.keyCode === 27) {
43+
hideConsole();
44+
}
45+
});
46+
console.focus(function() {
47+
shell.activate();
48+
});
49+
console.blur(function() {
50+
shell.deactivate();
51+
});
52+
showConsole();
53+
});
54+
</script>
55+
</head>
56+
<body>
57+
<div id="gh-console">
58+
<div id="shell"></div>
59+
</div>
60+
</body>
61+
</html>

‎index.html

Copy file name to clipboardExpand all lines: index.html
+17-55Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<html>
22
<head>
33
<meta charset="utf-8">
44
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
@@ -8,19 +8,19 @@
88
<script src="js/readline.js"></script>
99
<script src="js/shell.js"></script>
1010
<style type="text/css">
11-
#shell {
12-
display: none;
13-
height: 400px;
14-
width: 100%;
15-
opacity: 0.8;
16-
background-color: #002f05;
17-
color: #00fe00;
18-
position: fixed;
19-
padding: 20px 20px 20px 20px;
20-
top: 0;
21-
z-index: 1000;
22-
overflow: hidden;
23-
}
11+
#shell-panel {
12+
display: none;
13+
height: 400px;
14+
width: 100%;
15+
opacity: 0.8;
16+
background-color: #002f05;
17+
color: #00fe00;
18+
position: fixed;
19+
padding: 20px 20px 20px 20px;
20+
top: 0;
21+
z-index: 1000;
22+
overflow: hidden;
23+
}
2424
</style>
2525
<script type="text/javascript">
2626
var history = new ReadLine.History();
@@ -141,46 +141,8 @@
141141
</script>
142142
</head>
143143
<body>
144-
<div id="quake-console">
145-
<div id="line">jsh$ <span id="line-input"></span></div>
146-
</div>
147-
<div id="shell"></div>
148-
<p>&nbsp;</p>
149-
150-
<p>&nbsp;</p>
151-
152-
<p>&nbsp;</p>
153-
154-
<p>&nbsp;</p>
155-
156-
<p>&nbsp;</p>
157-
158-
<p>&nbsp;</p>
159-
160-
<p>&nbsp;</p>
161-
162-
<p>&nbsp;</p>
163-
164-
<p>&nbsp;</p>
165-
166-
<p>&nbsp;</p>
167-
168-
<p>&nbsp;</p>
169-
170-
<h2>Key event:</h2>
171-
<pre id="debug"></pre>
172-
<div id="x"></div>
173-
<div id="keydown"></div>
174-
<div id="keypress"></div>
175-
<script>
176-
// document.onkeydown = function (e) {
177-
// $('#keydown').text("keydown - code: "+ e.keyCode);
178-
// return true;
179-
// }
180-
// document.onkeypress = function(e) {
181-
// $('#keypress').text("keypress - code: "+ e.keyCode+", char: "+ String.fromCharCode(e.keyCode)+", ctrl: "+ e.ctrlKey);
182-
// return true;
183-
// }
184-
</script>
144+
<div id="shell-panel">
145+
<div id="shell-view"></div>
146+
</div>
185147
</body>
186148
</html>

‎js/github.js

Copy file name to clipboard
+232Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
(function($, _, document, window, localStorage) {
2+
GitHubShell = function(config) {
3+
config = config || {};
4+
var shellConfig = {};
5+
if(config.history) {
6+
shellConfig.history = config.history;
7+
}
8+
if(config.shell_view_id) {
9+
shellConfig.shell_view_id = config.shell_view_id;
10+
} else {
11+
config.shell_view_id
12+
}
13+
var _shell = new Shell(shellConfig);
14+
var _commandList = _.sortBy(['go', 'ls', 'cd', 'show', 'pwd', 'help','clear'], function(x) {
15+
return x;
16+
});
17+
var _namespaces = {
18+
0: "",
19+
2: "User:",
20+
10: "Template:",
21+
101: "Special:"
22+
};
23+
var _home = null;
24+
var _current = null;
25+
var _localState = {
26+
active: false
27+
};
28+
// public methods
29+
var self = {
30+
activate: function() {
31+
_shell.activate();
32+
},
33+
deactivate: function() {
34+
_shell.deactivate();
35+
},
36+
onDeactivate: function(completionHandler) {
37+
_shell.onDeactivate(completionHandler);
38+
}
39+
};
40+
41+
// private methods
42+
function completionHandler(line, callback) {
43+
var partial = line.text.substr(0, line.cursor);
44+
var parts = split(partial);
45+
console.log(parts);
46+
if(parts.length == 0) {
47+
return callback({suggestions: _commandList});
48+
}
49+
if(parts.length == 1) {
50+
var cmd = parts[0];
51+
if(startsWith(cmd, ".") || startsWith(cmd, "/")) {
52+
return completePath(cmd, callback);
53+
}
54+
return callback(bestMatch(cmd, _commandList));
55+
}
56+
return completePath(parts[1], callback);
57+
}
58+
59+
function completePath(path, callback) {
60+
var uri = "/@api/deki/console/matches?path=" + encodeURIComponent(path);
61+
if(_current) {
62+
uri = uri + "&current=" + _current.Id;
63+
}
64+
$.getJSON(uri, function(data) {
65+
console.log(data);
66+
callback(bestMatch(path, data.Matches));
67+
});
68+
}
69+
70+
function cmdHandler(cmdtext, input_id, callback) {
71+
var parts = split(cmdtext);
72+
var cmd = parts[0];
73+
var path = parts[1];
74+
if(startsWith(cmd, ".") || startsWith(cmd, "/") || startsWith(cmd, "#")) {
75+
path = cmd;
76+
cmd = "show";
77+
}
78+
switch(cmd) {
79+
case "clear":
80+
$(input_id).siblings().remove();
81+
callback();
82+
return;
83+
case "help":
84+
var content = $('<div><div><strong>Commands:</strong></div></div>');
85+
_.each(_commandList, function(command) {
86+
content.append(_.template('<div>&nbsp;<%=command%></div>', {command: command}))
87+
});
88+
$(input_id).after(content);
89+
callback();
90+
return;
91+
case "go":
92+
getPage(path, function(page) {
93+
var path = getPath(page);
94+
console.log("navigating to " + path);
95+
window.location = path;
96+
});
97+
return;
98+
case"ls":
99+
ls(input_id, path, callback);
100+
return;
101+
case"pwd":
102+
$(input_id).after(getPath(_current));
103+
callback();
104+
return;
105+
case "cd":
106+
getPage(path, function(page) {
107+
callback(setCurrent(page));
108+
});
109+
return;
110+
case "show":
111+
getPage(path, function(page) {
112+
show(input_id, page, callback);
113+
});
114+
return;
115+
default:
116+
var content = _.template('<div><strong>Unrecognized command:&nbsp;</strong><%=cmd%></div>', {cmd: cmd});
117+
$(input_id).after(content);
118+
callback();
119+
return;
120+
}
121+
}
122+
123+
function setCurrent(page) {
124+
_current = page;
125+
return getPath(_current) + " $";
126+
}
127+
128+
function getPath(page) {
129+
return " /" + _namespaces[page.Namespace] + page.Path;
130+
}
131+
132+
function getSegment(page) {
133+
var segments = page.Path.split('/');
134+
return segments[segments.length - 1];
135+
}
136+
137+
function ls(input_id, path, callback) {
138+
getPage(path, function(page) {
139+
getChildren(page, function(pages) {
140+
var p = _.map(pages, function(page) {
141+
var lsInfo = { id: page.Id, segment: getSegment(page), name: page.DisplayName};
142+
lsInfo.name = lsInfo.name || lsInfo.segment;
143+
return lsInfo;
144+
});
145+
console.log(p);
146+
$(input_id).after(_.template("<% _.each(p, function(page) { %><div>[<%=page.id%>]&nbsp;<%=page.segment%> (<%=page.name%>)</div> <% }); %>", {p: p}));
147+
callback();
148+
});
149+
});
150+
}
151+
152+
function getChildren(page, callback) {
153+
$.getJSON("/@api/deki/console/node/" + page.Id + "/children", function(children) {
154+
callback(children);
155+
});
156+
}
157+
158+
function show(input_id, page, callback) {
159+
var content = $(
160+
'<div>' +
161+
'<div><strong>Id:&nbsp;</strong><span class="id"></span></div>' +
162+
'<div><strong>Path:&nbsp;</strong><span class="path"></span></div>' +
163+
'<div><strong>Displayname:&nbsp;</strong><span class="display"></span></div>' +
164+
'<div><strong>Parent Id:&nbsp;</strong><span class="parentid"></span></div>' +
165+
'<div><strong>Child Ids:&nbsp;</strong><span class="childids"></span></div>' +
166+
'</div>'
167+
);
168+
$(content).find('.id').text(page.Id);
169+
$(content).find('.path').text(" /" + _namespaces[page.Namespace] + page.Path);
170+
$(content).find('.display').text(page.DisplayName);
171+
$(content).find('.parentid').text(page.ParentId);
172+
$(content).find('.childids').text("[" + page.ChildIds + "]");
173+
$(input_id).after(content);
174+
callback();
175+
}
176+
177+
function split(str) {
178+
var parts = str.split(/\s+/);
179+
if(parts.length > 0 && !parts[parts.length - 1]) {
180+
parts.pop();
181+
}
182+
return parts;
183+
}
184+
185+
function startsWith(str1, str2) {
186+
return str1.slice(0, str2.length) == str2;
187+
}
188+
189+
function bestMatch(partial, possible) {
190+
var completions = [];
191+
var common = '';
192+
for(var i = 0; i < possible.length; i++) {
193+
var option = possible[i];
194+
if(option.slice(0, partial.length) == partial) {
195+
completions.push(option);
196+
if(!common) {
197+
common = option;
198+
console.log("initial common:" + common);
199+
} else if(option.slice(0, common.length) != common) {
200+
console.log("find common stem for '" + common + "' and '" + option + "'");
201+
var j = partial.length;
202+
while(j < common.length && j < option.length) {
203+
if(common[j] != option[j]) {
204+
common = common.substr(0, j);
205+
break;
206+
}
207+
j++;
208+
}
209+
}
210+
}
211+
}
212+
if(completions.length == 0) {
213+
return;
214+
}
215+
if(completions.length == 1) {
216+
completions = null;
217+
}
218+
return {
219+
result: common.substr(partial.length),
220+
suggestions: completions
221+
};
222+
}
223+
224+
225+
_shell.onCompletion(completionHandler);
226+
_shell.onCmd(cmdHandler);
227+
228+
return self;
229+
};
230+
231+
})
232+
(jQuery, _, document, window, localStorage);

‎mtshell.html

Copy file name to clipboardExpand all lines: mtshell.html
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>Shell testbed</title>
66

77
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
8+
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
89
<script src="js/readline.js"></script>
910
<script src="js/shell.js"></script>
1011
<script src="js/mtshell.js"></script>

0 commit comments

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