@@ -6,15 +6,17 @@ import ora from "ora";
6
6
dotenv . config ( ) ;
7
7
8
8
// Log in to NR api
9
- const api = new LDBWSClient ( process . env . TOKEN ! ! )
9
+ const api = new LDBWSClient ( process . env . TOKEN ! ! ) ;
10
10
11
11
// 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 ( ) ;
13
15
14
16
// Get the number of services
15
17
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 ;
18
20
19
21
// Clear the terminal before printing
20
22
print ( "\x1Bc" ) ;
@@ -24,87 +26,97 @@ const spinner = ora("Loading departure board for " + crs).start();
24
26
25
27
// Update the data every 30 seconds
26
28
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 ( ) {
42
31
// 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" ) ;
62
35
// Print the data
63
36
printBody ( resp ) ;
64
37
// Live updates
65
38
update ( ) ;
39
+ } , 30000 ) ;
66
40
}
67
41
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 ( ) ;
69
71
70
72
// Print the main body of data from the response
71
73
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
+ } ) ;
90
98
}
91
99
92
100
// Prin the title block
93
101
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
97
107
}
98
108
99
109
// 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
+ }
101
113
102
114
// Format a string by adding spaces to the end
103
115
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