* Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
*
* 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 progress
import (
"fmt"
"os"
"strings"
"time"
)
const (
defaultRepeatCount = 9
defaultSleepTime = 300
FailedDone = "fail"
)
type Progress struct {
Begin time.Time
DoingMessage string
RepeatCount int
DoneChan chan struct{}
}
func CreateAndDoing(doingMessage string) *Progress {
progress := &Progress{
Begin: time.Now(),
RepeatCount: defaultRepeatCount,
DoingMessage: doingMessage,
DoneChan: make(chan struct{}),
}
go progress.doing()
return progress
}
func (p *Progress) doing() {
for i := 1; ; i++ {
select {
case _, ok := <-p.DoneChan:
if !ok {
return
}
default:
dynamicMessage := strings.Repeat(".", i)
if i == p.RepeatCount {
dynamicMessage = strings.Repeat(" ", i)
i = 0
}
if _, err := fmt.Fprintf(os.Stdout, "%s %s\r", p.DoingMessage, dynamicMessage); err != nil {
continue
}
time.Sleep(time.Millisecond * defaultSleepTime)
}
}
}
func (p *Progress) Done(doneMessage string) {
if doneMessage != FailedDone {
elapsed := time.Since(p.Begin)
fmt.Printf("%s costs time %dms\n", doneMessage, elapsed.Milliseconds())
}
close(p.DoneChan)
}