From d1e0cd76b691659f26982de2ec2fd83fb44a4a56 Mon Sep 17 00:00:00 2001 From: Pranav Bansal Date: Sat, 14 Sep 2024 18:25:20 +0100 Subject: [PATCH] add fuzzy show feature --- cmd/root.go | 20 ++++++++++++++++++-- go.mod | 2 +- internal/ui/clocks.go | 1 - internal/ui/config.go | 13 +++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 8676d87..338891f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,9 +29,10 @@ var ( cfg ui.AppConfig rootCmd = &cobra.Command{ - Use: "clocks", + Use: "clocks [CLOCK_NAME?]", Short: "A tool to display time across multiple timezones.", PersistentPreRunE: loadConfig, + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { if clocksAbsent() { return @@ -52,6 +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 + } + } ui.ShowClocks(cfg) }, @@ -152,7 +169,6 @@ func addFlags(cmd *cobra.Command) { cmd.Flags().BoolVarP(&live, "live", "l", false, "keeps clocks on screen") cmd.Flags().BoolVarP(&seconds, "seconds", "s", false, "shows seconds as well") cmd.Flags().BoolVar(&twelveHr, "t12", false, "print dates in 12 hour format") - } func fatal(err error, fmt string, args ...any) { diff --git a/go.mod b/go.mod index f730636..6ee6455 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.3 require ( github.com/adrg/xdg v0.4.0 + github.com/lithammer/fuzzysearch v1.1.8 github.com/pkg/errors v0.9.1 github.com/pterm/pterm v0.12.75 github.com/rs/zerolog v1.31.0 @@ -21,7 +22,6 @@ require ( github.com/gookit/color v1.5.4 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kr/text v0.2.0 // indirect - github.com/lithammer/fuzzysearch v1.1.8 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect diff --git a/internal/ui/clocks.go b/internal/ui/clocks.go index a77be10..e69102f 100644 --- a/internal/ui/clocks.go +++ b/internal/ui/clocks.go @@ -42,7 +42,6 @@ func ShowClocks(appCfg AppConfig) { } time.Sleep(time.Second) - } } diff --git a/internal/ui/config.go b/internal/ui/config.go index 22d5389..620738d 100644 --- a/internal/ui/config.go +++ b/internal/ui/config.go @@ -4,7 +4,9 @@ import ( "encoding/json" "fmt" "slices" + "strings" + "github.com/lithammer/fuzzysearch/fuzzy" "github.com/prnvbn/clocks/internal/tmz" ) @@ -93,3 +95,14 @@ func (s *SortedClockConfigs) Remove(toRemove ...ClockConfig) { return slices.Contains(toRemove, cfg) }) } + +func (s SortedClockConfigs) Filter(searchTerm string) (filtered SortedClockConfigs, n int) { + filtered = make(SortedClockConfigs, 0, len(s)) + for _, clockCfg := range s { + if fuzzy.Match(strings.ToLower(searchTerm), strings.ToLower(clockCfg.Heading)) { + filtered = append(filtered, clockCfg) + n++ + } + } + return +}