Skip to content

Commit

Permalink
add support for specifying multiple search terms
Browse files Browse the repository at this point in the history
  • Loading branch information
prnvbn committed Oct 19, 2024
1 parent a0f0e4d commit b81bf31
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
26 changes: 4 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ var (

cfg ui.AppConfig
rootCmd = &cobra.Command{
Use: "clocks [FUZZY_CLOCK_NAME?]",
Use: "clocks [FUZZY_CLOCK_NAME]+",
Short: "A tool to display time across multiple timezones.",
PersistentPreRunE: loadConfig,
Args: cobra.MaximumNArgs(1),
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if clocksAbsent() {
return
Expand All @@ -53,40 +53,22 @@ var (
if twelveHr {
cfg.TwelveHour = true
}

if len(args) >= 1 {
// when a @search term is passed, set layout to horizontal
// so that all clocks are displayed in one row
// clocks are filtered by the search term
// fuzzy search is used to match the search term
searchTerm := args[0]
cfg.Layout.LayoutType = ui.Horizontal

n := 0
cfg.ClockCfgs, n = cfg.ClockCfgs.Filter(searchTerm)
if n == 0 {
pterm.FgYellow.Println("No clocks match the search term:", searchTerm)
return
}
}

if len(args) >= 1 {
// when a @search term is passed, set layout to horizontal
// so that all clocks are displayed in one row
// clocks are filtered by the search term
// fuzzy search is used to match the search term
searchTerm := args[0]
cfg.Layout.LayoutType = ui.Horizontal

n := 0
cfg.ClockCfgs, n = cfg.ClockCfgs.Filter(searchTerm)
cfg.ClockCfgs, n = cfg.ClockCfgs.Filter(args...)
if n == 0 {
pterm.FgYellow.Println("No clocks match the search term:", searchTerm)
pterm.FgYellow.Printf("No clocks match the search terms: %#v", args)
return
}
}


ui.ShowClocks(cfg)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.1
0.8.0
12 changes: 8 additions & 4 deletions internal/ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"slices"
"strings"


"github.com/prnvbn/clocks/internal/match"
"github.com/prnvbn/clocks/internal/tmz"
)
Expand Down Expand Up @@ -98,9 +97,14 @@ func (s *SortedClockConfigs) Remove(toRemove ...ClockConfig) {
})
}

func (s SortedClockConfigs) Filter(searchTerm string) (filtered SortedClockConfigs, n int) {
filtered = slices.Collect(s.fuzzyFiltered(searchTerm))
n = len(filtered)
func (s SortedClockConfigs) Filter(searchTerms ...string) (filtered SortedClockConfigs, n int) {
filtered = make(SortedClockConfigs, 0)

for _, st := range searchTerms {
x := slices.Collect(s.fuzzyFiltered(st))
filtered = append(filtered, x...)
n += len(filtered)
}
return
}

Expand Down

0 comments on commit b81bf31

Please sign in to comment.