-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (113 loc) · 2.6 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
var (
cooking = []string{"煮", "煎", "炒", "烤"}
breakfast = []string{"玉米", "鸡蛋", "燕麦", "吐司"}
// lunch / dinner
meat = []string{"鸡胸肉", "鸡腿肉", "牛排", "金枪鱼", "三文鱼", "虾", "鸡蛋"}
vegetable = []string{"黄瓜", "彩椒", "胡萝卜", "香菇", "花椰菜", "秋葵", "甘蓝"}
carbohydrate = []string{"土豆", "红薯", "麦片", "玉米", "紫薯", "意面"}
fruit = []string{"圣女果", "香蕉", "苹果", "西柚", "牛油果", "火龙果"}
)
const templateToday = `~~~ [%s] ~~~~
[BREAKFAST] %s
[LUNCH] %s
[DINNER] %s
`
const templateTodayMeal = "%s => %s + %s + %s + %s"
type (
Meal struct {
cooking, meat, vegetable, carbohydrate, fruit string
}
OneDay struct {
Time time.Time
DateValue int
Breakfast string
Lunch, Dinner *Meal
}
)
func main() {
if len(os.Args) < 3 {
OneDayMeal()
} else if os.Args[2] == "s" || os.Args[2] == "schedule" {
OneWeekSchedule()
} else if os.Args[2] == "help" {
fmt.Println("s, OneWeekSchedule 生成一周食谱")
os.Exit(0)
} else {
OneDayMeal()
}
}
func OneDayMeal() {
day := NewOneDayNow()
PrintOneDay(day)
}
func PrintOneDay(day *OneDay) {
fmt.Printf(
templateToday,
day.Time.Format("2006 - 01 - 02 - Monday"),
day.Breakfast,
FormatMeal(day.Lunch),
FormatMeal(day.Dinner),
)
}
func FormatMeal(meal *Meal) string {
return fmt.Sprintf(
templateTodayMeal,
meal.cooking,
meal.meat,
meal.vegetable,
meal.carbohydrate,
meal.fruit,
)
}
func NewOneDayNow() *OneDay {
return NewOneDay(time.Now())
}
func NewOneDay(day time.Time) *OneDay {
date := RandSeed(day)
return &OneDay{
Time: day,
DateValue: date,
Breakfast: NewBreakFast(int64(date<<1 + 1)),
Lunch: NewMeal(int64(date<<1 + 2)),
Dinner: NewMeal(int64(date<<1 + 3)),
}
}
func NewBreakFast(day int64) string {
rand.Seed(day)
return random(breakfast)
}
func NewMeal(day int64) *Meal {
rand.Seed(day)
return &Meal{
cooking: random(cooking),
meat: random(meat),
vegetable: random(vegetable),
carbohydrate: random(carbohydrate),
fruit: random(fruit),
}
}
func random(slice []string) string {
return slice[rand.Intn(len(slice))]
}
func OneWeekSchedule() {
now := time.Now()
week := now.Weekday()
for i := time.Sunday; i <= time.Saturday; i++ {
offset := int(i - week)
offsetDate := now.AddDate(0, 0, offset)
day := NewOneDay(offsetDate)
PrintOneDay(day)
}
}
func RandSeed(now time.Time) int {
value, _ := strconv.Atoi(now.Format("20060102"))
return value
}