From abbf3bd7577106ca2b252fa1ec96da4ae70d3880 Mon Sep 17 00:00:00 2001 From: Lucas dos Santos Abreu Date: Fri, 16 Feb 2024 09:33:27 -0300 Subject: [PATCH] releasve: v0.48.1 --- CHANGELOG.md | 9 ++++++++- pkg/cmd/time-entry/in/in_test.go | 4 ++-- pkg/search/find.go | 3 ++- pkg/ui/ui.go | 11 +---------- strhlp/strhlp.go | 21 +++++++++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 261a805a..49d2e684 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [v0.48.1] - 2024-02-16 + +### Fixed + +- match how strings are compared when using `allow-name-for-id` and filtering on interactive mode. + ## [v0.48.0] - 2024-02-16 ### Added @@ -1142,7 +1148,8 @@ time entry. - Golang CLI using [cobra](https://github.com/spf13/cobra) - Makefile to help setup actions -[Unreleased]: https://github.com/lucassabreu/clockify-cli/compare/v0.48.0...HEAD +[Unreleased]: https://github.com/lucassabreu/clockify-cli/compare/v0.48.1...HEAD +[v0.48.1]: https://github.com/lucassabreu/clockify-cli/releases/tag/v0.48.1 [v0.48.0]: https://github.com/lucassabreu/clockify-cli/releases/tag/v0.48.0 [v0.47.0]: https://github.com/lucassabreu/clockify-cli/releases/tag/v0.47.0 [v0.46.0]: https://github.com/lucassabreu/clockify-cli/releases/tag/v0.46.0 diff --git a/pkg/cmd/time-entry/in/in_test.go b/pkg/cmd/time-entry/in/in_test.go index 2490b52d..2efb37d0 100644 --- a/pkg/cmd/time-entry/in/in_test.go +++ b/pkg/cmd/time-entry/in/in_test.go @@ -232,7 +232,7 @@ func TestNewCmdIn_ShouldLookupProject_WithAndWithoutClient(t *testing.T) { }, { name: "project and client's name", - args: []string{"-s=08:00", "-p", "second me"}, + args: []string{"-s=08:00", "-p", "sec me"}, param: api.CreateTimeEntryParam{ Workspace: w.ID, Start: defaultStart, @@ -241,7 +241,7 @@ func TestNewCmdIn_ShouldLookupProject_WithAndWithoutClient(t *testing.T) { }, { name: "project and client's name (other)", - args: []string{"-s=08:00", "-p=second clockify"}, + args: []string{"-s=08:00", "-p", "sec cloc"}, param: api.CreateTimeEntryParam{ Workspace: w.ID, Start: defaultStart, diff --git a/pkg/search/find.go b/pkg/search/find.go index 82e196c5..bf42adf4 100644 --- a/pkg/search/find.go +++ b/pkg/search/find.go @@ -26,12 +26,13 @@ func findByName( return r, err } + isSimilar := strhlp.IsSimilar(name) for _, e := range l { if strings.ToLower(e.GetID()) == name { return e.GetID(), nil } - if strings.Contains(strhlp.Normalize(e.GetName()), name) { + if isSimilar(e.GetName()) { return e.GetID(), nil } } diff --git a/pkg/ui/ui.go b/pkg/ui/ui.go index 52e628c6..74634e66 100644 --- a/pkg/ui/ui.go +++ b/pkg/ui/ui.go @@ -3,9 +3,7 @@ package ui import ( "fmt" "io" - "regexp" "strconv" - "strings" "time" "github.com/AlecAivazis/survey/v2" @@ -78,14 +76,7 @@ func (u *ui) SetPageSize(p uint) UI { } func selectFilter(filter, value string, _ int) bool { - // skipcq: GO-C4007 - filter = regexp.MustCompile(`[\]\^\\\,\.\(\)\-]+`). - ReplaceAllString(strhlp.Normalize(filter), " ") - filter = regexp.MustCompile(`\s+`).ReplaceAllString(filter, " ") - filter = strings.ReplaceAll(filter, " ", ".*") - filter = strings.ReplaceAll(filter, "*", ".*") - - return regexp.MustCompile(filter).MatchString(strhlp.Normalize(value)) + return strhlp.IsSimilar(filter)(value) } func askString(p survey.Prompt, options ...survey.AskOpt) (string, error) { diff --git a/strhlp/strhlp.go b/strhlp/strhlp.go index 484b3284..fce68d2c 100644 --- a/strhlp/strhlp.go +++ b/strhlp/strhlp.go @@ -1,6 +1,7 @@ package strhlp import ( + "regexp" "strings" "unicode" @@ -97,3 +98,23 @@ func PadSpace(s string, size int) string { } return s } + +// IsSimilar will convert the string into a regex and return a function the +// checks if a second string is similar to it. +// +// Both strings will normalized before mathing and any space on the filter +// string will be taken as .* on a regex +func IsSimilar(filter string) func(string) bool { + // skipcq: GO-C4007 + filter = regexp.MustCompile(`[\]\^\\\,\.\(\)\-]+`). + ReplaceAllString(Normalize(filter), " ") + filter = regexp.MustCompile(`\s+`).ReplaceAllString(filter, " ") + filter = strings.ReplaceAll(filter, " ", ".*") + filter = strings.ReplaceAll(filter, "*", ".*") + + r := regexp.MustCompile(filter) + + return func(s string) bool { + return r.MatchString(Normalize(s)) + } +}