Skip to content

Commit

Permalink
Add fuzzy usability improvemnts (#7)
Browse files Browse the repository at this point in the history
- add fuzzy usability improvements
- bump go version to 1.23
  • Loading branch information
prnvbn authored Sep 14, 2024
1 parent d1e0cd7 commit 47acb13
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 14 deletions.
9 changes: 7 additions & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import (

// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Use: "add [FUZZY_COUNTRY_NAME?]",
Short: "Add one or more clock",
PostRunE: saveConfig,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
oldNumClocks := len(cfg.ClockCfgs)
clockCfgs := ui.SelectClocks()
searchTerm := ""
if len(args) >= 1 {
searchTerm = args[0]
}
clockCfgs := ui.SelectClocks(searchTerm)
cfg.ClockCfgs.Add(clockCfgs...)

newNumClocks := len(cfg.ClockCfgs)
Expand Down
19 changes: 18 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (

cfg ui.AppConfig
rootCmd = &cobra.Command{
Use: "clocks [CLOCK_NAME?]",
Use: "clocks [FUZZY_CLOCK_NAME?]",
Short: "A tool to display time across multiple timezones.",
PersistentPreRunE: loadConfig,
Args: cobra.MaximumNArgs(1),
Expand Down Expand Up @@ -70,6 +70,23 @@ var (
}
}

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)
},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/prnvbn/clocks

go 1.21.3
go 1.23.1

require (
github.com/adrg/xdg v0.4.0
Expand Down
11 changes: 11 additions & 0 deletions internal/match/fuzzy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package match

import (
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"
)

func Fuzzy(searchTerm string, toMatch string) bool {
return fuzzy.Match(strings.ToLower(searchTerm), strings.ToLower(toMatch))
}
20 changes: 20 additions & 0 deletions internal/tmz/filtered.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tmz

import (
"iter"

"github.com/prnvbn/clocks/internal/match"
)

func FilteredCountries(searchTerm string) iter.Seq[string] {
return func(yield func(string) bool) {
for cntry := range CountryZonesMap {
if match.Fuzzy(searchTerm, cntry) {
if !yield(cntry) {
return
}
}
}
}

}
23 changes: 16 additions & 7 deletions internal/ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package ui
import (
"encoding/json"
"fmt"
"iter"
"slices"
"strings"

"github.com/lithammer/fuzzysearch/fuzzy"

"github.com/prnvbn/clocks/internal/match"
"github.com/prnvbn/clocks/internal/tmz"
)

Expand Down Expand Up @@ -97,12 +99,19 @@ func (s *SortedClockConfigs) Remove(toRemove ...ClockConfig) {
}

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++
filtered = slices.Collect(s.fuzzyFiltered(searchTerm))
n = len(filtered)
return
}

func (s SortedClockConfigs) fuzzyFiltered(searchTerm string) iter.Seq[ClockConfig] {
return func(yield func(ClockConfig) bool) {
for _, clockCfg := range s {
if match.Fuzzy(strings.ToLower(searchTerm), strings.ToLower(clockCfg.Heading)) {
if !yield(clockCfg) {
return
}
}
}
}
return
}
6 changes: 3 additions & 3 deletions internal/ui/select_clocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (

"github.com/prnvbn/clocks/internal/tmz"
"github.com/pterm/pterm"
"golang.org/x/exp/maps"
"golang.org/x/term"
)

const (
minMaxHeight = 5
)

func SelectClocks() []ClockConfig {
cntries := maps.Keys(tmz.CountryZonesMap)
func SelectClocks(searchTerm string) []ClockConfig {
cntries := slices.Collect(tmz.FilteredCountries(searchTerm))

slices.Sort(cntries)

cntryMenuHeading := pterm.ThemeDefault.PrimaryStyle.Sprint("Please select countries; you can select the timezones after this ") + pterm.ThemeDefault.SecondaryStyle.Sprint("[type to search]")
Expand Down

0 comments on commit 47acb13

Please sign in to comment.