Skip to content

Commit

Permalink
Apply color table.
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbae1212 committed Feb 17, 2020
1 parent a4750c4 commit db08979
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
52 changes: 41 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion search/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db08979

Please sign in to comment.