format.go (2714B)
1 package report 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.dwrz.net/src/pkg/color" 8 ) 9 10 func (r *Report) Format() string { 11 var str strings.Builder 12 13 fmt.Fprintf(&str, "%sEntries%s\n", color.BrightBlack, color.Reset) 14 fmt.Fprintf(&str, "Count: %d\n", len(r.Entries)) 15 if len(r.Entries) > 0 { 16 fmt.Fprintf( 17 &str, 18 "First: %s\n", 19 r.Entries[0].Date.Format("2006-01-02"), 20 ) 21 fmt.Fprintf( 22 &str, 23 "Latest: %s\n", 24 r.Entries[len(r.Entries)-1].Date.Format("2006-01-02"), 25 ) 26 str.WriteString("\n") 27 } 28 29 if len(r.BodyFat.Values) > 0 { 30 fmt.Fprintf( 31 &str, 32 "%sBody Fat%s\n", color.BrightBlack, color.Reset, 33 ) 34 fmt.Fprintf( 35 &str, 36 "Max: %.2f on %v\n", 37 r.BodyFat.Max.Value, 38 r.BodyFat.Max.Date.Format("2006-01-02"), 39 ) 40 fmt.Fprintf( 41 &str, 42 "Min: %.2f on %v\n", 43 r.BodyFat.Min.Value, 44 r.BodyFat.Min.Date.Format("2006-01-02"), 45 ) 46 fmt.Fprintf(&str, "Average: %.2f\n", *r.BodyFat.Average) 47 fmt.Fprintf(&str, "Median: %.2f\n", *r.BodyFat.Median) 48 str.WriteString("\n") 49 } 50 51 if len(r.DQS.Values) > 0 { 52 fmt.Fprintf(&str, "%sDQS%s\n", color.BrightBlack, color.Reset) 53 fmt.Fprintf( 54 &str, 55 "Max: %.2f on %v\n", 56 r.DQS.Max.Value, 57 r.DQS.Max.Date.Format("2006-01-02"), 58 ) 59 fmt.Fprintf( 60 &str, 61 "Min: %.2f on %v\n", 62 r.DQS.Min.Value, 63 r.DQS.Min.Date.Format("2006-01-02"), 64 ) 65 fmt.Fprintf(&str, "Average: %.2f\n", *r.DQS.Average) 66 fmt.Fprintf(&str, "Median: %.2f\n", *r.DQS.Median) 67 str.WriteString("\n") 68 } 69 70 if len(r.Weight.Values) > 0 { 71 fmt.Fprintf( 72 &str, "%sWeight%s\n", color.BrightBlack, color.Reset, 73 ) 74 fmt.Fprintf( 75 &str, 76 "Max: %.2f on %v\n", 77 r.Weight.Max.Value, 78 r.Weight.Max.Date.Format("2006-01-02"), 79 ) 80 fmt.Fprintf( 81 &str, 82 "Min: %.2f on %v\n", 83 r.Weight.Min.Value, 84 r.Weight.Min.Date.Format("2006-01-02"), 85 ) 86 fmt.Fprintf(&str, "Average: %.2f\n", *r.Weight.Average) 87 fmt.Fprintf(&str, "Median: %.2f\n", *r.Weight.Median) 88 str.WriteString("\n") 89 } 90 91 if len(r.More) > 0 || len(r.Less) > 0 { 92 fmt.Fprintf( 93 &str, 94 "%sRecommendations%s\n", color.BrightBlack, color.Reset, 95 ) 96 } 97 if len(r.More) > 0 { 98 fmt.Fprintf(&str, "You should eat more:\n\n") 99 for i := 0; i < 3 && i < len(r.More); i++ { 100 rec := r.More[i] 101 fmt.Fprintf( 102 &str, 103 "%s: %d lost points (%.2f per entry).\n", 104 rec.Name, 105 rec.Points, 106 float64(rec.Points)/float64(len(r.Entries)), 107 ) 108 } 109 } 110 str.WriteString("\n") 111 if len(r.Less) > 0 { 112 fmt.Fprintf(&str, "You should eat less:\n") 113 for i := 0; i < 2 && i < len(r.Less); i++ { 114 rec := r.Less[i] 115 fmt.Fprintf( 116 &str, 117 "%s: %d lost points (%.2f per entry).\n", 118 rec.Name, 119 rec.Points, 120 float64(rec.Points)/float64(len(r.Entries)), 121 ) 122 } 123 } 124 125 return str.String() 126 }