-
Notifications
You must be signed in to change notification settings - Fork 11
/
splash.go
147 lines (128 loc) · 4.15 KB
/
splash.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
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package splash
import (
"fmt"
"time"
"github.com/divVerent/aaaaxy/internal/flag"
)
var (
loadingScreen = flag.Bool("loading_screen", true, "show a detailed loading screen")
)
type Status int
const (
EndFrame Status = iota
Continue
)
// State represents the current splash screen state.
type State struct {
// knownFractions are the known progress bar fractions for each step as loaded.
knownFractions map[string]float64
// started is when the first init step started.
started time.Time
// startTimes are the actual start times of each step.
startTimes map[string]time.Time
// done is created when each step finishes.
done map[string]struct{}
// curStep is the currently executing step.
curStep string
// curFraction is the current progress bar fraction.
curFraction float64
}
// ProvideFractions loads a known fractions map.
// This should have been dumped by a previous run.
func (s *State) ProvideFractions(fractions map[string]float64) {
s.knownFractions = fractions
}
// RunImmediately runs the given status-ish function as a single step.
// Useful for doing stuff w/o an actual loading screen.
func RunImmediately(errPrefix string, f func(s *State) (Status, error)) (Status, error) {
// Simpler implementation that never updates the loading screen and does all init in one frame.
for {
status, err := f(nil)
if err != nil {
return EndFrame, fmt.Errorf("%v: %v", errPrefix, err)
}
if status == EndFrame {
// f did not terminate yet - we need to call it again.
continue
}
return status, nil
}
}
// Enter enters a splash screen section.
// step must be an unique string identifying what is being loaded.
// f is allowed to call Enter too, but must return false, nil if its own Enter calls returned false.
// f must repeat all Enter calls it does, but will never be called again once it returned true.
func (s *State) Enter(step string, errPrefix string, f func(s *State) (Status, error)) (Status, error) {
if !*loadingScreen || s == nil {
return RunImmediately(errPrefix, f)
}
if s.startTimes == nil {
s.startTimes = map[string]time.Time{}
}
if s.done == nil {
s.done = map[string]struct{}{}
}
if _, have := s.done[step]; have {
// Already done!
return Continue, nil
}
if _, have := s.startTimes[step]; !have {
s.startTimes[step] = time.Now()
s.curStep = step
frac := s.knownFractions[step]
if frac > s.curFraction {
s.curFraction = frac
}
// Must force a refresh so the new text actually can show.
// TODO: only do this when the new step may take long?
return EndFrame, nil
}
status, err := f(s)
if err != nil {
return EndFrame, fmt.Errorf("%v: %v", errPrefix, err)
}
if status == EndFrame {
// f did not terminate yet - we need to call it again next frame.
return EndFrame, nil
}
s.done[step] = struct{}{}
return status, nil
}
// Single wraps a simple function into a splash screen step.
func Single(f func() error) func(s *State) (Status, error) {
return func(s *State) (Status, error) {
return Continue, f()
}
}
// Current returns the current progress bar content.
func (s *State) Current() (string, float64) {
return s.curStep, s.curFraction
}
// ToFractions returns the init fraction map by step. This can be provided via ProvideFractions next time.
func (s *State) ToFractions() map[string]float64 {
ended := time.Now()
var started time.Time
for _, v := range s.startTimes {
if started.IsZero() || v.Before(started) {
started = v
}
}
fractions := make(map[string]float64, len(s.startTimes))
for k, v := range s.startTimes {
fractions[k] = float64(v.Sub(started)) / float64(ended.Sub(started))
}
return fractions
}