Skip to content

Commit

Permalink
Add documentation & reformat some code
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Oct 11, 2022
1 parent 29a40e8 commit b699ae5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 45 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,19 @@ Vous pouvez sauvegarder vos résultats dans un fichier JSON grâce à l'option `
linky loadcurve --start 2021-12-31 --end 2022-01-01 --output data/ma_conso.json
```

Pour voir l'aide détaillée :
Vous pouvez changer le format d'affichage de sortie grâce à l'option `--format`

```bash
linky daily --start 2022-01-01 --end 2022-01-08 --format json
```

Vous pouvez masquer les messages et animations de progression grâce à l'option `--quiet`

```bash
linky maxpower --quiet
```

Pour voir l'aide détaillée et plus d'exemples :

```bash
linky --help
Expand Down
2 changes: 1 addition & 1 deletion bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const mainHelp = `
linky auth -a Kft3SIZrcq -r F3AR0K8eoC -u 225169
linky daily
linky dailyprod --start 2022-01-01 --end 2022-01-08
linky maxpower --start 2021-08-01 --end 2021-08-15
linky maxpower --start 2021-08-01 --end 2021-08-15 --format json --quiet
linky loadcurve -s 2022-01-01 -e 2022-01-08 -o data/ma_conso.json
linky loadcurveprod -u 225169
`;
Expand Down
84 changes: 41 additions & 43 deletions bin/metering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,72 +93,70 @@ function handle(
const session = store.getStoredSession(usagePointId);
const previousAccessToken = session?.accessToken;

const spinner = ora();
if (!quiet) spinner.start(spinnerText);
const spinner = ora({ isSilent: quiet }).start(spinnerText);

return promise
.then(async (consumption) => {
if (output) {
try {
await mkdirp(path.dirname(output));
fs.writeFileSync(output, JSON.stringify(consumption, null, 4));
fs.writeFileSync(output, JSON.stringify(consumption, null, 2));
} catch (e) {
throw new Error(`Impossible d'écrire dans ${output}:\n${e.message}`);
}
}

if (!quiet) {
spinner.succeed();
spinner.succeed();

if (store.getStoredSession(session?.usagePointId)?.accessToken !== previousAccessToken) {
ora('Vos tokens ont été automatiquement renouvelés').succeed();
}
if (store.getStoredSession(session?.usagePointId)?.accessToken !== previousAccessToken) {
ora({ isSilent: quiet }).succeed('Vos tokens ont été automatiquement renouvelés');
}

if (output) {
ora(`Résultats sauvegardés dans ${output}`).succeed();
}
if (output) {
ora({ isSilent: quiet }).succeed(`Résultats sauvegardés dans ${output}`);
}

if (!output) {
render(format, consumption, displayTime);
}
})
.catch((e) => {
spinner.fail(e.message);
spinner.stop();
ora(e.message).fail();
throw new Error();
});
}

function render(format: Format, ...args: [Consumption, boolean]) {
Rendered[format](...args);
}

const Rendered = {
json(consumption: Consumption, displayTime: boolean) {
console.log(JSON.stringify(consumption, null, 2));
},
pretty(consumption: Consumption, displayTime: boolean) {
const maxValue = Math.max(...consumption.data.map((x) => x.value));
const chartLength = 30;
// Headers
console.info(
'\n' +
chalk.yellow.underline(`Date${' '.repeat(displayTime ? 16 : 7)}`) +
' ' +
chalk.green.underline(`Valeur (${consumption.unit})`) +
' ' +
chalk.cyan.underline(`Graphique${' '.repeat(chartLength - 9)}`)
);

for (const line of consumption.data) {
function render(format: Format, consumption: Consumption, displayTime: boolean) {
switch (format) {
case 'json':
console.info(JSON.stringify(consumption, null, 2));
break;
case 'pretty':
const maxValue = Math.max(...consumption.data.map((x) => x.value));
const chartLength = 30;
// Headers
console.info(
chalk.yellow(`${line.date} `) +
chalk.green(`${line.value}`) +
' '.repeat(10 + consumption.unit.length - line.value.toString().length) +
chalk.cyan(
'■'.repeat(maxValue && line.value ? Math.ceil((chartLength * line.value) / maxValue) : 0)
)
'\n' +
chalk.yellow.underline(`Date${' '.repeat(displayTime ? 16 : 7)}`) +
' ' +
chalk.green.underline(`Valeur (${consumption.unit})`) +
' ' +
chalk.cyan.underline(`Graphique${' '.repeat(chartLength - 9)}`)
);
}
},
};
// Data
for (const line of consumption.data) {
console.info(
chalk.yellow(`${line.date} `) +
chalk.green(`${line.value}`) +
' '.repeat(10 + consumption.unit.length - line.value.toString().length) +
chalk.cyan(
'■'.repeat(maxValue && line.value ? Math.ceil((chartLength * line.value) / maxValue) : 0)
)
);
}
break;
default:
ora(`Le format "${format}" est invalide. Formats acceptés: "pretty", "json".`).fail();
}
}

0 comments on commit b699ae5

Please sign in to comment.