completer 函数的使用


completer 函数将用户输入的当前行作为参数,并返回包含以下两个条目的数组:

  • 包含匹配补全输入的数组。
  • 用于匹配的子字符串。

例如:[[substr1, substr2, ...], originalsubstring]

function completer(line) {
  const completions = '.help .error .exit .quit .q'.split(' ');
  const hits = completions.filter((c) => c.startsWith(line));
  // 如果没有匹配,则显示所有补全。
  return [hits.length ? hits : completions, line];
}

如果 completer 函数接受两个参数,则可以异步地调用:

function completer(linePartial, callback) {
  callback(null, [['123'], linePartial]);
}

The completer function takes the current line entered by the user as an argument, and returns an Array with 2 entries:

  • An Array with matching entries for the completion.
  • The substring that was used for the matching.

For instance: [[substr1, substr2, ...], originalsubstring].

function completer(line) {
  const completions = '.help .error .exit .quit .q'.split(' ');
  const hits = completions.filter((c) => c.startsWith(line));
  // show all completions if none found
  return [hits.length ? hits : completions, line];
}

The completer function can be called asynchronously if it accepts two arguments:

function completer(linePartial, callback) {
  callback(null, [['123'], linePartial]);
}