~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to console/console.js

  • Committer: drtomc
  • Date: 2008-01-02 04:45:20 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:126
A basic version of the console going!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
var magic;
 
1
var magic = 'xyzzy';
 
2
 
 
3
function historyUp()
 
4
{
 
5
    if (this.cursor >= 0)
 
6
    {
 
7
        this.cursor--;
 
8
    }
 
9
}
 
10
 
 
11
function historyDown()
 
12
{
 
13
    if (this.cursor < this.items.length)
 
14
    {
 
15
        this.cursor++;
 
16
    }
 
17
}
 
18
 
 
19
function historyCurr()
 
20
{
 
21
    if (this.cursor < 0 || this.cursor >= this.items.length)
 
22
    {
 
23
        return "";
 
24
    }
 
25
    return this.items[this.cursor];
 
26
}
 
27
 
 
28
function historyAdd(text)
 
29
{
 
30
    this.items[this.items.length] = text;
 
31
    this.cursor = this.items.length
 
32
}
 
33
 
 
34
function historyShow()
 
35
{
 
36
    var res = "";
 
37
    if (this.cursor == -1)
 
38
    {
 
39
        res += "[]";
 
40
    }
 
41
    for (var i = 0; i < this.items.length; i++)
 
42
    {
 
43
        if (i == this.cursor)
 
44
        {
 
45
            res += "["
 
46
        }
 
47
        res += this.items[i].toString();
 
48
        if (i == this.cursor)
 
49
        {
 
50
            res += "]"
 
51
        }
 
52
        res += " "
 
53
    }
 
54
    if (this.cursor == this.items.length)
 
55
    {
 
56
        res += "[]";
 
57
    }
 
58
    return res;
 
59
}
 
60
 
 
61
function History()
 
62
{
 
63
    this.items = new Array();
 
64
    this.cursor = -1;
 
65
    this.up = historyUp;
 
66
    this.down = historyDown;
 
67
    this.curr = historyCurr;
 
68
    this.add = historyAdd;
 
69
    this.show = historyShow;
 
70
}
 
71
 
 
72
var hist = new History();
2
73
 
3
74
function make_query_string(pagename, args)
4
75
{
5
76
    var first = true;
6
77
    var qs = pagename;
7
 
    for (key in obj)
 
78
    for (key in args)
8
79
    {
9
 
        vals = obj[key];
 
80
        vals = args[key];
10
81
        // vals can be an array, to make multiple args with the same name
11
82
        // To handle this, make non-array objects into an array, then loop
12
83
        if (!(vals instanceof Array))
13
84
            vals = [vals];
14
85
        for each (val in vals)
15
86
        {
16
 
            if (true)
 
87
            if (first)
17
88
            {
18
89
                qs += "?";
19
90
                first = false;
28
99
    return qs;
29
100
}
30
101
 
31
 
function enter_line(inp)
32
 
{
33
 
    var digest = hex_md5(inp + magic);
34
 
    var url = make_query_string("chat", {"digest":digest, "text":inp});
35
 
    var xmlhttp = XMLHttpRequest();
36
 
    xmlhttp.open("POST", url, false);
37
 
    xmlhttp.send("")
38
 
    var res = JSON.parse(xmlhttp..responseText);
 
102
function make_post_body(args)
 
103
{
 
104
    var first = true;
 
105
    var qs = '';
 
106
    for (key in args)
 
107
    {
 
108
        vals = args[key];
 
109
        // vals can be an array, to make multiple args with the same name
 
110
        // To handle this, make non-array objects into an array, then loop
 
111
        if (!(vals instanceof Array))
 
112
            vals = [vals];
 
113
        for each (val in vals)
 
114
        {
 
115
            if (first)
 
116
            {
 
117
                first = false;
 
118
            }
 
119
            else
 
120
            {
 
121
                qs += "&";
 
122
            }
 
123
            qs += encodeURI(key) + "=" + encodeURI(val);
 
124
        }
 
125
    }
 
126
    return qs;
 
127
}
 
128
 
 
129
function enter_line()
 
130
{
 
131
    var inp = document.getElementById('inputText');
 
132
    var digest = hex_md5(inp.value + magic);
 
133
    var xmlhttp = new XMLHttpRequest();
 
134
    xmlhttp.open("POST", "chat", false);
 
135
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 
136
    xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
 
137
    var res = JSON.parse(xmlhttp.responseText);
 
138
    var output = document.getElementById("outputArea")
 
139
    output.appendChild(document.createTextNode(inp.value + "\n"));
39
140
    if (res && res[0])
40
141
    {
41
142
        // Success!
42
143
        // print out the output (res[0])
 
144
        output.appendChild(document.createTextNode(res[0]));
43
145
        // print out the return value (res[1])
 
146
        if (res[1])
 
147
        {
 
148
            output.appendChild(document.createTextNode(res[1] + "\n"));
 
149
        }
44
150
        // set the prompt to >>>
 
151
        var prompt = document.getElementById("prompt");
 
152
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
45
153
    }
46
154
    else if (res)
47
155
    {
48
156
        // Failure!
49
157
        // print out the error message (res[2])
 
158
        output.appendChild(document.createTextNode(res[2]));
50
159
    }
51
160
    else
52
161
    {
53
162
        // Need more input, so set the prompt to ...
54
 
    }
 
163
        var prompt = document.getElementById("prompt");
 
164
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
 
165
    }
 
166
}
 
167
 
 
168
function catch_input(key)
 
169
{
 
170
    var inp = document.getElementById('inputText');
 
171
    if (key == 13)
 
172
    {
 
173
        enter_line();
 
174
        hist.add(inp.value);
 
175
    }
 
176
    if (key == 38)
 
177
    {
 
178
        hist.up();
 
179
    }
 
180
    if (key == 40)
 
181
    {
 
182
        hist.down();
 
183
    }
 
184
    inp.value = hist.curr();
55
185
}