Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. Console_AutoCompleteFunctions = {};
  3. Console_HelpTexts = {};
  4.  
  5. function Console_DoAutocomplete()
  6. {
  7.     var name = arguments[0];
  8.  
  9.     //Log("Console_DoAutocomplete(), name=" + name);
  10.    
  11.     var result = {};
  12.    
  13.     if (typeof Console_AutoCompleteFunctions[name] != 'undefined')
  14.     {
  15.         //Log("calling autocomplete_callback");
  16.         result = Console_AutoCompleteFunctions[name](arguments);
  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. }
  28.  
  29. function Console_RegisterCommand(command, autocomplete_callback)
  30. {
  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.    
  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;
  58.         }
  59.        
  60.         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.     }
  81.    
  82.     return new Array();
  83. }
  84.  
  85.  
  86.  
  87. function help_complete(args)
  88. {
  89.     var result = {};
  90.    
  91.     var arg_index = args.length - 1;
  92.    
  93.     if (arg_index == 1) // command name
  94.     {
  95.         for (var n in Console_HelpTexts)
  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.    
  111.     if (typeof Console_HelpTexts[command_name] != 'undefined')
  112.     {
  113.         var result = "";
  114.        
  115.         if (Console_AutoCompleteFunctions[command_name])
  116.         {
  117.             result += "Argument autocompletion enabled for " + command_name + "\n";
  118.         }
  119.        
  120.         result += "Syntax: " + command_name + " " + Console_HelpTexts[command_name] + "\n";
  121.        
  122.         return 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;
  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. }
  162.