Rev 1614 | Rev 1644 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1614 | Rev 1620 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | function Console() { } |
2 | function Console() { } |
| 3 | 3 | ||
| 4 | Console.autocomplete_callbacks = new Array(); |
4 | Console.autocomplete_callbacks = new Array(); |
| 5 | Console.help_text = new Array(); |
5 | Console.help_text = new Array(); |
| 6 | 6 | ||
| 7 | function Console_DoAutocomplete() |
7 | function Console_DoAutocomplete() |
| 8 | { |
8 | { |
| 9 | var name = arguments[0]; |
9 | var name = arguments[0]; |
| 10 | 10 | ||
| 11 | //Log("Console_DoAutocomplete(), name=" + name); |
11 | //Log("Console_DoAutocomplete(), name=" + name); |
| 12 | 12 | ||
| 13 | var result = new Array(); |
13 | var result = new Array(); |
| 14 | 14 | ||
| 15 | if (typeof Console.autocomplete_callbacks[name] != 'undefined') |
15 | if (typeof Console.autocomplete_callbacks[name] != 'undefined') |
| 16 | { |
16 | { |
| 17 | //Log("calling autocomplete_callback"); |
17 | //Log("calling autocomplete_callback"); |
| 18 | result = Console.autocomplete_callbacks[name](arguments); |
18 | result = Console.autocomplete_callbacks[name](arguments); |
| 19 | } |
19 | } |
| 20 | 20 | ||
| 21 | var result_string = ""; |
21 | var result_string = ""; |
| 22 | 22 | ||
| 23 | for (var n = 0; n < result.length; n++) |
23 | for (var n = 0; n < result.length; n++) |
| 24 | { |
24 | { |
| 25 | result_string += result[n] + "\n"; |
25 | result_string += result[n] + "\n"; |
| 26 | } |
26 | } |
| 27 | 27 | ||
| 28 | return result_string; |
28 | return result_string; |
| 29 | } |
29 | } |
| 30 | 30 | ||
| 31 | function RegisterConsoleCommand(command, autocomplete_callback) |
31 | function RegisterConsoleCommand(command, autocomplete_callback) |
| 32 | { |
32 | { |
| 33 | var command_string = command.toString(); |
33 | var command_string = command.toString(); |
| 34 | 34 | ||
| 35 | var pos_end = command_string.indexOf("\n") - 1; |
35 | var pos_end = command_string.indexOf("\n") - 1; |
| 36 | var pos_start = command_string.indexOf(" ") + 1; |
36 | var pos_start = command_string.indexOf(" ") + 1; |
| 37 | 37 | ||
| 38 | var full_name = command_string.substr(pos_start, pos_end - pos_start); |
38 | var full_name = command_string.substr(pos_start, pos_end - pos_start); |
| 39 | 39 | ||
| 40 | var pos_split = full_name.indexOf("("); |
40 | var pos_split = full_name.indexOf("("); |
| 41 | 41 | ||
| 42 | var name = full_name.substr(0, pos_split); |
42 | var name = full_name.substr(0, pos_split); |
| 43 | 43 | ||
| 44 | var argument_string = full_name.substr(pos_split + 1); |
44 | var argument_string = full_name.substr(pos_split + 1); |
| 45 | 45 | ||
| 46 | if (argument_string.length > 0) |
46 | if (argument_string.length > 0) |
| 47 | { |
47 | { |
| 48 | argument_string = argument_string.replace(/,/g, ""); |
48 | argument_string = argument_string.replace(/,/g, ""); |
| 49 | argument_string = "<" + argument_string.replace(/ /g, "> <") + ">"; |
49 | argument_string = "<" + argument_string.replace(/ /g, "> <") + ">"; |
| 50 | } |
50 | } |
| 51 | 51 | ||
| 52 | if (Console_RegisterConsoleCommand(name)) |
52 | if (Console_RegisterConsoleCommand(name)) |
| 53 | { |
53 | { |
| 54 | Console.help_text[name] = argument_string; |
54 | Console.help_text[name] = argument_string; |
| 55 | 55 | ||
| 56 | 56 | ||
| 57 | if (autocomplete_callback) |
57 | if (autocomplete_callback) |
| 58 | { |
58 | { |
| 59 | Console.autocomplete_callbacks[name] = autocomplete_callback; |
59 | Console.autocomplete_callbacks[name] = autocomplete_callback; |
| 60 | } |
60 | } |
| 61 | 61 | ||
| 62 | return true; |
62 | return true; |
| 63 | } |
63 | } |
| 64 | 64 | ||
| 65 | return false; |
65 | return false; |
| 66 | } |
66 | } |
| 67 | 67 | ||
| 68 | function StandardAutocomplete() |
68 | function StandardAutocomplete() |
| 69 | { |
69 | { |
| 70 | // First argument is the command line split |
70 | // First argument is the command line split |
| 71 | var command_line_parts = arguments[0]; |
71 | var command_line_parts = arguments[0]; |
| 72 | 72 | ||
| 73 | // command_line_parts[0] == command name |
73 | // command_line_parts[0] == command name |
| 74 | var arg_index = arguments[0].length - 2; |
74 | var arg_index = arguments[0].length - 2; |
| 75 | 75 | ||
| 76 | // Supplied parameters to complete |
76 | // Supplied parameters to complete |
| 77 | var args_to_complete = arguments.length - 1; |
77 | var args_to_complete = arguments.length - 1; |
| 78 | 78 | ||
| 79 | if (0 <= arg_index && arg_index < args_to_complete) |
79 | if (0 <= arg_index && arg_index < args_to_complete) |
| 80 | { |
80 | { |
| 81 | return arguments[arg_index + 1]; |
81 | return arguments[arg_index + 1]; |
| 82 | } |
82 | } |
| 83 | 83 | ||
| 84 | return new Array(); |
84 | return new Array(); |
| 85 | } |
85 | } |
| 86 | 86 | ||
| 87 | function help_complete(args) |
87 | function help_complete(args) |
| 88 | { |
88 | { |
| 89 | var result = new Array(); |
89 | var result = new Array(); |
| 90 | 90 | ||
| 91 | var arg_index = args.length - 1; |
91 | var arg_index = args.length - 1; |
| 92 | 92 | ||
| 93 | if (arg_index == 1) // command name |
93 | if (arg_index == 1) // command name |
| 94 | { |
94 | { |
| 95 | for (var n in Console.help_text) |
95 | for (var n in Console.help_text) |
| 96 | { |
96 | { |
| 97 | result.push(n); |
97 | result.push(n); |
| 98 | } |
98 | } |
| 99 | } |
99 | } |
| 100 | 100 | ||
| 101 | return result; |
101 | return result; |
| 102 | } |
102 | } |
| 103 | 103 | ||
| 104 | function help(command_name) |
104 | function help(command_name) |
| 105 | { |
105 | { |
| 106 | if (!command_name) |
106 | if (!command_name) |
| 107 | { |
107 | { |
| 108 | command_name = "help"; |
108 | command_name = "help"; |
| 109 | } |
109 | } |
| 110 | 110 | ||
| 111 | if (typeof Console.help_text[command_name] != 'undefined') |
111 | if (typeof Console.help_text[command_name] != 'undefined') |
| 112 | { |
112 | { |
| 113 | var result = ""; |
113 | var result = ""; |
| 114 | 114 | ||
| 115 | if (Console.autocomplete_callbacks[command_name]) |
115 | if (Console.autocomplete_callbacks[command_name]) |
| 116 | { |
116 | { |
| 117 | result += "Argument autocompletion enabled for " + command_name + "\n"; |
117 | result += "Argument autocompletion enabled for " + command_name + "\n"; |
| 118 | } |
118 | } |
| 119 | 119 | ||
| 120 | result += "Syntax: " + command_name + " " + Console.help_text[command_name] + "\n"; |
120 | result += "Syntax: " + command_name + " " + Console.help_text[command_name] + "\n"; |
| 121 | 121 | ||
| 122 | return result; |
122 | return result; |
| 123 | } |
123 | } |
| 124 | 124 | ||
| 125 | return "No help for " + command_name + " found\n"; |
125 | return "No help for " + command_name + " found\n"; |
| 126 | } |
126 | } |
| 127 | RegisterConsoleCommand(help, help_complete); |
127 | RegisterConsoleCommand(help, help_complete); |
| - | 128 | ||
| - | 129 | var prompt_requests = new Array(); |
|
| - | 130 | var prompt_request_counter = 0; |
|
| - | 131 | ||
| - | 132 | function prompt(question, callback, text) |
|
| - | 133 | { |
|
| - | 134 | var id = prompt_request_counter++; |
|
| - | 135 | prompt_requests[id] = callback; |
|
| - | 136 | ||
| - | 137 | var prompt = ""; |
|
| - | 138 | ||
| - | 139 | if (text) |
|
| - | 140 | { |
|
| - | 141 | if (text[text.length - 1] != '\n') |
|
| - | 142 | { |
|
| - | 143 | text += "\n"; |
|
| - | 144 | } |
|
| - | 145 | ||
| - | 146 | prompt += text; |
|
| - | 147 | } |
|
| - | 148 | ||
| - | 149 | prompt += "P;" + id + ";" + question + "\n"; |
|
| - | 150 | ||
| - | 151 | return prompt; |
|
| - | 152 | } |
|
| - | 153 | ||
| - | 154 | function Console_PromptResponse(id, response) |
|
| - | 155 | { |
|
| - | 156 | var result = prompt_requests[id](response); |
|
| - | 157 | ||
| - | 158 | delete prompt_requests[id]; |
|
| - | 159 | ||
| - | 160 | return result; |
|
| - | 161 | } |
|
| 128 | 162 | ||