FelipeLema/emacs-counsel-gtags

color codes not getting filtered

jgarvin opened this issue · 4 comments

Originally posted this on syohex repo before I realized yours is what package actually got installed by use-package ;)

I assume gtags or global are producing output with colors in it, because when I try to find defintions I'm seeing this:

`(1/1) Find Definition: self_only_test

�[30;43mself_only_test�[0m�[K`

Then when I hit enter to pick the definition global complains "invalid regular expression" probably because of those brackets.

I checked my environment and I don't have any GREP_COLORS set (global --help mentions it respects this var).

I checked with C-h v for plausible looking vars for customizing the gtags/global commands but didn't see any.

My global version is a little old, 6.5.6.

@jgarvin

Author
jgarvin commented 23 hours ago
After digging though counsel-gtags.el I saw it was using grep, so I tried:

(setq counsul-grep-command "grep --color=never")

Which did the trick. I also discovered my bashrc sets an alias to enable color for grep -- but that would only be an issue if counsel runs the commands inside a shell, not sure if it does.

@jgarvin

Author
jgarvin commented 15 minutes ago
...Strangely on emacs restart this came back, not sure why. Now these have no effect on the problem:

(setq grep-find-command "grep --color=never")
(setq counsul-grep-command "grep --color=never")

a little bit more digging:

(defun counsel-gtags--get-grep-command ()
  "Get a grep command to be used to filter candidates.
Returns a command without arguments.
Otherwise, returns nil if couldn't find any."
  (cl-loop
   for command in (list grep-command "rg" "ag" "grep")
   for actual-command = (and command
			     (let ((command-no-args (car
						     (split-string command))))
			       (executable-find command-no-args)))
   while (not actual-command)
   finally return actual-command))

The problem appears to come from "ag" being in the list. Removing it fixes the problem. ag --help says it uses colors by default. I tried replacing "ag" with "ag --nocolor" but that didn't fix it. Only removing ag from the list all together did the trick.

Maybe there should be a variable to customize for setting the exact grep command?

And now I come full circle to realizing what I did before that went away on emacs restart. You already have grep-command in the list, so this is the fix:

(setq grep-command "grep --color=never")

I've added the --color=never parameter in af1ecbc , although I couldn't reproduce this problem (and, thus, add an unit test).

Anyone reading this: feel free do a PR for a unit test for this.

Ergus commented

I think that this issue can be closed now. Please confirm