forked from tools/josh
1
0
Fork 0

Allow newlines in output of echo

Fixes #145.
This commit is contained in:
Florine W. Dekker 2020-12-07 12:04:33 +01:00
parent 46400c0689
commit cd17170657
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
3 changed files with 35 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "0.39.1",
"version": "0.39.2",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"browser": "dist/bundle.js",

View File

@ -515,19 +515,22 @@ return new Command(
"echo": /* language=JavaScript */ `\
return new Command(
(input, streams) => {
const message = input.args.join(" ").replace("hunter2", "*******");
if (input.hasAnyOption("-n", "--newline"))
streams.out.write(message);
else
streams.out.writeLine(message);
let message = input.args.join(" ").replace("hunter2", "*******");
if (input.hasAnyOption("-e", "--escapes"))
message = message.replace(/\\\\n/g, "\\n");
if (!input.hasAnyOption("-n", "--newline"))
message = message + "\\n";
streams.out.write(message);
return ExitCode.OK;
},
\`display text\`,
\`echo [<b>-n</b> | <b>--newline</b>] [<u>text</u> <u>...</u>]\`,
\`echo [<b>-e</b> | <b>--escapes</b>] [<b>-n</b> | <b>--newline</b>] [<u>text</u> <u>...</u>]\`,
\`Displays each <u>text</u> separated by a single whitespace.
If the <b>--escapes</b> parameter is given, the newline escape sequence "\\\\n" is replaced by an actual newline
character.
Unless the <b>--newline</b> parameter is given, a newline is appended to the end.\`.trimMultiLines(),
new InputValidator()
)`,
@ -538,8 +541,10 @@ return new Command(
return parseInt(input.args[0] || "0");
},
\`close session\`,
\`exit\`,
\`Closes the terminal session.\`,
\`exit [<u>status</u>]\`,
\`Closes the terminal session.
Returns status code <u>status</u> if it is defined, and returns 0 otherwise.\`,
new InputValidator({minArgs: 0, maxArgs: 1})
)`,
"false": /* language=JavaScript */ `\

View File

@ -345,14 +345,28 @@ describe("commands", () => {
beforeEach(() => loadCommand("echo"));
it("adds a newline to the end by default", () => {
expect(execute("echo a b c \n")).to.equal(ExitCode.OK);
expect(readOut()).to.equal("a b c \n\n");
describe("--escapes", () => {
it("does not unescape newlines by default", () => {
expect(execute("echo 'a b \\n c'")).to.equal(ExitCode.OK);
expect(readOut()).to.equal("a b \\n c\n");
});
it("unescapes newlines if prompted to do so", () => {
expect(execute("echo -e 'a b \\n c'")).to.equal(ExitCode.OK);
expect(readOut()).to.equal("a b \n c\n");
});
});
it("does not add a newline if prompted to do so", () => {
expect(execute("echo -n a b c")).to.equal(ExitCode.OK);
expect(readOut()).to.equal("a b c");
describe("--newline", () => {
it("adds a newline to the end by default, even if there is already a newline", () => {
expect(execute("echo a b c \n")).to.equal(ExitCode.OK);
expect(readOut()).to.equal("a b c \n\n");
});
it("does not add a newline if prompted to do so", () => {
expect(execute("echo -n a b c")).to.equal(ExitCode.OK);
expect(readOut()).to.equal("a b c");
});
});
});