From ComputerCraft Wiki
Jump to: navigation, search

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


Grid paper.png  Example
Registers a function which handles tab completion behaviour for a hypothetical "would" script.
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] .. " "]
			return {}

	-- 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)
	return results

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).