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

Add option to select default choices #42

Merged
merged 6 commits into from
Apr 5, 2018
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
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,23 @@ Usage:
ipt [options] [<path>]

Options:
-0, --null Uses a null character as separator [boolean]
-a, --autocomplete Starts in autocomplete mode [boolean]
-c, --copy Copy selected item(s) to clipboard [boolean]
-d, --debug Prints to stderr any internal error [boolean]
-e, --file-encoding Encoding for file <path>, defaults to utf8 [string]
-h, --help Shows this help message [boolean]
-m, --multiple Allows the selection of multiple items [boolean]
-M, --message Replaces interface message [string]
-p, --extract-path Returns only a valid path for each item [boolean]
-s, --separator Separator to to split input into items [string]
-S, --size Amount of lines to display at once [number]
-t, --no-trim Prevents trimming of the result strings [boolean]
-u, --unquoted Force the output to be unquoted [boolean]
-v, --version Show version number [boolean]
-0, --null Uses a null character as separator [boolean]
-a, --autocomplete Starts in autocomplete mode [boolean]
-c, --copy Copy selected item(s) to clipboard [boolean]
-d, --debug Prints to stderr any internal error [boolean]
-D, --default Select a default choices by their name [string]
-P, --default-separator Separator to to split default choices into items,
defaults to the separator [string]
-e, --file-encoding Encoding for file <path>, defaults to utf8 [string]
-h, --help Shows this help message [boolean]
-m, --multiple Allows the selection of multiple items [boolean]
-M, --message Replaces interface message [string]
-p, --extract-path Returns only a valid path for each item [boolean]
-s, --separator Separator to to split input into items [string]
-S, --size Amount of lines to display at once [number]
-t, --no-trim Prevents trimming of the result strings [boolean]
-u, --unquoted Force the output to be unquoted [boolean]
-v, --version Show version number [boolean]

```

Expand Down
9 changes: 8 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ const { argv } = yargs
.describe("c", "Copy selected item(s) to clipboard")
.alias("d", "debug")
.describe("d", "Prints to stderr any internal error")
.alias("D", "default")
.describe("D", "Select a default choices by their name")
.alias("P", "default-separator")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know whether it makes sense to have an alias for default-separator.

.describe(
"P",
"Separator to to split default choices into items, defaults to the separator"
)
.alias("e", "file-encoding")
.describe("e", "Encoding for file <path>, defaults to utf8")
.help("h")
Expand All @@ -54,7 +61,7 @@ const { argv } = yargs
.describe("u", "Force the output to be unquoted")
.alias("v", "version")
.boolean(["a", "c", "d", "h", "m", "0", "t", "p", "u", "v"])
.string(["e", "M", "s"])
.string(["e", "M", "s", "D", "P"])
.number(["S"])
.epilog("Visit https://github.com/ruyadorno/ipt for more info");

Expand Down
26 changes: 22 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";

const os = require("os");
const cliWidth = require("cli-width");
const inquirer = require("inquirer");
const fuzzysearch = require("fuzzysearch");
Expand Down Expand Up @@ -28,6 +29,18 @@ function iPipeTo(
return str.length > maxWidth ? str.substr(0, maxWidth) + "..." : str;
}

function getDefaultChoices(promptType) {
if (promptType.type === "list") {
return options.default;
}

if (promptType.type === "checkbox") {
return options.default.split(
options["default-separator"] || options.separator || os.EOL
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This falls back to separator, otherwise os.EOL, similar to this line.

);
}
}

const prompt = inquirer.createPromptModule({
input: stdin,
output: stdout
Expand Down Expand Up @@ -86,11 +99,16 @@ function iPipeTo(
}
};

const result = prompt(
const promptType =
(options.multiple && promptTypes.multiple) ||
(options.autocomplete && promptTypes.autocomplete) ||
promptTypes.base
);
(options.autocomplete && promptTypes.autocomplete) ||
promptTypes.base;

if (options.default) {
promptType.default = getDefaultChoices(promptType);
}

const result = prompt(promptType);

if (__prompt) {
__prompt.ui = result.ui;
Expand Down
31 changes: 17 additions & 14 deletions test/fixtures/help
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ Usage:
ipt [options] [<path>]

Options:
-0, --null Uses a null character as separator [boolean]
-a, --autocomplete Starts in autocomplete mode [boolean]
-c, --copy Copy selected item(s) to clipboard [boolean]
-d, --debug Prints to stderr any internal error [boolean]
-e, --file-encoding Encoding for file <path>, defaults to utf8 [string]
-h, --help Shows this help message [boolean]
-m, --multiple Allows the selection of multiple items [boolean]
-M, --message Replaces interface message [string]
-p, --extract-path Returns only a valid path for each item [boolean]
-s, --separator Separator to to split input into items [string]
-S, --size Amount of lines to display at once [number]
-t, --no-trim Prevents trimming of the result strings [boolean]
-u, --unquoted Force the output to be unquoted [boolean]
-v, --version Show version number [boolean]
-0, --null Uses a null character as separator [boolean]
-a, --autocomplete Starts in autocomplete mode [boolean]
-c, --copy Copy selected item(s) to clipboard [boolean]
-d, --debug Prints to stderr any internal error [boolean]
-D, --default Select a default choices by their name [string]
-P, --default-separator Separator to to split default choices into items,
defaults to the separator [string]
-e, --file-encoding Encoding for file <path>, defaults to utf8 [string]
-h, --help Shows this help message [boolean]
-m, --multiple Allows the selection of multiple items [boolean]
-M, --message Replaces interface message [string]
-p, --extract-path Returns only a valid path for each item [boolean]
-s, --separator Separator to to split input into items [string]
-S, --size Amount of lines to display at once [number]
-t, --no-trim Prevents trimming of the result strings [boolean]
-u, --unquoted Force the output to be unquoted [boolean]
-v, --version Show version number [boolean]

Examples:
ls | ipt Builds an interactive interface out of current dir items
Expand Down
65 changes: 65 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,69 @@ if (!process.env.APPVEYOR) {
output: "foo"
})
);

test.cb(
"should be able to specify a default selected option in a list",
cli({
cmd: `node ${path.join("src", "cli.js")} ${path.join(
"test",
"fixtures",
"simpletest"
)} --stdin-tty=<%= stdin %> -D bar`,
input: ["k", "\n"],
output: "foo"
})
);

test.cb(
"should be able to specify a list of default choices to select for multiple choices",
cli({
cmd: `node ${path.join("src", "cli.js")} ${path.join(
"test",
"fixtures",
"simpletest"
)} --stdin-tty=<%= stdin %> -m -D "lorem${sep}ipsum${sep}sit"`,
input: ["j", " ", "j", "j", " ", sep],
output: `bar${sep}lorem${sep}sit`
})
);

test.cb(
"should be able to use ---default-separator to split multiple default choices",
cli({
cmd: `node ${path.join("src", "cli.js")} ${path.join(
"test",
"fixtures",
"simpletest"
)} --stdin-tty=<%= stdin %> -m --default-separator=, -D "lorem,ipsum,sit"`,
input: ["j", " ", "j", "j", " ", sep],
output: `bar${sep}lorem${sep}sit`
})
);

test.cb(
"should be able use --separator as the default separator to split multiple default choices",
cli({
cmd: `node ${path.join("src", "cli.js")} ${path.join(
"test",
"fixtures",
"test.csv"
)} --stdin-tty=<%= stdin %> -m --separator=, -D "banana,mangoes"`,
input: ["j", " ", sep],
output: `banana,oranges,mangoes`
})
);

test.cb(
"should be able override --separator with --default-separator to split multiple default choices",
cli({
cmd: `node ${path.join("src", "cli.js")} ${path.join(
"test",
"fixtures",
"test.csv"
)} --stdin-tty=<%= stdin %> -m --separator=, --default-separator=: -D "banana:mangoes"`,
input: ["j", " ", sep],
output: `banana,oranges,mangoes`
})
);
}