-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.go
186 lines (164 loc) · 4.65 KB
/
format.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package bete
import (
"bytes"
"html/template"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/yi-jiayu/datamall/v3"
)
// Format represents how arrivals should be formatted.
type Format string
const (
// FormatSummary shows up to the next 3 incoming buses for each service.
FormatSummary Format = "s"
// FormatDetails shows up to the next 10 incoming buses and their ETA,
// occupancy, type (single or double-decked) and additional features
// (whether the bus is wheelchair accessible).
FormatDetails Format = "f"
)
var trimTrailingLettersRegexp = regexp.MustCompile("[^0-9]$")
var funcMap = map[string]interface{}{
"arrivingBuses": arrivingBuses,
"filterByService": filterByService,
"inSGT": inSGT,
"join": strings.Join,
"sortByArrival": sortByArrival,
"sortByService": sortByService,
"take": take,
"tips": tips,
"until": minutesUntil,
}
var (
arrivalSummaryTemplate = template.Must(template.New("arrival_summary").
Funcs(funcMap).
Parse(templateArrivalSummary))
arrivalDetailsTemplate = template.Must(template.New("arrival_details").
Funcs(funcMap).
Parse(templateArrivalDetails))
)
var sgt = time.FixedZone("SGT", 8*3600)
func inSGT(t time.Time) string {
return t.In(sgt).Format("Mon, 02 Jan 06 15:04 MST")
}
// minutesUntil returns the number of minutes from now until then.
func minutesUntil(now time.Time, then time.Time) string {
if then.IsZero() {
return "?"
}
return strconv.Itoa(int(then.Sub(now).Minutes()))
}
func sortByService(services []datamall.Service) []datamall.Service {
sort.Slice(services, func(i, j int) bool {
first, err1 := strconv.Atoi(trimTrailingLettersRegexp.ReplaceAllString(services[i].ServiceNo, ""))
second, err2 := strconv.Atoi(trimTrailingLettersRegexp.ReplaceAllString(services[j].ServiceNo, ""))
switch {
case err1 != nil && err2 != nil:
// when both services cannot be parsed as integers, sort them lexicographically
return services[i].ServiceNo < services[j].ServiceNo
case err1 == nil && err2 != nil:
// if j cannot be parsed as an integer, then i should come before j
return true
case err1 != nil && err2 == nil:
// if i cannot be parsed as an integer, then j should come before i
return false
}
if first == second {
return services[i].ServiceNo < services[j].ServiceNo
}
return first < second
})
return services
}
func filterByService(filter []string, services []datamall.Service) []datamall.Service {
if len(filter) == 0 {
return services
}
var filtered []datamall.Service
for _, s := range services {
for _, f := range filter {
if strings.EqualFold(s.ServiceNo, f) {
filtered = append(filtered, s)
}
}
}
return filtered
}
type ArrivingBus struct {
ServiceNo string
datamall.ArrivingBus
}
func arrivingBuses(services []datamall.Service) []ArrivingBus {
var buses []ArrivingBus
for _, service := range services {
if !service.NextBus.EstimatedArrival.IsZero() {
buses = append(buses, ArrivingBus{
ServiceNo: service.ServiceNo,
ArrivingBus: service.NextBus,
})
}
if !service.NextBus2.EstimatedArrival.IsZero() {
buses = append(buses, ArrivingBus{
ServiceNo: service.ServiceNo,
ArrivingBus: service.NextBus2,
})
}
if !service.NextBus3.EstimatedArrival.IsZero() {
buses = append(buses, ArrivingBus{
ServiceNo: service.ServiceNo,
ArrivingBus: service.NextBus3,
})
}
}
return buses
}
func sortByArrival(buses []ArrivingBus) []ArrivingBus {
sort.Slice(buses, func(i, j int) bool {
return buses[i].EstimatedArrival.Before(buses[j].EstimatedArrival)
})
return buses
}
// take returns up to the first n arriving buses.
func take(n int, arriving []ArrivingBus) []ArrivingBus {
if n <= 0 || n > len(arriving) {
n = len(arriving)
}
return arriving[:n]
}
type ArrivalInfo struct {
Stop BusStop
Time time.Time
Services []datamall.Service
Filter []string
ErrMsg string
}
func formatArrivalsSummary(arrivals ArrivalInfo) (string, error) {
b := new(bytes.Buffer)
err := arrivalSummaryTemplate.Execute(b, arrivals)
if err != nil {
return "", err
}
return b.String(), nil
}
func formatArrivalsDetails(arrivals ArrivalInfo) (string, error) {
b := new(bytes.Buffer)
err := arrivalDetailsTemplate.Execute(b, arrivals)
if err != nil {
return "", err
}
return b.String(), nil
}
// FormatArrivals formats arrivals with the specified format.
func FormatArrivals(arrivals ArrivalInfo, format Format) (string, error) {
switch format {
case FormatDetails:
return formatArrivalsDetails(arrivals)
default:
return formatArrivalsSummary(arrivals)
}
}
func tips(t time.Time) string {
return stringsTips[int(t.Unix())%len(stringsTips)]
}