Subversion Repositories HomeAutomation

Rev

Rev 1644 | Rev 1649 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1644 Rev 1647
Line 1... Line 1...
1
 
1
 
2
Console_AutoCompleteFunctions = {};
2
Console_Functions = {};
-
 
3
Console_CurrentFunction = null;
-
 
4
Console_DefaultPrompt = "\033[29;1m[atomic] \033[0m";
3
Console_HelpTexts = {};
5
Console_LastClientId = null;
4
 
6
 
5
function Console_DoAutocomplete()
7
function Console_GetFunctionNames()
6
{
8
{
-
 
9
    return get_keys(Console_Functions);
-
 
10
}
-
 
11
 
-
 
12
// C++ callable functions
-
 
13
 
-
 
14
function Console_AutocompleteRequest(client_id, arg_index, commandline)
-
 
15
{
-
 
16
    Console_LastClientId = client_id;
-
 
17
   
-
 
18
    var result = [];
-
 
19
    var parts = [];
7
    var name = arguments[0];
20
    var name = "Console_Shell";
-
 
21
   
-
 
22
    if (commandline.length > 0 && arg_index != 0)
-
 
23
    {
-
 
24
        parts = commandline.split(" ");
-
 
25
        name = parts.shift();
-
 
26
        arg_index--;
-
 
27
    }
-
 
28
 
-
 
29
    if (Console_Functions[name] && Console_Functions[name]["autocomplete"])
-
 
30
    {
-
 
31
        //Log("calling autocomplete_callback: " + name + "\n");
-
 
32
        result = Console_Functions[name]["autocomplete"](arg_index, parts);
-
 
33
    }
-
 
34
   
-
 
35
    var result_string = result.join("\n");
-
 
36
   
-
 
37
    ConsoleExport_AutoCompleteResponse(Console_LastClientId, result_string);
-
 
38
    return true;
-
 
39
}
-
 
40
 
-
 
41
function Console_PromptResponse(client_id, response)
-
 
42
{
-
 
43
    Console_LastClientId = client_id;
-
 
44
   
-
 
45
    var parts = response.split(" ");
-
 
46
 
-
 
47
    var current_function = Console_CurrentFunction;
-
 
48
    Console_CurrentFunction = Console_Shell;
-
 
49
   
-
 
50
    var result = current_function.apply(null, Array.prototype.slice.call(parts, 0));
-
 
51
   
-
 
52
    if (Console_CurrentFunction == Console_Shell)
-
 
53
    {
-
 
54
        Console_SetDefaultPrompt();
-
 
55
    }
-
 
56
   
-
 
57
    return result;
-
 
58
}
-
 
59
 
-
 
60
function Console_NewConnection(client_id)
-
 
61
{
-
 
62
    Console_LastClientId = client_id;
-
 
63
   
-
 
64
    return Console_SetDefaultPrompt();
-
 
65
}
-
 
66
 
-
 
67
// User script functions
-
 
68
 
-
 
69
function Console_PreventDefaultPrompt()
-
 
70
{
-
 
71
    Console_CurrentFunction = null;
-
 
72
}
8
 
73
 
-
 
74
function Console_SetDefaultPrompt()
-
 
75
{
9
    //Log("Console_DoAutocomplete(), name=" + name);
76
    return Console_PromptRequest(Console_DefaultPrompt, Console_Shell);
-
 
77
}
10
   
78
 
11
    var result = {};
-
 
12
   
-
 
13
    if (typeof Console_AutoCompleteFunctions[name] != 'undefined')
79
function Console_PromptRequest(prompt, callback)
14
    {
80
{
15
        //Log("calling autocomplete_callback");
81
    Console_CurrentFunction = callback;
16
        result = Console_AutoCompleteFunctions[name](arguments);
82
    return ConsoleExport_PromptRequest(Console_LastClientId, prompt);
17
    }
-
 
18
   
-
 
19
    var result_string = "";
-
 
20
   
-
 
21
    for (var n = 0; n < result.length; n++)
-
 
22
    {
-
 
23
        result_string += result[n] + "\n";
-
 
24
    }
-
 
25
   
-
 
26
    return result_string;
-
 
27
}
83
}
28
 
84
 
29
function Console_RegisterCommand(command, autocomplete_callback)
85
function Console_RegisterCommand(command, autocomplete_callback)
30
{
86
{
31
    var command_string = command.toString();
87
    var command_string = command.toString();
32
 
88
 
33
    var pos_end = command_string.indexOf("\n") - 1;
89
    var pos_end = command_string.indexOf("\n") - 1;
34
    var pos_start = command_string.indexOf(" ") + 1;
90
    var pos_start = command_string.indexOf(" ") + 1;
35
   
91
   
36
    var full_name = command_string.substr(pos_start, pos_end - pos_start);
92
    var full_name = command_string.substr(pos_start, pos_end - pos_start);
37
   
93
   
38
    var pos_split = full_name.indexOf("(");
94
    var pos_split = full_name.indexOf("(");
39
   
95
   
40
    var name = full_name.substr(0, pos_split);
96
    var name = full_name.substr(0, pos_split);
41
   
97
   
42
    var argument_string = full_name.substr(pos_split + 1);
98
    var argument_string = full_name.substr(pos_split + 1);
43
   
99
   
44
    if (argument_string.length > 0)
100
    if (argument_string.length > 0)
45
    {
101
    {
46
        argument_string = argument_string.replace(/,/g, "");
102
        argument_string = argument_string.replace(/,/g, "");
47
        argument_string = "<" + argument_string.replace(/ /g, "> <") + ">";
103
        argument_string = "<" + argument_string.replace(/ /g, "> <") + ">";
48
    }
104
    }
49
   
105
   
50
    if (ConsoleExport_RegisterCommand(name))
-
 
51
    {
-
 
52
        Console_HelpTexts[name] = argument_string;
-
 
53
       
-
 
54
       
-
 
55
        if (autocomplete_callback)
-
 
56
        {
-
 
57
            Console_AutoCompleteFunctions[name] = autocomplete_callback;
106
    Console_Functions[name] = { "command" : command, "autocomplete" : autocomplete_callback, "help" : argument_string };
58
        }
-
 
59
       
-
 
60
        return true;
107
    return true;
61
    }
-
 
62
   
-
 
63
    return false;
-
 
64
}
-
 
65
 
-
 
66
function Console_StandardAutocomplete()
-
 
67
{
-
 
68
    // First argument is the command line split
-
 
69
    var command_line_parts = arguments[0];
-
 
70
   
-
 
71
    // command_line_parts[0] == command name
-
 
72
    var arg_index = arguments[0].length - 2;
-
 
73
   
-
 
74
    // Supplied parameters to complete
-
 
75
    var args_to_complete = arguments.length - 1;
-
 
76
   
-
 
77
    if (0 <= arg_index && arg_index < args_to_complete)
-
 
78
    {
-
 
79
        return arguments[arg_index + 1];
-
 
80
    }
108
}
81
   
109
 
-
 
110
function Console_StandardAutocomplete()
-
 
111
{
-
 
112
    var arg_index = arguments[0];
-
 
113
    var args = arguments[1];
-
 
114
   
-
 
115
    if (0 <= arg_index && arg_index < arguments.length - 2)
-
 
116
    {
-
 
117
        return arguments[arg_index + 2];
-
 
118
    }
-
 
119
   
-
 
120
    return [];
-
 
121
}
-
 
122
 
-
 
123
// Default call function
-
 
124
 
-
 
125
function Console_Shell()
-
 
126
{
-
 
127
    var args = to_array(arguments);
-
 
128
    var name = args.shift();
-
 
129
   
-
 
130
    if (Console_Functions[name] && Console_Functions[name]["command"])
-
 
131
    {
-
 
132
        return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0));
-
 
133
    }
-
 
134
   
82
    return new Array();
135
    return false;
83
}
136
}
-
 
137
Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); });
84
 
138
 
-
 
139
// Help functions
85
 
140
 
86
 
-
 
87
function help_complete(args)
141
function help_complete(arg_index, args)
88
{
142
{
89
    var result = {};
143
    var result = [];
90
   
-
 
91
    var arg_index = args.length - 1;
-
 
92
   
144
   
93
    if (arg_index == 1) // command name
145
    if (arg_index == 1) // command name
94
    {
146
    {
95
        for (var n in Console_HelpTexts)
147
        for (var name in Console_Functions)
96
        {
148
        {
97
            result.push(n);
149
            result.push(name);
98
        }
150
        }
99
    }
151
    }
100
   
152
   
101
    return result;
153
    return result;
102
}
154
}
Line 106... Line 158...
106
    if (!command_name)
158
    if (!command_name)
107
    {
159
    {
108
        command_name = "help";
160
        command_name = "help";
109
    }
161
    }
110
   
162
   
111
    if (typeof Console_HelpTexts[command_name] != 'undefined')
163
    if (Console_Functions[command_name] && Console_Functions[command_name]["help"])
112
    {
164
    {
113
        var result = "";
165
        var result = "";
114
       
166
       
115
        if (Console_AutoCompleteFunctions[command_name])
167
        if (Console_Functions[command_name] && Console_Functions[command_name]["autocomplete"])
116
        {
168
        {
117
            result += "Argument autocompletion enabled for " + command_name + "\n";
169
            result += "Argument autocompletion enabled for " + command_name + "\n";
118
        }
170
        }
119
       
171
       
120
        result += "Syntax: " + command_name + " " + Console_HelpTexts[command_name] + "\n";
172
        result += "Syntax: " + command_name + " " + Console_Functions[command_name]["help"] + "\n";
121
       
173
       
122
        return result;
174
        Log(result);
123
    }
-
 
124
   
-
 
125
    return "No help for " + command_name + " found\n";
-
 
126
}
-
 
127
Console_RegisterCommand(help, help_complete);
-
 
128
 
-
 
129
var prompt_requests = {};
-
 
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;
175
        return true;
147
    }
176
    }
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
   
177
   
-
 
178
    Log("No help for " + command_name + " found");
160
    return result;
179
    return false;
161
}
180
}
-
 
181
Console_RegisterCommand(help, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); });