Skip to content

Commit

Permalink
Truncate status according to maximum termal width
Browse files Browse the repository at this point in the history
  • Loading branch information
smikitky committed Apr 4, 2024
1 parent 129c3ce commit 00f02b8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const configureApiCaller = (options: ConfigureApiOptions) => {
onStatus(status);
return status;
} finally {
intervalId && clearInterval(intervalId);
clearInterval(intervalId);
}
onStatus({ status: 'done', translation: resultText });
return { status: 'done', translation: resultText };
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ const main = async () => {
String(config.temperature),
'\n'
);

const printStatus = () => {
if (args.quiet) return;
if (config.quiet) return;
process.stdout.write('\x1b[1A\x1b[2K'); // clear previous line
console.log(statusToText(status));
console.log(statusToText(status, process.stdout.columns - 1));
};
printStatus();

Expand Down
20 changes: 18 additions & 2 deletions src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const truncateStr = (str: string, maxWidth: number): string => {
return truncatedStr;
};

export const statusToText = (status: Status): string => {
export const statusToText = (
status: Status,
maxWidth = Number.POSITIVE_INFINITY
): string => {
switch (status.status) {
case 'waiting':
return '⏳';
Expand All @@ -33,7 +36,20 @@ export const statusToText = (status: Status): string => {
return `⚡${tok}`;
}
case 'split': {
return `[${status.members.map(statusToText).join(', ')}]`;
const m = status.members;
let str = status.members.map(statusToText).join(', ');
if (stringWidth(str) + 2 > maxWidth) {
const doneCount = m.filter(m => m.status === 'done').length;
const remaining = m
.filter(m => m.status !== 'done')
.map(statusToText)
.join(', ');
str = truncateStr(
(doneCount > 0 ? `✅x${doneCount}, ` : '') + remaining,
maxWidth - 2
);
}
return `[${str}]`;
}
case 'done':
return '✅';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/md-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const restoreCodeBlocks = (
*/
export const splitStringAtBlankLines = (
input: string,
fragmentLength = 2048
fragmentLength: number
): string[] | null => {
const lines = input.split('\n');
let inCodeBlock = false;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/readlineFromStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Readable } from 'node:stream';

export default async function* readlineFromStream(stream: Readable) {
// We use this because the default readline module does not
// forward errors from the stream.
let remaining = '';
for await (const chunk of stream) {
remaining += chunk;
Expand Down

0 comments on commit 00f02b8

Please sign in to comment.