From ddc9862c62b2ce4f3a4ff330bf59fc14c6328f99 Mon Sep 17 00:00:00 2001 From: TaurusXin Date: Sun, 15 Sep 2024 08:59:07 +0800 Subject: [PATCH] feat: colored text --- go.mod | 9 +++++---- go.sum | 2 ++ main.go | 18 ++++++++++-------- utils/printf.go | 22 ++++++++++++++++++++++ 4 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 utils/printf.go diff --git a/go.mod b/go.mod index 9c226e0..8349eff 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,13 @@ module ncmdump go 1.23.0 require github.com/tidwall/gjson v1.17.3 +require github.com/go-flac/go-flac v1.0.0 +require github.com/spf13/pflag v1.0.5 +require github.com/bogem/id3v2/v2 v2.1.4 +require github.com/go-flac/flacpicture v0.3.0 +require github.com/TwiN/go-color v1.4.1 require ( - github.com/bogem/id3v2/v2 v2.1.4 // indirect - github.com/go-flac/flacpicture v0.3.0 // indirect - github.com/go-flac/go-flac v1.0.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect golang.org/x/text v0.18.0 // indirect diff --git a/go.sum b/go.sum index 89fbfd2..5d2431d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/TwiN/go-color v1.4.1 h1:mqG0P/KBgHKVqmtL5ye7K0/Gr4l6hTksPgTgMk3mUzc= +github.com/TwiN/go-color v1.4.1/go.mod h1:WcPf/jtiW95WBIsEeY1Lc/b8aaWoiqQpu5cf8WFxu+s= github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI= github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY= github.com/frolovo22/tag v0.0.2 h1:gFv5P5nqE7purEipbKT7X/OjP286nx5gA30mjt/4SgA= diff --git a/main.go b/main.go index ed573e0..a9be6e7 100644 --- a/main.go +++ b/main.go @@ -2,30 +2,32 @@ package main import ( "fmt" - flag "github.com/spf13/pflag" "ncmdump/ncmcrypt" + "ncmdump/utils" "os" "path/filepath" + + flag "github.com/spf13/pflag" ) func processFile(filePath string) error { currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath) if err != nil { - fmt.Fprintf(os.Stderr, "[Error] Reading '%s' failed: '%s'\n", filePath, err.Error()) + utils.ErrorPrintfln("Reading '%s' failed: '%s'", filePath, err.Error()) return err } dump, err := currentFile.Dump() if err != nil { - fmt.Fprintf(os.Stderr, "[Error] Processing '%s' failed: '%s'\n", filePath, err.Error()) + utils.ErrorPrintfln("Processing '%s' failed: '%s'", filePath, err.Error()) return err } if dump { metadata, _ := currentFile.FixMetadata() if !metadata { - fmt.Fprintf(os.Stderr, "[Warning] Fix metadata for '%s' failed: '%s'\n", filePath, err.Error()) + utils.WarningPrintfln("Fix metadata for '%s' failed: '%s'", filePath, err.Error()) return err } - fmt.Printf("[Done] '%s' -> '%s'\n", filePath, currentFile.GetDumpFilePath()) + utils.DonePrintfln("'%s' -> '%s'", filePath, currentFile.GetDumpFilePath()) } return nil } @@ -56,19 +58,19 @@ func main() { // check if the folder exists info, err := os.Stat(folderPath) if err != nil { - fmt.Fprintf(os.Stderr, "[Error] Unable to access directory '%s'", folderPath) + utils.ErrorPrintfln("Unable to access directory: '%s'", folderPath) os.Exit(1) } if !info.IsDir() { - fmt.Fprintf(os.Stderr, "[Error] '%s' is not a directory", folderPath) + utils.ErrorPrintfln("Not a directory: '%s'", folderPath) os.Exit(1) } // dump files in the folder files, err := os.ReadDir(folderPath) if err != nil { - fmt.Fprintf(os.Stderr, "[Error] Unable to read directory '%s'", folderPath) + utils.ErrorPrintfln("Unable to read directory: '%s'", folderPath) os.Exit(1) } diff --git a/utils/printf.go b/utils/printf.go new file mode 100644 index 0000000..bb4e82c --- /dev/null +++ b/utils/printf.go @@ -0,0 +1,22 @@ +package utils + +import ( + "fmt" + "github.com/TwiN/go-color" +) + +func DonePrintfln(format string, a ...interface{}) { + fmt.Printf(color.InBold(color.InGreen("[Done] "))+format+"\n", a...) +} + +func InfoPrintfln(format string, a ...interface{}) { + fmt.Printf(color.InBold(color.InBlue("[Info] "))+format+"\n", a...) +} + +func WarningPrintfln(format string, a ...interface{}) { + fmt.Printf(color.InBold(color.InYellow("[Warning] "))+format+"\n", a...) +} + +func ErrorPrintfln(format string, a ...interface{}) { + fmt.Printf(color.InBold(color.InRed("[Error] "))+format+"\n", a...) +}