-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHelpers.ts
224 lines (194 loc) · 5.58 KB
/
WebHelpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
///<reference path='jquery.d.ts' />
///<reference path='jqconsole.d.ts' />
///<reference path='Utils.ts' />
///<reference path='ErrorFactory.ts' />
interface JQuery
{
jqconsole(header : string, ps : string, ps2 : string) : JQConsole;
}
module Common{
/**
* Module for emulating the console on the Web
*/
export module MyHtmlConsole{
var buffer : string = "";
var history : string[] = [];
var cur_history_num : number = 0;
var primary : bool = true;
var prompt_callback : (input : string) => void = null;
var console_elem : JQuery;
var console_text : string = "|";
var cursor_pos : number = 0;
var ps : string = "> ";
export function initialize()
{
$(document).bind("keypress", onKeyPress);
$(document).bind("keydown", onKeyDown);
console_elem = $("#console");
}
/*public getEnumerator() : Enumerator
{
var lines = [], cur_pos = -1;
return new Enumerator(() => {
if(cur_pos >= lines.length) cur_pos = -1;
if(cur_pos < 0 && this.buffer.search(/\n$/) != -1){
cur_pos = 0;
lines = this.buffer.split(/\n/);
lines.pop();
this.buffer = "";
return lines[cur_pos++];
}else if(cur_pos != -1 && cur_pos < lines.length){
return lines[cur_pos++];
}else{
this.primary = true;
return undefined;
}
});
}*/
export function printPS()
{
if(primary)
print(ps);
}
export function reset()
{
//primary = true;
}
function update()
{
print(buffer);
}
export function print(str : string)
{
console_text = console_text.substring(0, cursor_pos) + str;
cursor_pos += str.length;
console_text += "|";
$(console_elem).text(console_text);
}
export function println(text : string)
{
console_text = console_text.substr(0, cursor_pos) + text + "\n";
cursor_pos = console_text.length;
console_text += "|";
$(console_elem).text(console_text);
}
export function clearCurrentLine()
{
var tmp_cursor_pos = cursor_pos;
while(true){
if(console_text.substring(tmp_cursor_pos - ps.length, tmp_cursor_pos) === ps) break;
--tmp_cursor_pos;
}
console_text = console_text.substring(0, tmp_cursor_pos) + "|";
cursor_pos = tmp_cursor_pos;
$(console_elem).text(console_text);
}
export function setPromptCallback(callback)
{
prompt_callback = callback;
}
function onKeyPress(e : KeyboardEvent)
{
switch(e.keyCode){
case 13: //The return key
primary = false;
buffer += "\n";
print("\n");
prompt_callback(buffer);
if(!e.shiftKey){
history.push(buffer.substring(0, buffer.length - 1));
cur_history_num = history.length;
}
break;
default:
var chara = String.fromCharCode(e.keyCode);
buffer = buffer.concat(chara);
print(buffer.substr(-1));
}
}
function onKeyDown(e : KeyboardEvent)
{
switch(e.keyCode){
case 8: //The Backspace key
buffer = buffer.substr(0, buffer.length - 1);
clearCurrentLine();
print(buffer);
break;
case 38: //The up arrow key
e.preventDefault();
--cur_history_num;
if(cur_history_num < 0) cur_history_num = history.length - 1;
buffer = history[cur_history_num];
clearCurrentLine();
update();
break;
case 40: //The down arrow key
e.preventDefault();
++cur_history_num;
if(cur_history_num >= history.length) cur_history_num = 0;
buffer = history[cur_history_num];
clearCurrentLine();
update();
break;
}
}
}
export module HtmlConsole{
var console : any = null;
var use_my_console = false;
export function initialize(_use_my_console : bool, header_msg : string)
{
use_my_console = _use_my_console;
if(!use_my_console){
console = $("#console").jqconsole(header_msg, ">>> ", "... ");
console.RegisterMatching('(', ')', "paren");
}else{
console = MyHtmlConsole;
console.println(header_msg);
console.printPS();
}
}
export function print(text : string, cls? : string) : void
{
if(!use_my_console)
console.Write(text, cls);
else
console.print(text);
}
export function println(text : string, cls? : string) : void
{
if(!use_my_console)
console.Write(text + "\n", cls);
else
console.println(text);
}
export function printFormatted(format : string, values? : Object, cls? : string) : void
{
var formatted = Utils.substituteTemplate(format, values);
println(formatted, cls);
}
export function prompt(callback : (input) => void, continue_callback? : (input) => any) : void
{
if(use_my_console){
console.printPS();
console.setPromptCallback(callback);
}else{
console.Prompt(true, callback, continue_callback);
}
}
export function abortPrompt() : void
{
if(use_my_console)
throw ErrorFactory.makeError("Not implemented!");
else
console.AbortPrompt();
}
export function input(input_callback : (input) => void) : void
{
if(use_my_console)
throw ErrorFactory.makeError("Not implemented!");
else
console.Input(input_callback);
}
}
}