github.com/fjl/memsize@v0.0.2/memsizeui/template.go (about) 1 package memsizeui 2 3 import ( 4 "html/template" 5 "strconv" 6 "sync" 7 8 "github.com/fjl/memsize" 9 ) 10 11 var ( 12 base *template.Template // the "base" template 13 baseInitOnce sync.Once 14 ) 15 16 func baseInit() { 17 base = template.Must(template.New("base").Parse(`<!DOCTYPE html> 18 <html> 19 <head> 20 <meta charset="UTF-8"> 21 <title>memsize</title> 22 <style> 23 body { 24 font-family: sans-serif; 25 } 26 button, .button { 27 display: inline-block; 28 font-weight: bold; 29 color: black; 30 text-decoration: none; 31 font-size: inherit; 32 padding: 3pt; 33 margin: 3pt; 34 background-color: #eee; 35 border: 1px solid #999; 36 border-radius: 2pt; 37 } 38 form.inline { 39 display: inline-block; 40 } 41 </style> 42 </head> 43 <body> 44 {{template "content" .}} 45 </body> 46 </html>`)) 47 48 base.Funcs(template.FuncMap{ 49 "quote": strconv.Quote, 50 "humansize": memsize.HumanSize, 51 }) 52 53 template.Must(base.New("rootbuttons").Parse(` 54 <a class="button" href="{{$.Link ""}}">Overview</a> 55 {{- range $root := .Roots -}} 56 <form class="inline" method="POST" action="{{$.Link "scan?root=" $root}}"> 57 <button type="submit">Scan {{quote $root}}</button> 58 </form> 59 {{- end -}}`)) 60 } 61 62 func contentTemplate(source string) *template.Template { 63 baseInitOnce.Do(baseInit) 64 t := template.Must(base.Clone()) 65 template.Must(t.New("content").Parse(source)) 66 return t 67 } 68 69 var rootTemplate = contentTemplate(` 70 <h1>Memsize</h1> 71 {{template "rootbuttons" .}} 72 <hr/> 73 <h3>Reports</h3> 74 <ul> 75 {{range .Reports}} 76 <li><a href="{{printf "%d" | $.Link "report/"}}">{{quote .RootName}} @ {{.Date}}</a></li> 77 {{else}} 78 No reports yet, hit a scan button to create one. 79 {{end}} 80 </ul> 81 `) 82 83 var notFoundTemplate = contentTemplate(` 84 <h1>{{.Data}}</h1> 85 {{template "rootbuttons" .}} 86 `) 87 88 var reportTemplate = contentTemplate(` 89 {{- $report := .Data -}} 90 <h1>Memsize Report {{$report.ID}}</h1> 91 <form method="POST" action="{{$.Link "scan?root=" $report.RootName}}"> 92 <a class="button" href="{{$.Link ""}}">Overview</a> 93 <button type="submit">Scan Again</button> 94 </form> 95 <pre> 96 Root: {{quote $report.RootName}} 97 Date: {{$report.Date}} 98 Duration: {{$report.Duration}} 99 Bitmap Size: {{$report.Sizes.BitmapSize | humansize}} 100 Bitmap Utilization: {{$report.Sizes.BitmapUtilization}} 101 </pre> 102 <hr/> 103 <pre> 104 {{$report.Sizes.Report}} 105 </pre> 106 `)