const calcDb = await db("calc", { history: [] });
function createResult(calculationResult, input) {
return {
name: calculationResult,
description: input,
value: { calculationResult, input },
};
}
const { calculationResult, input, ...rest } = await arg("0", async (input) => {
const choices = [];
if (input) {
let { stdout } = exec(
`bc <<<"${input.replace(/\,/g, ".")}" -l`,
{ silent: true }
);
if (stdout) {
choices.push(createResult(stdout.replace(/^\./, "0.").trim(), input));
return choices.concat(calcDb.history);
}
}
});
if (calculationResult) {
const history = calcDb.history;
history.unshift(createResult(calculationResult, input));
calcDb.history = history
.filter(({ description }, index) => index === 0 || description !== input)
.slice(0, 10);
await calcDb.write();
}
copy(calculationResult);