feat: colored text

This commit is contained in:
TaurusXin 2024-09-15 08:59:07 +08:00
parent fb65a59845
commit ddc9862c62
4 changed files with 39 additions and 12 deletions

9
go.mod
View File

@ -3,12 +3,13 @@ module ncmdump
go 1.23.0 go 1.23.0
require github.com/tidwall/gjson v1.17.3 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 ( 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/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/text v0.18.0 // indirect golang.org/x/text v0.18.0 // indirect

2
go.sum
View File

@ -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 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI=
github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY= 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= github.com/frolovo22/tag v0.0.2 h1:gFv5P5nqE7purEipbKT7X/OjP286nx5gA30mjt/4SgA=

18
main.go
View File

@ -2,30 +2,32 @@ package main
import ( import (
"fmt" "fmt"
flag "github.com/spf13/pflag"
"ncmdump/ncmcrypt" "ncmdump/ncmcrypt"
"ncmdump/utils"
"os" "os"
"path/filepath" "path/filepath"
flag "github.com/spf13/pflag"
) )
func processFile(filePath string) error { func processFile(filePath string) error {
currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath) currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath)
if err != nil { 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 return err
} }
dump, err := currentFile.Dump() dump, err := currentFile.Dump()
if err != nil { 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 return err
} }
if dump { if dump {
metadata, _ := currentFile.FixMetadata() metadata, _ := currentFile.FixMetadata()
if !metadata { 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 return err
} }
fmt.Printf("[Done] '%s' -> '%s'\n", filePath, currentFile.GetDumpFilePath()) utils.DonePrintfln("'%s' -> '%s'", filePath, currentFile.GetDumpFilePath())
} }
return nil return nil
} }
@ -56,19 +58,19 @@ func main() {
// check if the folder exists // check if the folder exists
info, err := os.Stat(folderPath) info, err := os.Stat(folderPath)
if err != nil { 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) os.Exit(1)
} }
if !info.IsDir() { if !info.IsDir() {
fmt.Fprintf(os.Stderr, "[Error] '%s' is not a directory", folderPath) utils.ErrorPrintfln("Not a directory: '%s'", folderPath)
os.Exit(1) os.Exit(1)
} }
// dump files in the folder // dump files in the folder
files, err := os.ReadDir(folderPath) files, err := os.ReadDir(folderPath)
if err != nil { 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) os.Exit(1)
} }

22
utils/printf.go Normal file
View File

@ -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...)
}