103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package bus
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
type QueryResult struct {
|
|
Schedules *map[BusStop] *BusStopBuses
|
|
Lang string
|
|
Error error
|
|
Query *QueryObject
|
|
}
|
|
|
|
var MARKDOWN_ESC []string = []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
|
|
func _escape( t string ) string {
|
|
for _, c := range MARKDOWN_ESC {
|
|
t = strings.Replace( t, c, "\\" + c, -1 )
|
|
}
|
|
return t
|
|
}
|
|
|
|
func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
|
|
if b.PrevStop() != nil {
|
|
sb.WriteString( _escape( (*b.PrevStop().Name)[ *lang ] ) )
|
|
sb.WriteString( " \\> " )
|
|
}
|
|
|
|
sb.WriteString( "*" )
|
|
sb.WriteString( _escape( (*b.Name)[ *lang ] ) )
|
|
sb.WriteString( "*" )
|
|
|
|
if b.NextStop() != nil {
|
|
sb.WriteString( " \\> " )
|
|
sb.WriteString( _escape( (*b.NextStop().Name)[ *lang ] ) )
|
|
}
|
|
|
|
sb.WriteString( "\n" )
|
|
}
|
|
|
|
func ( result QueryResult ) Message() string {
|
|
|
|
if result.Error != nil {
|
|
return fmt.Sprintf( "%s", result.Error )
|
|
}
|
|
|
|
sb := strings.Builder{}
|
|
|
|
if result.Schedules == nil {
|
|
|
|
query := *result.Query
|
|
if query.Route == "" {
|
|
sort.Sort( ByRouteId( *query.BusStops ) )
|
|
for _, busStop := range *query.BusStops {
|
|
sb.WriteString( _escape( busStop.RouteId ) )
|
|
sb.WriteString( " " )
|
|
writeShortRoute( &result.Lang, &sb, &busStop )
|
|
}
|
|
} else if query.BusStops == nil && query.RouteStarts != nil {
|
|
for d, b := range *query.RouteStarts {
|
|
sb.WriteString( query.Route )
|
|
sb.WriteString( "\\-" )
|
|
sb.WriteString( _escape( d ) )
|
|
sb.WriteString( "\n " )
|
|
|
|
for {
|
|
sb.WriteString( _escape( (*b.Name)[ result.Lang ] ) )
|
|
b = b.NextStop()
|
|
if b == nil {
|
|
break
|
|
}
|
|
|
|
sb.WriteString( " \\> " )
|
|
}
|
|
sb.WriteString( "\n" )
|
|
}
|
|
} else {
|
|
sb.WriteString( _escape( "Unreachable condition occured!?" ) )
|
|
}
|
|
} else {
|
|
if 0 < len(*result.Schedules) {
|
|
for busStop, buses := range *result.Schedules {
|
|
writeShortRoute( &result.Lang, &sb, &busStop )
|
|
for _, bus := range buses.Buses {
|
|
sb.WriteString( " \\* " )
|
|
if bus.ETAText == "" {
|
|
sb.WriteString( _escape( bus.ETDText ) )
|
|
} else {
|
|
sb.WriteString( _escape( bus.ETAText ) )
|
|
}
|
|
sb.WriteString( "\n" )
|
|
}
|
|
sb.WriteString( "\n" )
|
|
}
|
|
} else {
|
|
sb.WriteString( _escape( "Schedules are empty...perhaps Out of Service Time?" ) )
|
|
}
|
|
}
|
|
|
|
return sb.String()
|
|
}
|