diff --git a/README-zh.md b/README-zh.md index 40565c3..daccd1c 100644 --- a/README-zh.md +++ b/README-zh.md @@ -93,13 +93,15 @@ $ red-tldr upgrade index-update = false github-update = false path = "" + color = true ``` | 配置项 | 描述 | 类型 | | ----------- | ----------- | ----------- | | index-update| 数据库索引文件是否自动更新 | Bool | | github-update | 数据库文件是否从github自动更新 | Bool | -| path | 数据库文件存放路径 | string | +| path | 数据库文件存放路径 | String | +| color | 高亮输出命令 | Bool | diff --git a/README.md b/README.md index a37cc50..4f561f9 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ Default configuration file path:`HOMEDIR/.red-tldr/config.toml` index-update = false github-update = false path = "" + color = true ``` @@ -100,7 +101,8 @@ Default configuration file path:`HOMEDIR/.red-tldr/config.toml` | ----------- | ----------- | ----------- | | index-update| Whether the database index file is automatically updated | Bool | | github-update | Whether the database file is automatically updated from github | Bool | -| path | Database file storage path | string | +| path | Database file storage path | String | +| color | Print highlight command | Bool | ## Contributing diff --git a/config/config.toml b/config/config.toml index 44c6e6a..dcf6e0a 100644 --- a/config/config.toml +++ b/config/config.toml @@ -2,3 +2,4 @@ index-update = false github-update = false path = "" + color = true diff --git a/go.mod b/go.mod index 62639dd..db62a2b 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,7 @@ module red-tldr go 1.16 require ( - github.com/google/go-github v17.0.0+incompatible - github.com/google/go-querystring v1.1.0 // indirect + github.com/gookit/color v1.4.2 github.com/spf13/viper v1.8.1 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index e0093fa..bc03db4 100644 --- a/go.sum +++ b/go.sum @@ -115,12 +115,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= -github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= -github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= -github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -140,6 +135,8 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4 github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gookit/color v1.4.2 h1:tXy44JFSFkKnELV6WaMo/lLfu/meqITX3iAV52do7lk= +github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= @@ -233,6 +230,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/pkg/search.go b/pkg/search.go index 171ce2c..e4749e1 100644 --- a/pkg/search.go +++ b/pkg/search.go @@ -3,12 +3,14 @@ package pkg import ( "encoding/json" "fmt" + "github.com/spf13/viper" yaml "gopkg.in/yaml.v2" "io/ioutil" "log" "os" "red-tldr/utils" "strings" + "github.com/gookit/color" ) var ( @@ -16,6 +18,16 @@ var ( SearchDbName = "" ) +func init() { + viper.SetConfigName("config") + viper.SetConfigType("toml") + viper.AddConfigPath(utils.GetConfigPath()) + err := viper.ReadInConfig() + if err != nil { + utils.GenerateConfig() + } +} + func SetDbDir() { SearchDbDir = utils.GetDatabasePath() + string(os.PathSeparator) SearchDbName = utils.GetDatabaseFilePath() @@ -65,16 +77,46 @@ func Search(file string,keyword string) { } } +func ShowHighLightData(Data * SearchDataStruct){ + if viper.GetBool("red-tldr.color"){ + // Color + color.Style{color.Red, color.OpBold}.Println(Data.Name) + lines := strings.Split(Data.Data,"\n") + printColor := false + for _,line := range lines{ + if strings.HasPrefix(line,"```"){ + if printColor { + printColor = false + continue + }else{ + fmt.Println() + printColor = true + continue + } + } + if printColor { + color.Style{color.Green, color.OpBold}.Println(line) + // color.Green.Println(line) + continue + } + fmt.Println(line) + } + }else{ + fmt.Println("=================") + fmt.Println(Data.Name) + fmt.Println("=================") + fmt.Println(Data.Data) + return + } +} + func ShowDetails(file SearchResultStruct) { Data := new(SearchDataStruct) yamlFile, err := ioutil.ReadFile(SearchDbDir+ utils.GetPathSeparator() + file.Filename) utils.CheckErrorOnExit(err) err = yaml.Unmarshal(yamlFile, Data) utils.CheckErrorOnExit(err) - fmt.Println("=================") - fmt.Println(Data.Name) - fmt.Println("=================") - fmt.Println(Data.Data) + ShowHighLightData(Data) } func SelectOneResult(fileList []SearchResultStruct) { diff --git a/utils/common.go b/utils/common.go index 1822d81..fbf5418 100644 --- a/utils/common.go +++ b/utils/common.go @@ -105,7 +105,7 @@ func GenerateConfig() { viper.SetDefault("red-tldr.path",GetDatabasePath()) viper.SetDefault("red-tldr.index-update",false) viper.SetDefault("red-tldr.github-update",false) - + viper.SetDefault("red-tldr.color",true) CheckErrorOnExit(viper.WriteConfigAs(GetConfigFilePath())) log.Println("[Generate Config Success, Config File Path : ", GetConfigFilePath() ,"]")