/
text.go
55 lines (44 loc) · 1.2 KB
/
text.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
package format
import (
"fmt"
"io"
"math"
"github.com/asmarques/miles/flight"
)
// TextFormatter formats a path as plain text
var TextFormatter = &textFormatter{}
type textFormatter struct{}
func (tf *textFormatter) Write(path *flight.Path, writer io.Writer, verbose bool) error {
for i, pair := range path.Segments {
o := pair.Origin
d := pair.Destination
distance := int(math.Floor(pair.Distance() + 0.5))
var line string
if verbose {
line = fmt.Sprintf("%d:\t%s - %s, %s (%f, %f)\n\t%s - %s, %s (%f, %f)\n\t%d miles\n\n",
i, o.Iata, o.Name, o.Country, o.Lat, o.Long,
d.Iata, d.Name, d.Country, d.Lat, d.Long, distance)
} else {
line = fmt.Sprintf("%d: %s\t%s\t%d\tmiles\n", i,
o.Iata, d.Iata, distance)
}
_, err := fmt.Print(line)
if err != nil {
return fmt.Errorf("error writing output: %s", err)
}
}
if len(path.Segments) > 1 {
distance := int(math.Floor(path.Distance() + 0.5))
var line string
if verbose {
line = fmt.Sprintf("total:\t%d miles\n", distance)
} else {
line = fmt.Sprintf("\n\ttotal:\t%d\tmiles\n", distance)
}
_, err := fmt.Print(line)
if err != nil {
return fmt.Errorf("error writing output: %s", err)
}
}
return nil
}