Subversion Repositories HomeAutomation

Rev

Rev 1620 | Rev 1647 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1608 runge 1
 
1644 runge 2
Console_AutoCompleteFunctions = {};
3
Console_HelpTexts = {};
1609 runge 4
 
5
function Console_DoAutocomplete()
1608 runge 6
{
1609 runge 7
	var name = arguments[0];
8
 
9
	//Log("Console_DoAutocomplete(), name=" + name);
1608 runge 10
 
1644 runge 11
	var result = {};
1609 runge 12
 
1644 runge 13
	if (typeof Console_AutoCompleteFunctions[name] != 'undefined')
1609 runge 14
	{
15
		//Log("calling autocomplete_callback");
1644 runge 16
		result = Console_AutoCompleteFunctions[name](arguments);
1609 runge 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;
1608 runge 27
}
1609 runge 28
 
1644 runge 29
function Console_RegisterCommand(command, autocomplete_callback)
1609 runge 30
{
1610 runge 31
	var command_string = command.toString();
32
 
33
	var pos_end = command_string.indexOf("\n") - 1;
34
	var pos_start = command_string.indexOf(" ") + 1;
35
 
36
	var full_name = command_string.substr(pos_start, pos_end - pos_start);
37
 
38
	var pos_split = full_name.indexOf("(");
39
 
40
	var name = full_name.substr(0, pos_split);
41
 
42
	var argument_string = full_name.substr(pos_split + 1);
43
 
44
	if (argument_string.length > 0)
45
	{
46
		argument_string = argument_string.replace(/,/g, "");
47
		argument_string = "<" + argument_string.replace(/ /g, "> <") + ">";
48
	}
49
 
1644 runge 50
	if (ConsoleExport_RegisterCommand(name))
1609 runge 51
	{
1644 runge 52
		Console_HelpTexts[name] = argument_string;
1610 runge 53
 
54
 
1609 runge 55
		if (autocomplete_callback)
56
		{
1644 runge 57
			Console_AutoCompleteFunctions[name] = autocomplete_callback;
1609 runge 58
		}
59
 
60
		return true;
61
	}
62
 
63
	return false;
64
}
1610 runge 65
 
1644 runge 66
function Console_StandardAutocomplete()
1614 runge 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
	}
81
 
82
	return new Array();
83
}
84
 
1644 runge 85
 
86
 
1610 runge 87
function help_complete(args)
88
{
1644 runge 89
	var result = {};
1610 runge 90
 
91
	var arg_index = args.length - 1;
92
 
93
	if (arg_index == 1) // command name
94
	{
1644 runge 95
		for (var n in Console_HelpTexts)
1610 runge 96
		{
97
			result.push(n);
98
		}
99
	}
100
 
101
	return result;
102
}
103
 
104
function help(command_name)
105
{
106
	if (!command_name)
107
	{
108
		command_name = "help";
109
	}
110
 
1644 runge 111
	if (typeof Console_HelpTexts[command_name] != 'undefined')
1610 runge 112
	{
113
		var result = "";
114
 
1644 runge 115
		if (Console_AutoCompleteFunctions[command_name])
1610 runge 116
		{
117
			result += "Argument autocompletion enabled for " + command_name + "\n";
118
		}
119
 
1644 runge 120
		result += "Syntax: " + command_name + " " + Console_HelpTexts[command_name] + "\n";
1610 runge 121
 
122
		return result;
123
	}
124
 
125
	return "No help for " + command_name + " found\n";
126
}
1644 runge 127
Console_RegisterCommand(help, help_complete);
1620 runge 128
 
1644 runge 129
var prompt_requests = {};
1620 runge 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
}