/
render.go
111 lines (92 loc) · 2.75 KB
/
render.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
package actions
import (
"fmt"
"math"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/plush"
)
var r *render.Engine
var assetsBox = packr.New("app:assets", "../public")
// SecondsToHuman converts minutes to human duration string (1d 7h 32m )
func SecondsToHuman(duration int) string {
if duration == 0 {
return "0"
}
// seconds := int(duration % 60)
minutes := int(math.Floor(float64(duration%3600.0) / 60))
hours := int(math.Floor(float64(duration%86400.0) / 3600))
days := int(math.Floor(float64(duration) / 86400))
if days > 0 {
return fmt.Sprintf("%dd %02dh%02dm", days, hours, minutes)
}
if hours > 0 {
return fmt.Sprintf("%02dh%02dm", hours, minutes)
}
return fmt.Sprintf("%02dm", minutes)
}
func metersToKm(distance int) string {
return fmt.Sprintf("%.2f", float64(distance)/1000.0)
}
func speed(distanceMeters, durationSeconds int) string {
if durationSeconds == 0 {
return "-"
}
speedKmPerHour := float64(distanceMeters) / float64(durationSeconds) * 3.6 // x3600s/1000m
return fmt.Sprintf("%.2f", speedKmPerHour)
}
func pace(distanceMeters, durationSeconds int) string {
if durationSeconds == 0 {
return "-"
}
paceSecondsKm := float64(durationSeconds) / (float64(distanceMeters) / 1000.0)
min := int(paceSecondsKm / 60.0)
sec := int(paceSecondsKm) % 60
return fmt.Sprintf("%02d:%02d", min, sec)
}
func eq(a, b interface{}) bool {
return a == b
}
func init() {
r = render.New(render.Options{
// HTML layout to be used for all HTML requests:
HTMLLayout: "application.plush.html",
// Box containing all of the templates:
TemplatesBox: packr.New("app:templates", "../templates"),
AssetsBox: assetsBox,
// Add template helpers here:
Helpers: render.Helpers{
"appShortName": "ROAW",
"appLongName": "Run Once a Week",
"appFullName": "ROAW - Run Once a Week",
"isLoggedIn": isLoggedIn,
"secondsToHuman": SecondsToHuman,
"metersToKm": metersToKm,
"speed": speed,
"pace": pace,
"eq": eq,
"host": App().Options.Host,
// "isActive": func(name string, help plush.HelperContext) string {
// if cr, ok := help.Value("current_path").(string); ok {
// if strings.HasPrefix(cr, name) {
// return "active"
// }
// }
// return "inactive"
// },
// for non-bootstrap form helpers uncomment the lines
// below and import "github.com/gobuffalo/helpers/forms"
// forms.FormKey: forms.Form,
// forms.FormForKey: forms.FormFor,
},
})
}
func isLoggedIn(help plush.HelperContext) bool {
if session, ok := help.Value("session").(*buffalo.Session); ok {
if u := session.Get("current_user_id"); u != nil {
return true
}
}
return false
}