Skip to content

Commit

Permalink
Add fuzzy searching for search sub command.
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeexe committed Jul 19, 2021
1 parent f930c95 commit 1392f8c
Show file tree
Hide file tree
Showing 4 changed files with 775 additions and 9 deletions.
50 changes: 48 additions & 2 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ limitations under the License.
package cmd

import (
"fmt"
"os"

"github.com/gookit/color"
fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/yankeexe/air-quality-cli/pkg/aqi"
Expand Down Expand Up @@ -59,14 +61,58 @@ var searchCmd = &cobra.Command{
}

all, _ := cmd.Flags().GetBool("all")
search, _ := cmd.Flags().GetBool("fuzzy")

table := tablewriter.NewWriter(os.Stdout)

utils.CreateTable(*res, table, all)
if search {
selectedStations, err := fuzzySearchList(res.Data)
if err != nil {
color.Warn.Println(err)
os.Exit(0)
}
utils.CreateTable(aqi.Response{Data: *selectedStations}, table, all)
} else {
utils.CreateTable(*res, table, all)
}

table.Render()
},
}

func init() {
rootCmd.AddCommand(searchCmd)
searchCmd.Flags().Bool("all", false, "Show stations even with no station data")
searchCmd.Flags().BoolP("all", "a", false, "air search -a")
searchCmd.Flags().BoolP("fuzzy", "f", false, "air search -s")
}

func fuzzySearchList(response []aqi.Weather) (*[]aqi.Weather, error) {
index, err := fuzzyfinder.FindMulti(response, func(i int) string {
return response[i].Station.Name
},
fuzzyfinder.WithPreviewWindow(func(i, width, height int) string {
if i == -1 {
return ""
}

return fmt.Sprintf(`%s
Station Name: %s
Index: %s`,
"Use <TAB> to select multiple stations",
response[i].Station.Name,
response[i].AirQuality)
}))

if err != nil {
return nil, err
}

selectedItems := []aqi.Weather{}

for _, idx := range index {
selectedItems = append(selectedItems, response[idx])
}

return &selectedItems, nil
}
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ go 1.16
require (
github.com/AlecAivazis/survey/v2 v2.2.14
github.com/gookit/color v1.4.2
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/ktr0731/go-fuzzyfinder v0.4.0
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/smartystreets/assertions v1.0.0 // indirect
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit 1392f8c

Please sign in to comment.