aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPridon Tetradze <[email protected]>2022-12-10 12:07:19 +0400
committerGitHub <[email protected]>2022-12-10 12:07:19 +0400
commitcb43f40f8f3c9e495f025359eed6e24a6542f47f (patch)
tree99ac12a50e997a78309d08b676b1a3d55e5d3096
parentc7e06f35d9f4b6ac685d59de0ea7d062f520d0e0 (diff)
downloadcountryfetch-cb43f40f8f3c9e495f025359eed6e24a6542f47f.tar.xz
countryfetch-cb43f40f8f3c9e495f025359eed6e24a6542f47f.zip
add progress logger (#11)
-rwxr-xr-xrun.sh2
-rw-r--r--src/countries.ts18
-rw-r--r--src/util/logger.ts12
3 files changed, 26 insertions, 6 deletions
diff --git a/run.sh b/run.sh
index bbd8b74..0d62b77 100755
--- a/run.sh
+++ b/run.sh
@@ -1,4 +1,4 @@
#!/bin/sh
# Just a short-hand to run instead of typing it out in terminal during development
-deno run --allow-all --unstable "$@" ./index.ts \ No newline at end of file
+deno run --allow-all --unstable ./index.ts "$@" \ No newline at end of file
diff --git a/src/countries.ts b/src/countries.ts
index 2485c49..e2230dc 100644
--- a/src/countries.ts
+++ b/src/countries.ts
@@ -43,10 +43,9 @@ export class Countries {
this.cache.saveTxt("last-synced", JSON.stringify(Date.now()));
if (config?.flagAscii) {
- this.logger.alert(
- "Generating ASCII art for each country flag. This may take a minute..."
- );
- const flagStrings = await this.generateFlagImgs(countries);
+ const logTitle =
+ "Generating ASCII art for each country flag. This may take a minute...";
+ const flagStrings = await this.generateFlagImgs(countries, logTitle);
this.flags = flagStrings;
this.cache.saveJson("flags", flagStrings);
}
@@ -173,8 +172,12 @@ export class Countries {
return result.join(" | ");
}
- private async generateFlagImgs(countries: Country[]): Promise<FlagAscii[]> {
+ private async generateFlagImgs(
+ countries: Country[],
+ logTitle?: string
+ ): Promise<FlagAscii[]> {
const data = [];
+ let index = 0;
for (const country of countries) {
// Replace png with jpg as the library used has trouble with png
const flagUrl = country.flags["png"].replace(".png", ".jpg");
@@ -183,6 +186,11 @@ export class Countries {
countryName: country.name.common,
flagString,
});
+ index++;
+ this.logger.progress(index, countries.length, {
+ title: logTitle,
+ description: "Generating a flag for " + country.name.common,
+ });
}
return data;
}
diff --git a/src/util/logger.ts b/src/util/logger.ts
index 4df9fb7..49dcd86 100644
--- a/src/util/logger.ts
+++ b/src/util/logger.ts
@@ -18,6 +18,18 @@ export class Logger {
console.error(nano.red(data));
}
+ public progress(
+ current: number,
+ total: number,
+ config?: { title: string; description: string }
+ ) {
+ console.clear();
+ if (config?.title) {
+ this.alert(config.title);
+ }
+ console.log(`${current}/${total} ${config?.description || ""}`);
+ }
+
public logCountry(country: FetchedCountry) {
console.log(
nano.cyan("Country:"),