Skip to content

Commit

Permalink
feat: remove message helper line breaks by default
Browse files Browse the repository at this point in the history
  • Loading branch information
adbayb committed Nov 18, 2024
1 parent c3d6b7d commit a14c632
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-windows-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"termost": minor
---

Remove default line breaks for message helper.
16 changes: 12 additions & 4 deletions examples/command/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ program
handler(context, argv) {
const { globalFlag, localFlag } = context;

helpers.message(`👋 Hello, I'm the ${argv.command} command`);
helpers.message(`👉 Shared global flag = ${globalFlag}`);
helpers.message(`👉 Local command flag = ${localFlag}`);
helpers.message(`👉 Context value = ${JSON.stringify(context)}`);
helpers.message(`👋 Hello, I'm the ${argv.command} command`, {
lineBreak: { end: true, start: false },
});
helpers.message(`👉 Shared global flag = ${globalFlag}`, {
label: false,
});
helpers.message(`👉 Local command flag = ${localFlag}`, {
lineBreak: true,
});
helpers.message(`👉 Context value = ${JSON.stringify(context)}`, {
lineBreak: { end: true, start: true },
});
helpers.message(`👉 Argv value = ${JSON.stringify(argv)}`);
},
});
Expand Down
68 changes: 26 additions & 42 deletions termost/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -109,68 +109,58 @@ OPTIONS:
`;
exports[`termost > should handle \`command\` api 2`] = `
"
ℹ️ Information
"ℹ️ Information
 👋 Hello, I'm the build command

ℹ️ Information
 👉 Shared global flag = false
ℹ️ 👉 Shared global flag = false

ℹ️ Information
 👉 Local command flag = local-value

ℹ️ Information
 👉 Context value = {"globalFlag":false,"localFlag":"local-value"}
[34m[34m[1m
ℹ️ Information[22m[34m[39m
[34m[34m[1mℹ️ Information[22m[34m[39m
 👉 Argv value = {"command":"build","operands":[],"options":{}}"
`;
exports[`termost > should handle \`command\` api 3`] = `
"
ℹ️ Information
"ℹ️ Information
 👋 Hello, I'm the watch command

ℹ️ Information
ℹ️ Information
 👉 Shared global flag = false

ℹ️ Information
ℹ️ Information
 👉 Context value = {"globalFlag":false}

ℹ️ Information
ℹ️ Information
 👉 Argv value = {"command":"watch","operands":[],"options":{}}"
`;
exports[`termost > should handle \`command\` api 4`] = `
"
ℹ️ Information
"ℹ️ Information
 👋 Hello, I'm the build command

ℹ️ Information
 👉 Shared global flag = true
ℹ️ 👉 Shared global flag = true

ℹ️ Information
 👉 Local command flag = hello

ℹ️ Information
 👉 Context value = {"globalFlag":true,"localFlag":"hello"}
[34m[34m[1m
ℹ️ Information[22m[34m[39m
[34m[34m[1mℹ️ Information[22m[34m[39m
 👉 Argv value = {"command":"build","operands":[],"options":{"global":true,"local":"hello"}}"
`;
exports[`termost > should handle \`command\` api 5`] = `
"
ℹ️ Information
"ℹ️ Information
 👋 Hello, I'm the watch command

ℹ️ Information
ℹ️ Information
 👉 Shared global flag = true

ℹ️ Information
ℹ️ Information
 👉 Context value = {"globalFlag":true}

ℹ️ Information
ℹ️ Information
 👉 Argv value = {"command":"watch","operands":[],"options":{"global":true}}"
`;
Expand Down Expand Up @@ -204,8 +194,7 @@ OPTIONS:
`;
exports[`termost > should handle \`option\` api 1`] = `
"
ℹ️ Information
"ℹ️ Information
 {
"optionWithAlias": 0,
"optionWithoutAlias": "defaultValue"
Expand All @@ -223,20 +212,15 @@ exports[`termost > should handle \`task\` api 1`] = `
✔ Or even execute external commands thanks to its provided helpers
❯ A task can have a dynamic label generated from contextual values: small
✔ A task can have a dynamic label generated from contextual values: small

ℹ️ Label & console output
ℹ️ Label & console output
 If you don't specify a label, the handler is executed in "live mode" (the output is not hidden by the label and is displayed gradually).

ℹ️ Context management
ℹ️ Context management
 A task with a specified "key" can be retrieved here. Size = 45. If no "key" was specified the task returned value cannot be persisted across program instructions.

ℹ️ Output formatting
ℹ️ Output formatting
 The \`message\` helpers can be used to display task content in a nice way

✅ Success
✅ Success
 The \`message\` helpers can be used to display task content in a nice way

ℹ️ 👋 You can also customize the label
ℹ️ 👋 You can also customize the label
 The \`message\` helpers can be used to display task content in a nice way

You can also have a total control on the formatting through the \`format\` helper.
Expand Down
36 changes: 23 additions & 13 deletions termost/src/helpers/stdout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ export const message = (
const isTextualContent = typeof content === "string";
const type = optionType ?? (isTextualContent ? "information" : "error");
const { color, defaultLabel, icon, method } = formatPropertiesByType[type];
const hasNoLabel = optionLabel === false;

const getLineBreak = (): LineBreakByPosition => {
if (optionlineBreak === undefined) {
return {
end: false,
start: true,
start: false,
};
}

Expand All @@ -80,7 +81,7 @@ export const message = (
};

const getLabel = () => {
if (optionLabel === false) {
if (hasNoLabel) {
return isTextualContent ? content : content.message;
}

Expand All @@ -89,18 +90,27 @@ export const message = (

const lineBreak = getLineBreak();

method(
format(
`${lineBreak.start ? "\n" : ""}${icon} ${getLabel()}${lineBreak.end ? "\n" : ""}`,
{
color,
modifiers: ["bold"],
},
),
);
const output = [
format(`${lineBreak.start ? "\n" : ""}${icon} ${getLabel()}`, {
color,
modifiers: ["bold"],
}),
!hasNoLabel && isTextualContent
? format(` ${content}`, { color })
: undefined,
!isTextualContent
? // Do not format error with colors to preserve the stack trace:
content
: undefined,
].filter(Boolean);

output.forEach((item) => {
method(item);
});

// Do not format error with colors to preserve the stack trace:
method(isTextualContent ? format(` ${content}`, { color }) : content);
if (lineBreak.end) {
method();
}
};

type LineBreakByPosition = { end: boolean; start: boolean };
Expand Down

0 comments on commit a14c632

Please sign in to comment.