/
title.go
106 lines (91 loc) · 3.66 KB
/
title.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
package title
import (
"fmt"
"strings"
"time"
)
// Title represents a title that may be sent to the player. The title will show up as large text in the middle
// of the screen, with optional subtitle and action text.
type Title struct {
text, subtitle, actionText string
fadeInDuration, fadeOutDuration, duration time.Duration
}
// New returns a new title using the text passed. The text is formatted according to the formatting rules of
// fmt.Sprintln, but with no newline at the end.
// The title has default durations set, which will generally suffice.
func New(text ...any) Title {
return Title{
text: format(text),
fadeInDuration: time.Second / 20,
fadeOutDuration: time.Second / 20,
duration: time.Second * 2,
}
}
// Text returns the text of the title, as passed to New when created.
func (title Title) Text() string {
return title.text
}
// WithSubtitle sets the subtitle of the title. The text passed will be formatted according to the formatting
// rules of fmt.Sprintln, but without the newline.
// The subtitle is shown under the title in a somewhat smaller font.
// The new Title with the subtitle is returned.
func (title Title) WithSubtitle(text ...any) Title {
title.subtitle = format(text)
return title
}
// Subtitle returns the subtitle of the title, as passed to SetSubtitle. Subtitle returns an empty string if
// no subtitle was previously set.
func (title Title) Subtitle() string {
return title.subtitle
}
// WithActionText sets the action text of the title. This text is roughly the same as sending a tip/popup, but
// will synchronise with the title.
// SetActionText will format the text passed using the formatting rules of fmt.Sprintln, but without newline.
// The new Title with the action text is returned.
func (title Title) WithActionText(text ...any) Title {
title.actionText = format(text)
return title
}
// ActionText returns the action text added to the title. This text is roughly the same as sending a tip, but
// will synchronise with the title. By default, the action text is empty.
func (title Title) ActionText() string {
return title.actionText
}
// Duration returns the duration that the title will be visible for, without fading in or out. By default,
// this is two seconds.
func (title Title) Duration() time.Duration {
return title.duration
}
// WithDuration sets the duration that the title will be visible for without fading in or fading out.
// The new Title with the duration is returned.
func (title Title) WithDuration(d time.Duration) Title {
title.duration = d
return title
}
// WithFadeInDuration sets the duration that the title takes to fade in on the screen.
// The new Title with the fade-in duration is returned.
func (title Title) WithFadeInDuration(d time.Duration) Title {
title.fadeInDuration = d
return title
}
// FadeInDuration returns the duration that the fade-in of the title takes. By default, this is a quarter of
// a second.
func (title Title) FadeInDuration() time.Duration {
return title.fadeInDuration
}
// WithFadeOutDuration sets the duration that the title takes to fade out of the screen.
// The new Title with the fade-out duration is returned.
func (title Title) WithFadeOutDuration(d time.Duration) Title {
title.fadeOutDuration = d
return title
}
// FadeOutDuration returns the duration that the fade-out of the title takes.By default, this is a quarter of
// a second.
func (title *Title) FadeOutDuration() time.Duration {
return title.fadeOutDuration
}
// format is a utility function to format a list of values to have spaces between them, but no newline at the
// end.
func format(a []any) string {
return strings.TrimSuffix(strings.TrimSuffix(fmt.Sprintln(a...), "\n"), "\n")
}