Difference between revisions of "Shell.setCompletionFunction"

From ComputerCraft Wiki
Jump to: navigation, search
(Created page with "{{lowercase}} {{Function |name=shell.setCompletionFunction |args= {{Type|string}} path, {{Type|function}} completion function |api=shell |addon=ComputerCraft |desc=Added by Co...")
 
m
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
 
|desc=Added by ComputerCraft 1.74, shell.setCompletionFunction() associates a "completion function" with a given path. When a user enters that path into the [[shell]]'s command line, the assigned function will be used to provide suggestions for tab completion (which can be based on the number of parameters entered so far, and what the previous ones were). For example, if the user enters "pastebin ", a suitable tab completion function might offer "put" as an option for the first parameter, changing to "get" if a "g" is entered.
 
|desc=Added by ComputerCraft 1.74, shell.setCompletionFunction() associates a "completion function" with a given path. When a user enters that path into the [[shell]]'s command line, the assigned function will be used to provide suggestions for tab completion (which can be based on the number of parameters entered so far, and what the previous ones were). For example, if the user enters "pastebin ", a suitable tab completion function might offer "put" as an option for the first parameter, changing to "get" if a "g" is entered.
  
The "completion function", when actually utilised (generally via a call to [[shell.complete]]()), is passed four parameters - the shell table it is to be used with, the number of parameters entered (1, while user is typing the first; then 2, while the user is typing the second, and so on), the content of the current parameter being typed, and a numerically indexed table containing all the previous parameters typed (1 being the command name, 2 being the first parameter, and so on).
+
The "completion function", when actually utilised (generally via a call to [[shell.complete]]()), is passed four parameters - the shell table it is to be used with, the number of parameters entered (1, while user is typing the first; then 2, while the user is typing the second, and so on), the content of the current parameter being typed, and a numerically indexed table containing all the previous parameters typed (1 being the command name, 2 being the first parameter, and so on). For example, if the user enters "pastebin p", the function would be passed (shell, 1, "p", {"pastebin"}).
  
 
In turn, the function is expected to return a numerically indexed table containing all the possible strings that could be used to complete the current parameter (or an empty table, if none are known). For example, if the user enters "pastebin p", the expected result is {"ut "}.
 
In turn, the function is expected to return a numerically indexed table containing all the possible strings that could be used to complete the current parameter (or an empty table, if none are known). For example, if the user enters "pastebin p", the expected result is {"ut "}.
Line 14: Line 14:
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Registers a function which handles tab completion behaviour for a hypothetical "would" script in a directory titled "folder".
+
|desc=Registers a function which handles tab completion behaviour for a hypothetical "would" script.
 
|code=
 
|code=
 
  local parameters = { ["you "] = { ["please "] = { ["listen"] = {}, ["look "] = { ["up"] = {}, ["down"] = {} },
 
  local parameters = { ["you "] = { ["please "] = { ["listen"] = {}, ["look "] = { ["up"] = {}, ["down"] = {} },
Line 41: Line 41:
 
   
 
   
 
  shell.setCompletionFunction("would", tabCompletionFunction)
 
  shell.setCompletionFunction("would", tabCompletionFunction)
|output=If the user enters "would" into the CraftOS shell after running this code, assuming that file exists, various silly sentences will be suggested as they type further (use up/down to cycle through the suggestions).
+
|output=If the user enters "would" into the CraftOS shell after running this code, ''assuming that file exists,'' various silly sentences will be suggested as they type further (use up/down to cycle through the suggestions).
 
}}
 
}}
 
}}
 
}}
  
 
[[Category:API_Functions]]
 
[[Category:API_Functions]]

Latest revision as of 12:14, 29 June 2015


Grid Redstone.png  Function shell.setCompletionFunction
Added by ComputerCraft 1.74, shell.setCompletionFunction() associates a "completion function" with a given path. When a user enters that path into the shell's command line, the assigned function will be used to provide suggestions for tab completion (which can be based on the number of parameters entered so far, and what the previous ones were). For example, if the user enters "pastebin ", a suitable tab completion function might offer "put" as an option for the first parameter, changing to "get" if a "g" is entered.

The "completion function", when actually utilised (generally via a call to shell.complete()), is passed four parameters - the shell table it is to be used with, the number of parameters entered (1, while user is typing the first; then 2, while the user is typing the second, and so on), the content of the current parameter being typed, and a numerically indexed table containing all the previous parameters typed (1 being the command name, 2 being the first parameter, and so on). For example, if the user enters "pastebin p", the function would be passed (shell, 1, "p", {"pastebin"}).

In turn, the function is expected to return a numerically indexed table containing all the possible strings that could be used to complete the current parameter (or an empty table, if none are known). For example, if the user enters "pastebin p", the expected result is {"ut "}.

A list of registered paths (together with their functions) can be obtained via shell.getCompletionInfo(). Other functions that may be useful in creating a "tab completion" function suitable for shell registration are fs.complete(), textutils.complete(), shell.completeProgram() and help.completeTopic().
Syntax shell.setCompletionFunction(string path, function completion function)
Returns nil
Part of ComputerCraft
API shell

Examples

Grid paper.png  Example
Registers a function which handles tab completion behaviour for a hypothetical "would" script.
Code
local parameters = { ["you "] = { ["please "] = { ["listen"] = {}, ["look "] = { ["up"] = {}, ["down"] = {} },
 	["eat"] = {} }, ["give "] = { ["up"] = {}, ["blood"] = {}, }, ["kindly"] = {} } }

local function tabCompletionFunction(shell, parNumber, curText, lastText)
	-- Check that the parameters entered so far are valid:
	local curParam = parameters
	for i = 2, #lastText do
		if curParam[lastText[i] .. " "] then
			curParam = curParam[lastText[i] .. " "]
		else
			return {}
		end
	end

	-- Check for suitable words for the current parameter:
	local results = {}
	for word, _ in pairs(curParam) do
		if word:sub(1, #curText) == curText then
			results[#results + 1] = word:sub(#curText + 1)
		end
	end
	return results
end

shell.setCompletionFunction("would", tabCompletionFunction)
Output If the user enters "would" into the CraftOS shell after running this code, assuming that file exists, various silly sentences will be suggested as they type further (use up/down to cycle through the suggestions).