From db0897954e311a80d819fe7e8b56cf19456872df Mon Sep 17 00:00:00 2001 From: gjbae1212 Date: Mon, 17 Feb 2020 16:13:50 +0900 Subject: [PATCH] Apply color table. --- cmd/run.go | 52 ++++++++++++++++++++++++++++++++++++---------- go.mod | 1 + go.sum | 3 +++ search/searcher.go | 2 +- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 18a0187..57785ad 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -2,13 +2,14 @@ package cmd import ( "fmt" - "github.com/c-bata/go-prompt" "os" "strconv" "strings" + "github.com/c-bata/go-prompt" "github.com/fatih/color" "github.com/gjbae1212/findgs/search" + "github.com/olekukonko/tablewriter" "github.com/pkg/browser" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -193,18 +194,47 @@ func executor(t string) { } func showSearchedList() { + // table writer + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"NUM", "SCORE", "NAME", "URL", "TOPIC", "DESCRIPTION"}) + table.SetFooter([]string{"", "", "", "", "TOTAL", fmt.Sprintf("%d", len(foundList))}) + table.SetBorder(false) + table.SetAutoMergeCells(true) + table.SetRowLine(true) + table.SetAlignment(tablewriter.ALIGN_CENTER) + table.SetRowLine(true) + table.SetHeaderColor( + tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor}, + tablewriter.Colors{tablewriter.Bold, tablewriter.BgHiBlueColor}, + tablewriter.Colors{tablewriter.Bold, tablewriter.BgCyanColor}, + tablewriter.Colors{tablewriter.Bold, tablewriter.BgMagentaColor}, + tablewriter.Colors{tablewriter.Bold, tablewriter.BgYellowColor}, + tablewriter.Colors{tablewriter.Bold, tablewriter.BgRedColor}) + table.SetColumnColor( + tablewriter.Colors{tablewriter.Bold}, + tablewriter.Colors{}, + tablewriter.Colors{tablewriter.Bold}, + tablewriter.Colors{}, + tablewriter.Colors{}, + tablewriter.Colors{tablewriter.Bold}) + table.SetFooterColor( + tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{}, + tablewriter.Colors{tablewriter.Bold, tablewriter.BgRedColor, tablewriter.FgWhiteColor}, + tablewriter.Colors{tablewriter.BgGreenColor, tablewriter.FgHiWhiteColor}) + + data := [][]string{} for i, found := range foundList { - sepFmt := color.BlueString("||") - fmt.Printf("%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n", - color.MagentaString("[num]"), color.GreenString("%d", i+1), sepFmt, - color.YellowString("[score]"), color.WhiteString("%f", found.Score), sepFmt, - color.YellowString("[name]"), color.GreenString(found.FullName), sepFmt, - color.YellowString("[url]"), color.WhiteString(found.Url), sepFmt, - color.YellowString("[topic]"), color.WhiteString("%s", found.Topics), sepFmt, - color.YellowString("[desc]"), color.CyanString(found.Description), - ) - fmt.Println() + data = append(data, []string{ + fmt.Sprintf("%d", i+1), + fmt.Sprintf("%f", found.Score), + found.FullName, + found.Url, + fmt.Sprintf("%s", found.Topics), + found.Description, + }) } + table.AppendBulk(data) + table.Render() } func init() { diff --git a/go.mod b/go.mod index b8d0311..27048bd 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/mattn/go-runewidth v0.0.8 // indirect github.com/mattn/go-tty v0.0.3 // indirect github.com/mitchellh/go-homedir v1.1.0 + github.com/olekukonko/tablewriter v0.0.4 github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 // indirect github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237 // indirect diff --git a/go.sum b/go.sum index 8bbb5a8..eee299c 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,7 @@ github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcME github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0= github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-tty v0.0.3 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI= @@ -103,6 +104,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae h1:VeRdUYdCw49yizlSbMEn2SZ+gT+3IUKx8BqxyQdz+BY= github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg= +github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8= +github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/search/searcher.go b/search/searcher.go index b102d1b..dc639e5 100644 --- a/search/searcher.go +++ b/search/searcher.go @@ -102,7 +102,7 @@ func NewSearcher(token string) (Searcher, error) { // make bolt db db, err := bolt.Open(dbPath, os.ModePerm, &bolt.Options{Timeout: 2 * time.Second}) if err != nil { - return nil, fmt.Errorf("[err] NewSearcher fail db %w", err) + return nil, fmt.Errorf("[err] NewSearcher fail db %w.(maybe already running findgs)", err) } // make index