Skip to content

Commit

Permalink
Display number of matches
Browse files Browse the repository at this point in the history
  • Loading branch information
terminationshock committed Oct 29, 2023
1 parent 8ab4f54 commit 3217f02
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,18 @@ func (item *Item) RunCommand() (string, string) {
return RunCommand(item.match)
}

func (list *ItemList) Filter() error {
func (list *ItemList) NumMatches() int {
count := 0
for _, item := range list.items {
if item.HasMatch() {
count++
}
}
return count
}

func (list *ItemList) Filter() error {
count := list.NumMatches()

if count == 0 {
return errors.New("Empty list")
Expand Down Expand Up @@ -136,7 +141,7 @@ func (list *ItemList) Sort() {
}

func (list *ItemList) Get(index int) *Item {
return &list.items[index]
return &list.items[index]
}

func (list *ItemList) Print() {
Expand Down
12 changes: 12 additions & 0 deletions src/items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func TestProcessNoRegexp(t *testing.T) {
if items.items[0].match != "" || items.items[1].match != "" {
t.Error("Incorrect matches")
}
if items.NumMatches() != 0 {
t.Error("Incorrect number of matches")
}
}

func TestProcessRegexp(t *testing.T) {
Expand All @@ -42,6 +45,9 @@ func TestProcessRegexp(t *testing.T) {
if items.items[2].display != "the [::r]match[::-], the mbtch, the mctch" {
t.Error("Incorrect processed line with multiple submatches highlighting the first")
}
if items.NumMatches() != 3 {
t.Error("Incorrect number of matches")
}

config.pattern = regexp.MustCompile("m[a-c]tch")
items = NewItemList(lines)
Expand All @@ -52,6 +58,9 @@ func TestProcessRegexp(t *testing.T) {
if items.items[1].display != "no [::r]match[::-], but the match here and not the match again" {
t.Error("Incorrect processed line with multiple matches highlighting the first")
}
if items.NumMatches() != 3 {
t.Error("Incorrect number of matches")
}

config.pattern = regexp.MustCompile("m[b-c]t(c)h")
items = NewItemList(lines)
Expand All @@ -65,6 +74,9 @@ func TestProcessRegexp(t *testing.T) {
if items.items[2].display != "the match, the mbt[::r]c[::-]h, the mctch" {
t.Error("Incorrect processed line with multiple submatches highlighting the first")
}
if items.NumMatches() != 1 {
t.Error("Incorrect number of matches")
}

config.pattern = regexp.MustCompile("m(at)(c(h))")
items = NewItemList(lines)
Expand Down
11 changes: 9 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func PrintHelp() {
fmt.Println("\nOther keyword OPTIONS:")
fmt.Println("\n --show-output Show the output (both stdout and stderr) of COMMAND")
fmt.Println(" --filter Hide lines without a match")
fmt.Println(" --sort Sort all lines by their matches")
fmt.Println(" --sort Sort lines by their matches")
fmt.Println(" --help Display this help")
fmt.Println("\nExamples:")
fmt.Println("\n git log --oneline | " + os.Args[0] + " \"\\b[0-9a-z]{7,40}\\b\" git show")
Expand Down Expand Up @@ -265,7 +265,14 @@ func (pageList *PageList) setStatus(programExecuted string) {
space := " "

if config.pattern != nil {
info += fmt.Sprintf("%s%s", config.pattern, space)
numMatches := pageList.itemList.NumMatches()
if numMatches > 1 {
info += fmt.Sprintf("%d matches with %s%s", numMatches, config.pattern, space)
} else if numMatches == 1 {
info += fmt.Sprintf("1 match with %s%s", config.pattern, space)
} else {
info += fmt.Sprintf("No match with %s%s", config.pattern, space)
}
}

index := pageList.list.GetCurrentItem()
Expand Down

0 comments on commit 3217f02

Please sign in to comment.