Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inject console.log as parameter #26

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions examples/tabtab-test-complete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ const tabtab = require('../..');
const args = opts._;

const completion = env => {
const shell = tabtab.getShellFromEnv(env);

if (!env.complete) return;

if (env.prev === 'someCommand') {
return tabtab.log(['is', 'this', 'the', 'real', 'life']);
return tabtab.log(['is', 'this', 'the', 'real', 'life'], shell, console.log);
}

if (env.prev === 'anotherOne') {
return tabtab.log(['is', 'this', 'just', 'fantasy']);
return tabtab.log(['is', 'this', 'just', 'fantasy'], shell, console.log);
}

if (env.prev === '--loglevel') {
return tabtab.log(['error', 'warn', 'info', 'notice', 'verbose']);
return tabtab.log(['error', 'warn', 'info', 'notice', 'verbose'], shell, console.log);
}

return tabtab.log([
Expand All @@ -40,7 +42,7 @@ const completion = env => {
description: 'You must add a description for items with ":" in them'
},
'anotherOne'
]);
], shell, console.log);
};

const init = async () => {
Expand Down Expand Up @@ -90,13 +92,19 @@ const init = async () => {
}

if (cmd === 'install-completion') {
const shell = args[1];
if (!tabtab.isShellSupported(shell)) {
throw new Error(`${shell} is not supported`);
}

// Here we install for the program `tabtab-test` (this file), with
// completer being the same program. Sometimes, you want to complete
// another program that's where the `completer` option might come handy.
await tabtab
.install({
name: 'tabtab-test',
completer: 'tabtab-test'
completer: 'tabtab-test',
shell,
})
.catch(err => console.error('INSTALL ERROR', err));

Expand Down
11 changes: 6 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,12 @@ const completionItem = (item, shell) => {
* Bash needs in addition to filter out the args for the completion to work
* (zsh, fish don't need this).
*
* @param {Array.<CompletionItem | String>} args to log, Strings or Objects with name and
* @param {Array.<CompletionItem | String>} args - to log, Strings or Objects with name and
* description property.
* @param {SupportedShell} shell
* @param {(message: String) => void} logToConsole - Function to actually log to the console, usually `console.log`
*/
const log = (args, shell) => {
const log = (args, shell, logToConsole = console.log) => {
if (!Array.isArray(args)) {
throw new Error('log: Invalid arguments, must be an array');
}
Expand All @@ -243,9 +244,9 @@ const log = (args, shell) => {
let lines = args.map(item => completionItem(item, shell)).map(item => {
const { name: rawName, description: rawDescription } = item;

const name = shell === 'zsh' ? rawName?.replace(/:/g, '\\:') : rawName;
const name = shell === 'zsh' ? rawName?.replaceAll(':', '\\:') : rawName;
const description =
shell === 'zsh' ? rawDescription?.replace(/:/g, '\\:') : rawDescription;
shell === 'zsh' ? rawDescription?.replaceAll(':', '\\:') : rawDescription;
let str = name;

if (shell === 'zsh' && description) {
Expand All @@ -263,7 +264,7 @@ const log = (args, shell) => {
}

for (const line of lines) {
console.log(`${line}`);
logToConsole(`${line}`);
}
};

Expand Down
6 changes: 3 additions & 3 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ const getCompletionScript = async ({ name, completer, shell }) => {
const templatePath = scriptFromShell(shell);
const templateContent = await readFile(templatePath, 'utf8');
const scriptContent = templateContent
.replace(/\{pkgname\}/g, name)
.replace(/{completer}/g, completer)
.replaceAll('{pkgname}', name)
.replaceAll('{completer}', completer)
// on Bash on windows, we need to make sure to remove any \r
.replace(/\r?\n/g, '\n');
.replaceAll(/\r?\n/g, '\n');
return scriptContent
};

Expand Down
18 changes: 13 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ const opts = require('minimist')(process.argv.slice(2), {

const args = opts._;
const completion = env => {
const shell = tabtab.getShellFromEnv(env);

if (!env.complete) return;

// Write your completions there

if (env.prev === 'foo') {
return tabtab.log(['is', 'this', 'the', 'real', 'life']);
return tabtab.log(['is', 'this', 'the', 'real', 'life'], shell, console.log);
}

if (env.prev === 'bar') {
return tabtab.log(['is', 'this', 'just', 'fantasy']);
return tabtab.log(['is', 'this', 'just', 'fantasy'], shell, console.log);
}

if (env.prev === '--loglevel') {
return tabtab.log(['error', 'warn', 'info', 'notice', 'verbose']);
return tabtab.log(['error', 'warn', 'info', 'notice', 'verbose'], shell, console.log);
}

return tabtab.log([
Expand All @@ -91,7 +93,7 @@ const completion = env => {
description: 'You must add a description for items with ":" in them'
},
'anotherOne'
]);
], shell, console.log);
};

const run = async () => {
Expand All @@ -103,10 +105,16 @@ const run = async () => {
// completer being the same program. Sometimes, you want to complete
// another program that's where the `completer` option might come handy.
if (cmd === 'install-completion') {
const shell = args[1];
if (!tabtab.isShellSupported(shell)) {
throw new Error(`${shell} is not supported`);
}

await tabtab
.install({
name: 'tabtab-test',
completer: 'tabtab-test'
completer: 'tabtab-test',
shell,
})
.catch(err => console.error('INSTALL ERROR', err));

Expand Down
6 changes: 3 additions & 3 deletions test/getCompletionScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('getCompletionScript gets the right completion script for', () => {
shell
});
const expected = fs.readFileSync(require.resolve(`../lib/templates/completion.${COMPLETION_FILE_EXT[shell]}`), 'utf8')
.replace(/\{pkgname\}/g, 'foo')
.replace(/{completer}/g, 'foo-complete')
.replace(/\r?\n/g, '\n');
.replaceAll('{pkgname}', 'foo')
.replaceAll('{completer}', 'foo-complete')
.replaceAll(/\r?\n/g, '\n');
assert.equal(received, expected);
});
}
Expand Down
8 changes: 4 additions & 4 deletions test/log.js → test/logCompletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe('tabtab.log', () => {

const logTestHelper = (items, shell) => {
const logs = [];
const { log } = console;
console.log = data => logs.push(data);
tabtab.log(items, shell);
console.log = log;
const log = message => {
logs.push(message);
}
tabtab.log(items, shell, log);
return logs;
};

Expand Down
Loading