Skip to content

Commit

Permalink
Merge pull request #42 from fnky/select-default-choices
Browse files Browse the repository at this point in the history
Add option to select default choices
  • Loading branch information
ruyadorno authored Apr 5, 2018
2 parents c480f16 + 772a2a8 commit 90647fd
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 33 deletions.
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")
.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
);
}
}

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`
})
);
}

0 comments on commit 90647fd

Please sign in to comment.