Skip to content

Commit

Permalink
fix: upgrade spin progress, prevent lost characters
Browse files Browse the repository at this point in the history
  • Loading branch information
JonDotsoy committed Jun 25, 2024
1 parent c2c3c01 commit db09e76
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ import { pkg } from "./pkg";
function progresSpin() {
const symbols = [". ", ".. ", "..."];
let count = 0;
let paragraphSize = 0;
let stoped = false;

const layout: typeof String.raw = (...args) => {
const txt = String.raw(...args);
paragraphSize = Math.max(paragraphSize, txt.length);
return txt.padEnd(paragraphSize, " ");
};

const ping = () => {
process.stdout.write(`\r${symbols[count++ % symbols.length]} processing`);
process.stdout.write(
`\r${layout`${symbols[count++ % symbols.length]} processing`}`,
);
};

const interval = setInterval(() => ping(), 150);
Expand All @@ -22,7 +31,7 @@ function progresSpin() {
if (stoped) return;
stoped = true;
clearInterval(interval);
process.stdout.write(`\r`);
process.stdout.write(`\r${layout``}\r`);
};

return { stop };
Expand Down

0 comments on commit db09e76

Please sign in to comment.