Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1x 1x 1x 8x 8x | import _every from "lodash/every"; import { KeyPress, State } from "makitso-prompt"; import { debug } from "../debug"; function allMatch(suggests: string[], match: string) { return _every(suggests, choice => choice.startsWith(match)); } function findCommonPrefix(suggests: string[]) { const letters = suggests[0].split(""); debug({ suggests: suggests, letters }); let prefix = letters.shift(); if (!prefix) { return ""; } while (letters.length && allMatch(suggests, prefix)) { prefix += letters.shift(); debug({ prefix }); } return prefix.slice(0, -1); } function completedCommandPortion(command: string) { const words = command.split(" "); words.pop(); return words.length ? `${words.join(" ")} ` : ``; } export function Autocomplete({ commandInfo }: { commandInfo: (state: State) => Promise<void>; }) { return async function keyPress(state: State, press: KeyPress) { const commandLine = state.commandLine(); if (state.mode === "command" && commandLine) { let { command } = commandLine; let { info } = state.stash; if (press.key.name === "tab" && info.suggests && info.suggests.length) { // set last word of commandline to the prefix common to all suggests command = completedCommandPortion(command); let text; if (info.suggests.length === 1) { text = `${command}${info.suggests[0]} `; } else { const prefix = findCommonPrefix(info.suggests); text = `${command}${prefix}`; } // debug({ help2: help }); state.command = text; await commandInfo(state); } ({ info } = state.stash); if (info.suggests && info.suggests.length) { state.footer = info.suggests.join(" "); } else { state.footer = ""; } } return state; }; } |