-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtitle.go
More file actions
136 lines (115 loc) · 2.83 KB
/
title.go
File metadata and controls
136 lines (115 loc) · 2.83 KB
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
135
136
package main
import (
"errors"
"fmt"
"time"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
type Title struct {
state *state
Form *huh.Form
}
func (m Title) New(selected int) Title {
username := m.state.GetName()
options := []huh.Option[Screen]{
huh.NewOption(string(PlayScreen), PlayScreen),
huh.NewOption(string(BoardScreen), BoardScreen),
huh.NewOption(string(HelpScreen), HelpScreen),
huh.NewOption(profileKey(*m.state), ProfileScreen),
}
if m.state.GetDone() {
options[0].Key = m.state.styles.Disabled.Render(string(PlayScreen))
if selected == -1 {
options[1] = options[1].Selected(true)
}
}
if selected != -1 {
options[selected] = options[selected].Selected(true)
}
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().Key("name").Value(&username).CharLimit(maxNameLen).Placeholder("what's ur name?").Prompt("? ").Validate(
func(str string) error {
if len(str) == 0 {
return errors.New("what's ur name!?")
}
return nil
},
),
huh.NewSelect[Screen]().
Key("screen").
Options(options...),
),
).WithWidth(formWidth).WithShowHelp(false).WithShowErrors(false).WithTheme(m.state.styles.FormTheme)
if len(username) > 0 {
form.NextField()
}
m.Form = form
return m
}
func (m Title) Init() tea.Cmd {
return tea.Batch(m.Form.Init(), textinput.Blink)
}
func (m Title) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
form, cmd := m.Form.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.Form = f
}
return m, cmd
}
func (m Title) View() string {
errs := m.Form.Errors()
if len(errs) > 0 {
return m.state.styles.FormBox.Render(lipgloss.JoinVertical(0,
m.Form.View(),
m.state.styles.FormError.Render("* "+errs[0].Error()),
))
} else {
return m.state.styles.FormBox.Render(m.Form.View())
}
}
func profileKey(s state) string {
profile, forced := s.ren.ColorProfile(), s.GetForceProfile()
out := ""
if profile == termenv.TrueColor {
out += "on"
} else {
out += "off"
}
if forced {
out += " -f"
}
return "truecolor? " + s.styles.Base.Bold(true).Render(out)
}
func dist() string {
loc, _ := time.LoadLocation("America/New_York")
now := time.Now().In(loc)
next11 := time.Date(now.Year(), now.Month(), now.Day(), 11, 0, 0, 0, loc)
if now.After(next11) {
next11 = next11.Add(24 * time.Hour)
}
diff := next11.Sub(now)
hours := int(diff.Hours())
minutes := int(diff.Minutes()) % 60
seconds := int(diff.Seconds()) % 60
if hours > 0 {
if hours == 1 {
return "1 hour"
}
return fmt.Sprintf("%d hours", hours)
} else if minutes > 0 {
if minutes == 1 {
return "1 minute"
}
return fmt.Sprintf("%d minutes", minutes)
} else {
if seconds == 1 {
return "1 second"
}
return fmt.Sprintf("%d seconds", seconds)
}
}