Skip to content

Commit

Permalink
feat: upgrade ndate command
Browse files Browse the repository at this point in the history
  • Loading branch information
JonDotsoy committed Jul 28, 2023
1 parent 80d4562 commit e83848e
Showing 1 changed file with 92 additions and 45 deletions.
137 changes: 92 additions & 45 deletions handler.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,117 @@
import {
toDateStyle,
toTimeStyle,
dateParse,
toHourCycle,
} from "./lib/common.ts";
import {
dateParse,
toDateStyle,
toHourCycle,
toTimeStyle,
} from "./lib/common.ts";
import { renderDateTemplate } from "./lib/renderDateTemplate.ts";
import { makeFlags } from "./makeFlags.ts";

export const handler = async function*(args: string[]): AsyncGenerator<Uint8Array> {
let {
hourCycle, dateStyle, timeStyle, insertFinalNewLine, local, timeZone, date, outputAsEpoch, outputAsEpochMS, outputAsJSON, outputAsUTC, stdinReadable, showHelp, template, transformOptions, optionsLabels,
} = makeFlags(args);

if(showHelp) {
const items = Object.keys(transformOptions)
.map(item => {
const label = optionsLabels[item]?.label;
return label ? `[${item} ${label}]` : `[${item}]`;
});
const makeHelpDialog = function* (
transformOptions: Record<
string,
(nextArgument: () => string | undefined) => void
>,
optionsLabels: Record<
string,
{
label?: string | undefined;
} | undefined
>,
) {
const items = Object.keys(transformOptions)
.map((item) => {
const label = optionsLabels[item]?.label;
return label ? `[${item} ${label}]` : `[${item}]`;
});

const textUsage = `Usage: ndate`;
const textUsage = `Usage: ndate`;

const lines: string[] = [];
let currentLine: string | undefined;
for(const item of items) {
if(!currentLine) {
currentLine = lines.length ? `${' '.repeat(textUsage.length)}` : `${textUsage}`;
}
const lines: string[] = [];
let currentLine: string | undefined;
for (const item of items) {
if (!currentLine) {
currentLine = lines.length
? `${" ".repeat(textUsage.length)}`
: `${textUsage}`;
}

currentLine = `${currentLine} ${item}`;
if(currentLine.length > 80) {
lines.push(currentLine);
currentLine = undefined;
}
currentLine = `${currentLine} ${item}`;
if (currentLine.length > 80) {
lines.push(currentLine);
currentLine = undefined;
}
}

if(currentLine) lines.push(currentLine);
if (currentLine) lines.push(currentLine);

for(const line of lines) {
yield new TextEncoder().encode(line);
yield new TextEncoder().encode('\n');
}
for (const line of lines) {
yield new TextEncoder().encode(line);
yield new TextEncoder().encode("\n");
}
};

export const handler = async function* (
args: string[],
): AsyncGenerator<Uint8Array> {
let {
hourCycle,
dateStyle,
timeStyle,
insertFinalNewLine,
local,
timeZone,
date,
outputAsEpoch,
outputAsEpochMS,
outputAsJSON,
outputAsUTC,
stdinReadable,
showHelp,
template,
transformOptions,
optionsLabels,
} = makeFlags(args);

if (showHelp) {
yield* makeHelpDialog(transformOptions, optionsLabels);
return;
}

if(timeZone) Deno.env.set(`TZ`, timeZone);
if(local) Deno.env.set(`LANG`, local);
if (timeZone) Deno.env.set(`TZ`, timeZone);
if (local) Deno.env.set(`LANG`, local);

if(stdinReadable) {
if (stdinReadable) {
const buff = new Uint8Array(256);
await Deno.stdin.read(buff);
const text = new TextDecoder().decode(buff.subarray(0, buff.findIndex(p => p === 0))).trim();
const text = new TextDecoder().decode(
buff.subarray(0, buff.findIndex((p) => p === 0)),
).trim();
date = dateParse(text);
}

const toOutput = () => {
if(template) return renderDateTemplate(template, date, local, { dateStyle: toDateStyle(dateStyle), timeStyle: toTimeStyle(timeStyle), timeZone, hourCycle: toHourCycle(hourCycle) });
if(outputAsEpochMS) return Math.floor(date.getTime()).toString();
if(outputAsEpoch) return Math.floor(date.getTime() / 1000).toString();
if(outputAsJSON) return date.toJSON();
if(outputAsUTC) return date.toUTCString();
return date.toLocaleString(local, { dateStyle: toDateStyle(dateStyle), timeStyle: toTimeStyle(timeStyle), timeZone, hourCycle: toHourCycle(hourCycle) });
if (template) {
return renderDateTemplate(template, date, local, {
dateStyle: toDateStyle(dateStyle),
timeStyle: toTimeStyle(timeStyle),
timeZone,
hourCycle: toHourCycle(hourCycle),
});
}
if (outputAsEpochMS) return Math.floor(date.getTime()).toString();
if (outputAsEpoch) return Math.floor(date.getTime() / 1000).toString();
if (outputAsJSON) return date.toJSON();
if (outputAsUTC) return date.toUTCString();
return date.toLocaleString(local, {
dateStyle: toDateStyle(dateStyle),
timeStyle: toTimeStyle(timeStyle),
timeZone,
hourCycle: toHourCycle(hourCycle),
});
};

yield new TextEncoder().encode(toOutput());

if(insertFinalNewLine) yield new TextEncoder().encode(`\n`);
if (insertFinalNewLine) yield new TextEncoder().encode(`\n`);
};

0 comments on commit e83848e

Please sign in to comment.