Skip to content

Commit

Permalink
fix(log): fix some log issue
Browse files Browse the repository at this point in the history
  • Loading branch information
waynezhang committed Jan 2, 2024
1 parent 16419cd commit 13add32
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ module github.com/waynezhang/foto
go 1.18

require (
github.com/chelnak/ysmrr v0.3.0
github.com/disintegration/imaging v1.6.2
github.com/fsnotify/fsnotify v1.7.0
github.com/gookit/color v1.5.4
github.com/mitchellh/mapstructure v1.5.0
github.com/otiai10/copy v1.14.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
github.com/tdewolff/minify/v2 v2.20.10
github.com/theckman/yacspin v0.13.12
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/chelnak/ysmrr v0.3.0 h1:eBm6PvNnjAV920MOEwCzCG3gLn+EFGUf4Go+t+T2/Ps=
github.com/chelnak/ysmrr v0.3.0/go.mod h1:HedVxtqeGSPnSS7qDRtq4vPVGoz8DXcCr9jvjF263rQ=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -26,8 +28,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
Expand All @@ -38,9 +38,6 @@ github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
Expand Down Expand Up @@ -75,8 +72,6 @@ github.com/tdewolff/parse/v2 v2.7.7 h1:V+50eFDH7Piw4IBwH8D8FtYeYbZp3T4SCtIvmBSIM
github.com/tdewolff/parse/v2 v2.7.7/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52 h1:gAQliwn+zJrkjAHVcBEYW/RFvd2St4yYimisvozAYlA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/theckman/yacspin v0.13.12 h1:CdZ57+n0U6JMuh2xqjnjRq5Haj6v1ner2djtLQRzJr4=
github.com/theckman/yacspin v0.13.12/go.mod h1:Rd2+oG2LmQi5f3zC3yeZAOl245z8QOvrH4OPOJNZxLg=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
33 changes: 16 additions & 17 deletions internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package export

import (
"fmt"
"time"

"github.com/theckman/yacspin"
"github.com/chelnak/ysmrr"
"github.com/chelnak/ysmrr/pkg/animations"
"github.com/waynezhang/foto/internal/cache"
"github.com/waynezhang/foto/internal/config"
"github.com/waynezhang/foto/internal/constants"
Expand Down Expand Up @@ -36,33 +36,29 @@ func Export(outputPath string, minimize bool) error {
}

func export(cfg config.Config, outputPath string, minimizer mm.Minimizer, cache cache.Cache, ctx Context) error {
spinner, _ := yacspin.New(yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[14],
Suffix: fmt.Sprintf(" exporting to %s", outputPath),
SuffixAutoColon: true,
StopMessage: " succeed",
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
})
_ = spinner.Start()
sm := ysmrr.NewSpinnerManager(
ysmrr.WithAnimation(animations.Dots),
)
prefixSpinnerMsg := fmt.Sprintf("exporting to %s: ", outputPath)
spinner := sm.AddSpinner(prefixSpinnerMsg)
sm.Start()

spinnerMsg := func(format string, a ...interface{}) {
spinner.Message(fmt.Sprintf(format, a...))
spinner.UpdateMessagef(prefixSpinnerMsg+format, a...)
}

spinnerMsg("Removing directory %s", outputPath)
spinnerMsg("removing directory %s", outputPath)
err := ctx.cleanDirectory(outputPath)
if err != nil {
return err
}

spinnerMsg("Building index %s", outputPath)
spinnerMsg("building index %s", outputPath)
photosDirectory := files.OutputPhotosFilePath(outputPath)
section, err := ctx.buildIndex(cfg)
if err != nil {
ctx.cleanDirectory(outputPath)
utils.CheckFatalError(err, "Failed t build index.")
utils.CheckFatalError(err, "Failed to build index.")
}

ctx.exportPhotos(section, photosDirectory, cache, func(path string) {
Expand All @@ -78,7 +74,10 @@ func export(cfg config.Config, outputPath string, minimizer mm.Minimizer, cache
}
ctx.processOtherFolders(cfg.GetOtherFolders(), outputPath, minimizer, msgFunc)

_ = spinner.Stop()
spinner.UpdateMessagef(prefixSpinnerMsg + "succeeded")

spinner.Complete()
sm.Stop()

return nil
}
Expand Down
38 changes: 29 additions & 9 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,50 @@ package log

import (
"fmt"
"sync"

"github.com/gookit/color"
)

var verbose = false
func Debug(format string, a ...interface{}) { Shared().debug(format, a...) }
func Info(format string, a ...interface{}) { Shared().info(format, a...) }
func Println(format string, a ...interface{}) { Shared().println(format, a...) }
func Fatal(format string, a ...interface{}) { Shared().fatal(format, a...) }

func SetVerbose(v bool) {
verbose = v
func SetVerbose(v bool) { Shared().verbose = v }

type logger struct {
verbose bool
}

var (
once sync.Once
instance logger
)

func Shared() *logger {
once.Do(func() {
instance = logger{verbose: false}
})

return &instance
}

func Debug(format string, a ...interface{}) {
if verbose {
func (l *logger) debug(format string, a ...interface{}) {
if l.verbose {
fmt.Printf(format+"\n", a...)
}
}

func Info(format string, a ...interface{}) {
func (l *logger) info(format string, a ...interface{}) {
color.Info.Printf(format+"\n", a...)
}

func Println(format string, a ...interface{}) {
func (l *logger) println(format string, a ...interface{}) {
fmt.Printf(format+"\n", a...)
}

func Fatal(format string, a ...interface{}) {
color.Printf("<fg=red>ERROR</>: "+format+"\n", a...)
func (l *logger) fatal(format string, a ...interface{}) {
str := fmt.Sprintf(format, a...)
color.Println("<fg=red>ERROR</>: " + str)
}

0 comments on commit 13add32

Please sign in to comment.