remoteCompletionCommand function

String remoteCompletionCommand(
  1. String word, {
  2. required bool isCommand,
})

Returns a POSIX sh command that prints completion candidates for word, one per line. When isCommand is true the word is in command position.

Implementation

String remoteCompletionCommand(String word, {required bool isCommand}) {
  final w = _singleQuote(word);
  // Glob `<word>*`, marking directories with a trailing slash. `"$w"*` keeps the
  // already-typed prefix literal (handles spaces/special chars) while globbing.
  const fileGlob =
      r'for p in "$w"*; do [ -e "$p" ] || continue; '
      r'if [ -d "$p" ]; then printf "%s/\n" "$p"; '
      r'else printf "%s\n" "$p"; fi; done';
  if (!isCommand) {
    return 'w=$w; $fileGlob';
  }
  // Command position: a word with a slash is a path; otherwise scan $PATH for
  // executables and print their basenames, sorted and de-duplicated.
  const pathScan =
      r'IFS=:; for d in $PATH; do [ -d "$d" ] || continue; '
      r'for p in "$d"/"$w"*; do [ -f "$p" ] && [ -x "$p" ] && '
      r'printf "%s\n" "${p##*/}"; done; done | sort -u';
  return 'w=$w; case "\$w" in */*) $fileGlob ;; *) $pathScan ;; esac';
}