forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratePlainOutput.ts
More file actions
91 lines (76 loc) · 2.88 KB
/
generatePlainOutput.ts
File metadata and controls
91 lines (76 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { PlainData } from "./getInformation";
import { lines } from "./getResponses";
import { getSaying } from "./getSaying";
import { getTimestamp } from "./getTimestamp";
/**
* @param info The plain data that will be shown at the top in two columns
* @param chartType The type of chart that will be shown. Ex: "Global Update", "Philippine Historical Chart"
* @param quiet Boolean, set to true if the user requsted quiet mode
* @param extraRows Any extra rows that will be presented under the main info. Used for Asciichart
* @returns A string showing the provided data and configuration
*/
export const generatePlainOutput: (
info: PlainData,
chartType: string,
quiet: boolean,
extraRows?: string[]
) => string = ({ data, metainfo }, chartType, quiet, extraRows) => {
// Set line depending if it contains a chart or not
let line = extraRows === undefined ? "-".repeat(60) : "-".repeat(68);
line += "\n";
let header = `${lines.defaultHeader} - ${chartType}`;
let timestamp = getTimestamp(metainfo.updated as number);
let saying = getSaying();
// Generate table
let table = "";
// Create columns
let normalizedArray: string[] = [];
Object.keys(data).forEach((key) => {
let value = data[key];
let line = `${key.padEnd(15, " ")}| ${value.padEnd(13, " ")}`; // create a line with length 30;
normalizedArray.push(line);
});
while (normalizedArray.length > 0) {
let left = normalizedArray.shift();
let right = normalizedArray.shift();
//right may be undefined, so default to empty string
if (right === undefined) right = "";
table += `${left}${right}`;
if (normalizedArray.length !== 0) table += `\n`; // do not add whitespace at the end of the table
}
// responseArray is the array of the raw data **before** adding the separator lines
let responseArray: string[] = [timestamp, table];
if (!quiet) responseArray.unshift(header);
// Add extraRows to responseArray
if (extraRows !== undefined) {
extraRows.forEach((str) => {
responseArray.push(str);
});
}
// Add the help msg and other messages
if (!quiet)
responseArray = responseArray.concat([
lines.helpMessage,
lines.docsLink,
lines.WNrepoLink,
`\n${saying}\n`,
]);
responseArray.push(`${lines.sponsorMessage}${lines.BMCLink}`);
if (!quiet)
responseArray.push(
`${lines.twitterPlug}${lines.handleHashtag.join(" ")}`
);
// Construct the final output
let response: string = "\n";
responseArray.forEach((str) => {
response += `${line}`;
response += `${str}\n`;
});
// Add padding to the side
response = response
.split("\n")
.map((str) => ` ${str}`)
.join("\n");
response += "\n";
return response;
};