Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to quickly filter out files from counsel-ag #1512

Closed
narendraj9 opened this issue Mar 28, 2018 · 6 comments
Closed

Ability to quickly filter out files from counsel-ag #1512

narendraj9 opened this issue Mar 28, 2018 · 6 comments

Comments

@narendraj9
Copy link

@narendraj9 narendraj9 commented Mar 28, 2018

Hi,

A lot of times I am working on a Clojure project that also has tests written in Python. While searching with counsel-ag, it also includes files in the Python virtualenv folder. It would be great if it was possible to quickly exclude or fix the pattern for files while searching with counsel-ag.

Thanks :)

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Mar 28, 2018

This is related to a few already open issues. The general solution to this will be the ability to specify, in addition to input, the shell command flags to counsel-ag, counsel-rg etc.

@basil-conto
Copy link
Collaborator

@basil-conto basil-conto commented Mar 28, 2018

The general solution to this will be the ability to specify, in addition to input, the shell command flags to counsel-ag, counsel-rg etc.

Note that this is already possible for these two commands by way of specifying a prefix argument.

@abo-abo
Copy link
Owner

@abo-abo abo-abo commented Mar 28, 2018

Note that this is already possible for these two commands by way of specifying a prefix argument.

True, but there was a request to be able to do this dynamically, i.e. you specify both the regex and the cmd flags as input. I think that would be a good solution.

@sadhu89
Copy link

@sadhu89 sadhu89 commented Jul 27, 2018

Is there any alternative solution for this?

@basil-conto
Copy link
Collaborator

@basil-conto basil-conto commented Jul 27, 2018

@sadhu89 An alternative to what?

Currently the only supported means of specifying extra ag options is by calling counsel-ag with a prefix argument. #1559 adds the ability to specify options along with the search term, during completion. See also #1408.

@jixiuf
Copy link
Contributor

@jixiuf jixiuf commented Jul 28, 2018

I use counsel-rg and with this script
I can use "foo !bar " to filter the result

(setq counsel-rg-base-command  "~/.emacs.d/bin/rgwrapper -z %s ")

package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

var (
	params = []string{"-S", "--no-heading", "--line-number", "--color", "never"}
)

var first = true
var notMatchList []string
var matchList []string

func main() {

	args := []string{}
	for _, arg := range os.Args[1:] {
		arg = strings.Replace(arg, ".*?", " ", -1)
		arg = strings.Replace(arg, "\\(", "quote_prefix", -1)
		arg = strings.Replace(arg, "\\)", "quote_suffix", -1)
		arg = strings.Replace(arg, "(", "", -1)
		arg = strings.Replace(arg, ")", "", -1)
		arg = strings.Replace(arg, "quote_prefix", "\\(", -1)
		arg = strings.Replace(arg, "quote_suffix", "\\)", -1)
		tokens := strings.Split(arg, " ")
		for _, token := range tokens {
			args = append(args, token)
		}
	}

	for _, arg := range args {

		if strings.HasPrefix(arg, "!") {
			if arg != "!" {
				notMatchList = append(notMatchList, arg[1:])
			}

		} else if strings.HasPrefix(arg, "-") {
			params = append(params, arg)
		} else {
			if first {
				params = append(params, arg)
				params = append(params, ".")
				first = false
			} else {
				matchList = append(matchList, arg)
			}
		}
	}
	cmd := exec.Command("rg", params...)
	data, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(string(data), err)
		return
	}

	lines := strings.Split(string(data), "\n")
	lines = filterNotMatch(filter(lines))
	for _, line := range lines {
		fmt.Println(line)
	}
}

func filter(lines []string) (result []string) {
	for _, line := range lines {
		skip := false
		for _, key := range matchList {
			if !strings.Contains(line, key) {
				skip = true
				continue
			}
		}
		if !skip {
			result = append(result, line)

		}

	}
	return
}

func filterNotMatch(lines []string) (result []string) {
	for _, line := range lines {
		skip := false
		for _, key := range notMatchList {
			if strings.Contains(line, key) {
				skip = true
				continue
			}
		}
		if !skip {
			result = append(result, line)

		}

	}
	return
}

DEADB17 pushed a commit to DEADB17/swiper that referenced this issue Aug 4, 2018
@abo-abo abo-abo closed this in 826bb5e Aug 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants