Skip to content

Commit

Permalink
refactor: simplify console output
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 13, 2023
1 parent b45be3f commit e741be0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ export async function listen(
const showURL = async (showURLOptions: ShowURLOptions = {}) => {
const lines = [];

const copiedToClipboardMessage = listhenOptions.clipboard
? colors.gray("(copied to clipboard)")
: "";

const nameSuffix =
showURLOptions.name || listhenOptions.name
? ` (${showURLOptions.name || listhenOptions.name})`
Expand All @@ -191,6 +187,8 @@ export async function listen(
const firstLocalUrl = urls.find((u) => u.type === "local");
const firstPublicUrl = urls.find((u) => u.type !== "local");

const showQR = (showURLOptions.qr ?? listhenOptions.qr) !== false;

const typeMap: Record<ListenURL["type"], [string, ColorName]> = {
local: ["Local", "green"],
tunnel: ["Tunnel", "yellow"],
Expand All @@ -202,8 +200,14 @@ export async function listen(
const label = getColor(type[1])(
` ➜ ${(type[0] + ":").padEnd(8, " ")}${nameSuffix} `,
);
const suffix = url === firstLocalUrl ? copiedToClipboardMessage : "";
lines.push(`${label} ${formatURL(url.url)} ${suffix}`);
let suffix = "";
if (url === firstLocalUrl && listhenOptions.clipboard) {
suffix += colors.gray(" [copied to clipboard]");
}
if (url === firstPublicUrl && showQR) {
suffix += colors.gray(" [QR code ⬇️ ]");
}
lines.push(`${label} ${formatURL(url.url)}${suffix}`);
}

if (!firstPublicUrl) {
Expand All @@ -212,15 +216,15 @@ export async function listen(
);
}

if (firstPublicUrl && (showURLOptions.qr ?? listhenOptions.qr) !== false) {
// Show QR code
if (firstPublicUrl && showQR) {
const space = " ".repeat(15);
lines.push(" ");
lines.push(
...renderQRCode(firstPublicUrl.url)
.split("\n")
.map((line) => space + line),
);
lines.push(space + formatURL(firstPublicUrl.url));
}

// eslint-disable-next-line no-console
Expand Down

0 comments on commit e741be0

Please sign in to comment.