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 54d171d

Browse filesBrowse files
committed
full readline, shell and pathhandler implementation with example
2 parents 709fe82 + 01df104 commit 54d171d
Copy full SHA for 54d171d

File tree

Expand file treeCollapse file tree

10 files changed

+994
-365
lines changed
Filter options
Expand file treeCollapse file tree

10 files changed

+994
-365
lines changed

‎.gitignore

Copy file name to clipboard
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/workspace.xml
2-
.idea/tasks.xml
2+
.idea/tasks.xml
3+
.DS_Store

‎.idea/inspectionProfiles/Project_Default.xml

Copy file name to clipboardExpand all lines: .idea/inspectionProfiles/Project_Default.xml
+7Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/inspectionProfiles/profiles_settings.xml

Copy file name to clipboardExpand all lines: .idea/inspectionProfiles/profiles_settings.xml
+7Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/jsLinters/jslint.xml

Copy file name to clipboardExpand all lines: .idea/jsLinters/jslint.xml
+3-1Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎index.html

Copy file name to clipboard
100644100755
+15-167Lines changed: 15 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,37 @@
1-
<!DOCTYPE html>
1+
<html>
22
<head>
33
<meta charset="utf-8">
44
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
55
<title>Shell testbed</title>
66

7+
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro' rel='stylesheet' type='text/css'>
78
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
9+
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
10+
<script src="js/history.js"></script>
811
<script src="js/readline.js"></script>
912
<script src="js/shell.js"></script>
13+
<script src="js/pathhandler.js"></script>
14+
<script src="js/example.js"></script>
1015
<style type="text/css">
11-
#quake-console {
16+
#shell-panel {
1217
display: none;
1318
height: 400px;
1419
width: 100%;
1520
opacity: 0.9;
16-
background-color: gray;
17-
color: white;
21+
background-color: #002f05;
22+
color: #00fe00;
1823
position: fixed;
1924
padding: 20px 20px 20px 20px;
2025
top: 0;
2126
z-index: 1000;
27+
font-family: 'Source Code Pro', sans-serif;
28+
font-size: 0.9em;
29+
overflow: hidden;
2230
}</style>
23-
<script type="text/javascript">
24-
var history = new ReadLine.History();
25-
var shell = new Shell();
26-
var dir = {
27-
"Guide-Admin": {
28-
"AccessControlPanel": {},
29-
"AddingUsers": {},
30-
"GettingStarted": {}
31-
},
32-
"Guide-User": {
33-
"GettingStarted": {},
34-
"ContentCreation": {},
35-
"ContentManagement": {}
36-
}
37-
};
38-
39-
$(document).ready(function() {
40-
// r.onKeydown(function (key) {
41-
// $('#debug').text(JSON.stringify(key));
42-
// });
43-
// r.onChange(function (line) {
44-
// $('#line-input').text(line.text);
45-
// });
46-
var quakeConsole = $('#quake-console');
47-
$(document).on('keyup', function(e) {
48-
if(e.keyCode && e.keyCode === 192 && e.shiftKey) {
49-
quakeConsole.slideDown();
50-
quakeConsole.focus();
51-
} else if(e.keyCode && e.keyCode === 27) {
52-
quakeConsole.slideUp();
53-
quakeConsole.blur();
54-
console.log("blur");
55-
}
56-
});
57-
quakeConsole.focus(function() {
58-
shell.activate();
59-
});
60-
quakeConsole.blur(function() {
61-
shell.deactivate();
62-
});
63-
shell.onCmd(function(cmd, input_id, callback) {
64-
var content = $('<div><strong>Command:&nbsp;</strong><span class="cmd"></span></div>');
65-
$(content).find('.cmd').text(cmd);
66-
$(input_id).after(content);
67-
window.setTimeout(callback, 1000);
68-
});
69-
shell.onCompletion(function(line, callback) {
70-
console.log("slow completion");
71-
window.setTimeout(function() {
72-
var match = line.text.substr(0, line.cursor);
73-
if(!match) {
74-
callback(bestMatch("", dir));
75-
return;
76-
}
77-
var parts = match.split(" ");
78-
var cur = dir;
79-
match = parts[parts.length - 1];
80-
var segments = match.split("/");
81-
var last = segments[segments.length - 1];
82-
for(var i = 0; i < segments.length; i++) {
83-
var segment = segments[i];
84-
if(!cur[segment]) {
85-
if(i == segments.length - 1) {
86-
87-
// look for partial match
88-
callback(bestMatch(segment, cur));
89-
} else {
90-
91-
// we can't even find a partial match, no completion
92-
callback();
93-
}
94-
return;
95-
}
96-
if(i == segments.length - 1) {
97-
// exact match on last segment, no completion
98-
callback();
99-
return;
100-
}
101-
cur = cur[segment];
102-
}
103-
callback();
104-
}, 1000);
105-
});
106-
shell.activate();
107-
});
108-
function bestMatch(match, pwd) {
109-
var completions = [];
110-
var common = '';
111-
for(var segment in pwd) {
112-
if(!pwd.hasOwnProperty(segment)) {
113-
continue;
114-
}
115-
if(segment.slice(0, match.length) == match) {
116-
completions.push(segment);
117-
if(!common) {
118-
common = segment;
119-
console.log("initial common:" + common);
120-
} else if(segment.slice(0, common.length) != common) {
121-
console.log("find common stem for '" + common + "' and '" + segment + "'");
122-
var i = match.length;
123-
while(true) {
124-
if(common[i] != segment[i]) {
125-
common = common.substr(0, i);
126-
break;
127-
}
128-
i++;
129-
}
130-
}
131-
}
132-
}
133-
if(completions.length == 0) {
134-
return;
135-
}
136-
if(completions.length == 1) {
137-
completions = null;
138-
}
139-
return {
140-
result: common.substr(match.length),
141-
suggestions: completions
142-
};
143-
}
144-
</script>
14531
</head>
14632
<body>
147-
<div id="quake-console">
148-
<div id="line">jsh$ <span id="line-input"></span></div>
33+
<div id="shell-panel">
34+
<div id="shell-view"></div>
14935
</div>
150-
<div id="shell"></div>
151-
<p>&nbsp;</p>
152-
153-
<p>&nbsp;</p>
154-
155-
<p>&nbsp;</p>
156-
157-
<p>&nbsp;</p>
158-
159-
<p>&nbsp;</p>
160-
161-
<p>&nbsp;</p>
162-
163-
<p>&nbsp;</p>
164-
165-
<p>&nbsp;</p>
166-
167-
<p>&nbsp;</p>
168-
169-
<p>&nbsp;</p>
170-
171-
<p>&nbsp;</p>
172-
173-
<h2>Key event:</h2>
174-
<pre id="debug"></pre>
175-
<div id="x"></div>
176-
<div id="keydown"></div>
177-
<div id="keypress"></div>
178-
<script>
179-
// document.onkeydown = function (e) {
180-
// $('#keydown').text("keydown - code: "+ e.keyCode);
181-
// return true;
182-
// }
183-
// document.onkeypress = function(e) {
184-
// $('#keypress').text("keypress - code: "+ e.keyCode+", char: "+ String.fromCharCode(e.keyCode)+", ctrl: "+ e.ctrlKey);
185-
// return true;
186-
// }
187-
</script>
18836
</body>
189-
</html>
37+
</html>

‎js/example.js

Copy file name to clipboard
+131Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var fs = {
2+
bin: {},
3+
boot: {},
4+
dev: {},
5+
etc: {
6+
default: {},
7+
'rc.d': {},
8+
sysconfig: {},
9+
x11: {}
10+
},
11+
home: {
12+
bob: {},
13+
jane: {}
14+
},
15+
lib: {},
16+
'lost+found': {},
17+
misc: {},
18+
mnt: {
19+
cdrom: {},
20+
sysimage: {}
21+
},
22+
net: {},
23+
opt: {},
24+
proc: {},
25+
root: {},
26+
sbin: {},
27+
usr: {
28+
x11: {},
29+
bin: {},
30+
include: {},
31+
lib: {},
32+
local: {},
33+
man: {},
34+
sbin: {},
35+
share: {
36+
doc: {}
37+
},
38+
src: {}
39+
},
40+
var: {
41+
lib: {},
42+
lock: {},
43+
run: {},
44+
log: {
45+
httpd: {
46+
access_log: {},
47+
error_log: {}
48+
},
49+
'boot.log': {},
50+
cron: {},
51+
messages: {}
52+
}
53+
}
54+
};
55+
function buildTree(parent, node) {
56+
parent.childnodes = _.map(_.pairs(node), function(pair) {
57+
var child = {
58+
name: pair[0],
59+
path: parent.path + "/" + pair[0],
60+
parent: parent
61+
};
62+
buildTree(child, pair[1]);
63+
return child;
64+
});
65+
parent.children = _.keys(node);
66+
return parent;
67+
}
68+
function findNode(current, parts, callback) {
69+
if(!parts || parts.length == 0) {
70+
return callback(current);
71+
}
72+
if(parts[0] == ".") {
73+
// carry on
74+
} else if(parts[0] == "..") {
75+
current = current.parent;
76+
} else {
77+
current = _.first(_.filter(current.childnodes, function(node) {
78+
return node.name == parts[0];
79+
}));
80+
}
81+
if(!current) {
82+
return callback();
83+
}
84+
return findNode(current, _.rest(parts), callback);
85+
}
86+
var root = buildTree({
87+
name: "",
88+
path: ""
89+
},
90+
fs
91+
);
92+
root.path = '/';
93+
var history = Josh.History();
94+
var shell = Josh.Shell({history: history});
95+
var pathhandler = Josh.PathHandler();
96+
pathhandler.current = root;
97+
pathhandler.attach(shell);
98+
pathhandler.getNode = function(path, callback) {
99+
if(!path) {
100+
return callback(pathhandler.current);
101+
}
102+
var parts = _.filter(path.split('/'), function(x) { return x; });
103+
var start = ((path || '')[0] == '/') ? root : pathhandler.current;
104+
console.log('start: '+start.path+', parts: '+JSON.stringify(parts));
105+
return findNode(start, parts, callback);
106+
};
107+
pathhandler.getChildNodes = function(node, callback) {
108+
console.log("children for "+node.name);
109+
callback(node.childnodes);
110+
};
111+
112+
$(document).ready(function() {
113+
var console = $('#shell-panel');
114+
shell.onActivate(function() {
115+
showConsole();
116+
});
117+
shell.onDeactivate(function() {
118+
hideConsole();
119+
});
120+
function showConsole() {
121+
console.slideDown();
122+
console.focus();
123+
}
124+
125+
function hideConsole() {
126+
console.slideUp();
127+
console.blur();
128+
}
129+
130+
shell.activate();
131+
});

0 commit comments

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