Skip to content

Commit acc513e

Browse files
committed
Fixes
1 parent afb46c1 commit acc513e

File tree

6 files changed

+862
-224
lines changed

6 files changed

+862
-224
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.env
22
node_modules
3-
.exe
3+
.exe
4+
5+
build

build/index.js

-119
This file was deleted.

build/index.js.map

-1
This file was deleted.

index.ts

+80-68
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import ora from "ora";
66
dotenv.config();
77

88
// Log in to NR api
9-
const api = new LDBWSClient(process.env.TOKEN!!)
9+
const api = new LDBWSClient(process.env.TOKEN!!);
1010

1111
// Get the station code args
12-
const crs = process.argv[2].toUpperCase();
12+
process.argv.shift();
13+
process.argv.shift();
14+
const crs = process.argv.join().toUpperCase();
1315

1416
// Get the number of services
1517
if (process.argv.length == 4) {
16-
var num = process.argv[3]
17-
} else num = 10
18+
var num = process.argv[3];
19+
} else num = 10;
1820

1921
// Clear the terminal before printing
2022
print("\x1Bc");
@@ -24,87 +26,97 @@ const spinner = ora("Loading departure board for " + crs).start();
2426

2527
// Update the data every 30 seconds
2628
async function update() {
27-
// Move the cursor to remove the new line
28-
setTimeout(async function () {
29-
// Get the departures from NR
30-
var resp = await api.GetDepartureBoard(crs, num);
31-
// Move the cursor to write over the previous table
32-
print("\x1B[5;0H");
33-
// Print the data
34-
printBody(resp);
35-
// Live updates
36-
update();
37-
}, 30000);
38-
}
39-
40-
// Get the initial data & then begin updates
41-
const get = async () => {
29+
// Move the cursor to remove the new line
30+
setTimeout(async function () {
4231
// Get the departures from NR
43-
try {
44-
var resp = await api.GetDepartureBoard(crs, num);
45-
} catch(err) {
46-
// If an error occured
47-
print(chalk.red("\nError: " + err.message));
48-
return
49-
}
50-
// Stop spinning & clear the terminal
51-
// to remove loading message
52-
spinner.stop();
53-
print("\x1Bc");
54-
// Print the title block
55-
title("BHM");
56-
// Print the table headings
57-
print(chalk.bold(formatString("Time", 5)));
58-
print(chalk.bold(formatString("Destination", 25)));
59-
print(chalk.bold(formatString("Plat", 4)));
60-
print(chalk.bold(formatString("Expected", 11)));
61-
print(chalk.bold(formatString("Operator", 30)) + "\n");
32+
var resp = await api.GetDepartureBoard(crs, num);
33+
// Move the cursor to write over the previous table
34+
print("\x1B[5;0H");
6235
// Print the data
6336
printBody(resp);
6437
// Live updates
6538
update();
39+
}, 30000);
6640
}
6741

68-
get()
42+
// Get the initial data & then begin updates
43+
const get = async () => {
44+
// Get the departures from NR
45+
try {
46+
var resp = await api.GetDepartureBoard(crs, num);
47+
} catch (err) {
48+
// If an error occured
49+
print(chalk.red("\nError: " + err.message));
50+
return;
51+
}
52+
// Stop spinning & clear the terminal
53+
// to remove loading message
54+
spinner.stop();
55+
print("\x1Bc");
56+
// Print the title block
57+
title("BHM");
58+
// Print the table headings
59+
print(chalk.bold(formatString("Time", 5)));
60+
print(chalk.bold(formatString("Destination", 25)));
61+
print(chalk.bold(formatString("Plat", 4)));
62+
print(chalk.bold(formatString("Expected", 11)));
63+
print(chalk.bold(formatString("Operator", 30)) + "\n");
64+
// Print the data
65+
printBody(resp);
66+
// Live updates
67+
update();
68+
};
69+
70+
get();
6971

7072
// Print the main body of data from the response
7173
function printBody(resp) {
72-
resp.trainServices.service.forEach(departure => {
73-
// If there isn't a platform, set it to "-"
74-
if (departure.platform != null) {
75-
var platform = departure.platform
76-
} else platform = "-"
77-
// Calculate formatting for the expected time
78-
var expected
79-
if (departure.etd == "On time") expected = chalk.green(formatString("✓ " + departure.etd, 11))
80-
else if (departure.etd == "Cancelled") expected = chalk.red(formatString("✗ " + departure.etd, 11))
81-
else expected = chalk.yellow(formatString("! " + departure.etd, 11))
82-
// Print the row out
83-
print(chalk.gray(formatString(departure.std, 5)))
84-
print(chalk.white(formatString(departure.destination.location[0].locationName, 25)))
85-
print(chalk.grey(formatString(platform, 4)))
86-
print(expected)
87-
print(chalk.grey(formatString(departure.operator, 30)))
88-
print("\n") // New line for next row
89-
});
74+
resp.trainServices.service.forEach((departure) => {
75+
// If there isn't a platform, set it to "-"
76+
if (departure.platform != null) {
77+
var platform = departure.platform;
78+
} else platform = "-";
79+
// Calculate formatting for the expected time
80+
var expected;
81+
if (departure.etd == "On time")
82+
expected = chalk.green(formatString("✓ " + departure.etd, 11));
83+
else if (departure.etd == "Cancelled")
84+
expected = chalk.red(formatString("✗ " + departure.etd, 11));
85+
else expected = chalk.yellow(formatString("! " + departure.etd, 11));
86+
// Print the row out
87+
print(chalk.gray(formatString(departure.std, 5)));
88+
print(
89+
chalk.white(
90+
formatString(departure.destination.location[0].locationName, 25)
91+
)
92+
);
93+
print(chalk.grey(formatString(platform, 4)));
94+
print(expected);
95+
print(chalk.grey(formatString(departure.operator, 30)));
96+
print("\n"); // New line for next row
97+
});
9098
}
9199

92100
// Prin the title block
93101
function title() {
94-
print(chalk.redBright("LIVE") + chalk.white(" Departures from " + crs + "\n")) // Station code
95-
print(chalk.gray("Powered by National Rail Enquiries\n")) // NR Attribution
96-
console.log() // New line for spacing
102+
print(
103+
chalk.redBright("LIVE") + chalk.white(" Departures from " + crs + "\n")
104+
); // Station code
105+
print(chalk.gray("Powered by National Rail Enquiries\n")); // NR Attribution
106+
console.log(); // New line for spacing
97107
}
98108

99109
// Wrapper function for printing without new line
100-
function print(string) { process.stdout.write(string); }
110+
function print(string) {
111+
process.stdout.write(string);
112+
}
101113

102114
// Format a string by adding spaces to the end
103115
function formatString(string, maxLength) {
104-
var spaces = maxLength - string.length;
105-
var result = string;
106-
for (var i = 0; i < spaces + 2; i++) {
107-
result += " "
108-
}
109-
return result
110-
}
116+
var spaces = maxLength - string.length;
117+
var result = string;
118+
for (var i = 0; i < spaces + 2; i++) {
119+
result += " ";
120+
}
121+
return result;
122+
}

0 commit comments

Comments
 (0)