print.go 2.0 KB
Newer Older
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
1 2 3 4 5
package logUtils

import (
	"encoding/json"
	"fmt"
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
6 7
	commonUtils "github.com/easysoft/zendata/src/utils/common"
	fileUtils "github.com/easysoft/zendata/src/utils/file"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
8
	"github.com/easysoft/zendata/src/utils/vari"
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
9 10 11 12 13 14 15
	"github.com/fatih/color"
	"os"
	"regexp"
	"strings"
)

var (
aaronchen2k2k's avatar
aaronchen2k2k 已提交
16
	exampleFile  = fmt.Sprintf("res%sdoc%ssample.yaml", string(os.PathSeparator), string(os.PathSeparator))
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
17 18 19
	usageFile  = fmt.Sprintf("res%sdoc%susage.txt", string(os.PathSeparator), string(os.PathSeparator))
)

aaronchen2k2k's avatar
aaronchen2k2k 已提交
20
func PrintExample() {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
21 22 23 24 25
	if vari.Config.Language == "en" {
		exampleFile = strings.Replace(exampleFile, ".yaml", "_en.yaml", -1)
		usageFile = strings.Replace(usageFile, ".txt", "_en.txt", -1)
	}

aaronchen2k2k's avatar
aaronchen2k2k 已提交
26 27 28 29
	content := fileUtils.ReadResData(exampleFile)
	fmt.Printf("%s\n", content)
}

aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
30
func PrintUsage() {
aaronchen2k2k's avatar
aaronchen2k2k 已提交
31 32 33 34
	if vari.Config.Language == "en" {
		exampleFile = strings.Replace(exampleFile, ".yaml", "_en.yaml", -1)
		usageFile = strings.Replace(usageFile, ".txt", "_en.txt", -1)
	}
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
35 36

	usage := fileUtils.ReadResData(usageFile)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
37
	exeFile := "zd"
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
38 39 40
	if commonUtils.IsWin() {
		exeFile += ".exe"
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
41

aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
42 43
	if !commonUtils.IsWin() {
		regx, _ := regexp.Compile(`\\`)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
44
		usage = regx.ReplaceAllString(usage, "/")
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
45

aaronchen2k2k's avatar
aaronchen2k2k 已提交
46 47
		regx, _ = regexp.Compile(`zd.exe`)
		usage = regx.ReplaceAllString(usage, "zd")
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
48

aaronchen2k2k's avatar
aaronchen2k2k 已提交
49 50
		regx, _ = regexp.Compile(`d:`)
		usage = regx.ReplaceAllString(usage, "/home/user")
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
51
	}
aaronchen2k2k's avatar
aaronchen2k2k 已提交
52
	fmt.Printf("%s\n", usage)
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

func PrintTo(str string) {
	output := color.Output
	fmt.Fprint(output, str+"\n")
}

func PrintToWithColor(msg string, attr color.Attribute) {
	output := color.Output

	if attr == -1 {
		fmt.Fprint(output, msg+"\n")
	} else {
		color.New(attr).Fprintf(output, msg+"\n")
	}
}

func PrintToCmd(msg string, attr color.Attribute) {
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
71
	output := color.Output
aaronchen2k2k's avatar
init  
aaronchen2k2k 已提交
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

	if attr == -1 {
		fmt.Fprint(output, msg+"\n")
	} else {
		clr := color.New(attr)
		clr.Fprint(output, msg+"\n")
	}
}

func PrintUnicode(str []byte) {
	var a interface{}

	temp := strings.Replace(string(str), "\\\\", "\\", -1)

	err := json.Unmarshal([]byte(temp), &a)

	var msg string
	if err == nil {
		msg = fmt.Sprint(a)
	} else {
		msg = temp
	}

	PrintToCmd(msg, -1)
}