Skip to content

Commit 99fefd8

Browse files
committed
add adaptive message for arrival times
Signed-off-by: Daniel Stamer <[email protected]>
1 parent 4c10386 commit 99fefd8

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

cmd/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"time"
78

89
"github.com/spf13/cobra"
@@ -44,3 +45,7 @@ func unixMillisToTime(millis uint64) time.Time {
4445
seconds := int64(millis / 1000)
4546
return time.Unix(seconds, 0)
4647
}
48+
49+
func formatTimeDelta(d time.Duration) string {
50+
return strings.Split(d.String(), ".")[0]
51+
}

cmd/stops.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ var stopsCmd = &cobra.Command{
4040
if remainingDuration < 0 {
4141
return "-"
4242
}
43-
return remainingDuration.String()
43+
if remainingDuration < time.Duration(time.Minute*3) {
44+
return "GET OUT NOW"
45+
}
46+
return formatTimeDelta(remainingDuration)
4447
}()
4548
stopView.DelayReasons = func() string {
4649
reasons := []string{}

cmd/trip.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"os"
10+
"time"
1011

1112
"github.com/spf13/cobra"
1213

@@ -33,14 +34,15 @@ type Trip struct {
3334
NextStop string `header:"Next Stop"`
3435
StopsToDestination int
3536
Progress string `header:"Progress"`
37+
TimeToArrival string `header:"Arriving"`
3638
}
3739

3840
type StopInfo struct {
3941
ScheduledNext string `json:"scheduledNext"`
4042
ActualNext string `json:"actualNext"`
4143
ActualLast string `json:"actualLast"`
4244
ActualLastStarted string `json:"actualLastStarted"`
43-
FinalStationName string `header:"FD" json:"finalStationName"`
45+
FinalStationName string `json:"finalStationName"`
4446
}
4547

4648
type Stop struct {
@@ -199,6 +201,21 @@ func refreshTrip() (Trip, error) {
199201
}
200202
return fmt.Sprintf("%d/%d", departedStops, envelope.Trip.StopsToDestination)
201203
}()
204+
envelope.Trip.TimeToArrival = func() string {
205+
var arrival uint64
206+
for _, stop := range envelope.Trip.Stops {
207+
if stop.Station.Name == envelope.Trip.YourDestination {
208+
arrival = stop.TimeTable.ActualArrivalTime
209+
break
210+
}
211+
}
212+
arrivalTime := unixMillisToTime(arrival)
213+
remainingDuration := time.Until(arrivalTime)
214+
if remainingDuration < time.Duration(time.Minute*3) {
215+
return "GET OUT NOW"
216+
}
217+
return formatTimeDelta(remainingDuration)
218+
}()
202219

203220
return envelope.Trip, nil
204221
}

0 commit comments

Comments
 (0)