Enhance tab completion.

pull/38/head
Yipeng Liu 2 years ago
parent 1f2e543bdb
commit a9ca4ab885
No known key found for this signature in database
GPG Key ID: 836A2F6C6D60065C

@ -34,7 +34,7 @@ export const Input = ({
if (event.key === 'Tab') { if (event.key === 'Tab') {
event.preventDefault(); event.preventDefault();
handleTabCompletion(command, setCommand); handleTabCompletion(command, setHistory, setCommand);
} }
if (event.key === 'Enter' || event.code === '13') { if (event.key === 'Enter' || event.code === '13') {

@ -2,13 +2,22 @@ import * as bin from './bin';
export const handleTabCompletion = ( export const handleTabCompletion = (
command: string, command: string,
setHistory: (value: string) => void,
setCommand: React.Dispatch<React.SetStateAction<string>>, setCommand: React.Dispatch<React.SetStateAction<string>>,
) => { ) => {
const commands = Object.keys(bin).filter((entry) => const commands = ['clear', ...Object.keys(bin)].filter((entry) =>
entry.startsWith(command), entry.startsWith(command),
); );
if (commands.length === 1) { if (commands.length >= 1) {
setCommand(commands[0]); const prefix = commands.reduce((prev, curr) => {
let i = 0;
while (i < prev.length && i < curr.length && prev[i] === curr[i])
++i;
return prev.slice(0, i);
}, commands[0]);
setCommand(prefix);
if (commands.length > 1)
setHistory(commands.join(' '));
} }
}; };

Loading…
Cancel
Save