-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 simultaneously dump results with line numbers to a user-specified log file #101
Comments
At a high level, I think the way people usually solve this problem is by running I'm unlikely to support a You might consider forcing colors to be shown (say, with the |
Thanks. The Before I post process the search log, I get: After removing the ANSI color codes (found this perl one-liner), I get: As we can see there are 2 issues here:
I do that too. But I like have this terminal based flow too.
OK, that is understood. But I hope you can help me with point 1 above regarding the mysterious |
Turns out that the So using just this or this did not help. On examining that
So, eventually, this seems to do the trick of removing all the escape codes: alias rg2 "rg --line-number -p \!* |& tee /tmp/${USER}_rg_temp.txt; \\
\sed -r -e 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' \\
-e 's/\x0F//g' /tmp/${USER}_rg_temp.txt \\
>! ${SEARCHLOG}; \\
\rm -f /tmp/${USER}_rg_temp.txt" You can close this bug report if that Thanks. |
Now I just need a script to convert from
to
|
That |
If you do |
I tried that, but now I get a different extra escape code |
For the sake of completeness, I now 'sort of solve' the original issue in the first post as follows rg still needs to be run twice in some cases. But atleast now I can run something like below
with results from that first rg_wrapper.sh saved to a #!/usr/bin/env bash
# Time-stamp: <2016-12-01 11:47:54 kmodi>
set -euo pipefail # http://redsymbol.net/articles/unofficial-bash-strict-mode
# /home/kmodi/scripts/bash/rg_wrapper.sh: line 81: /home/kmodi/usr_local/6/bin/rg
# --follow --no-ignore-vcs --smart-case --ignore-file /home/kmodi/.ignore: No such
# file or directory
# Uncommenting below gives the above error
# IFS=$'\n\t'
# Initialize variables
debug=0
user_args=''
piped_data=''
input_from_pipe_flag=0
output_to_pipe_flag=0
rg_bin="rg"
if ! hash ${rg_bin} 2>/dev/null # Quit if rg is not found in PATH
then
echo "ERROR: The ripgrep binary (rg) is not installed."
echo "Install from https://github.com/BurntSushi/ripgrep/releases ."
exit 1
fi
# Parse the arguments, mainly to catch the debug flag for this script.
# Rest of the arguments are passed to rg.
while [[ $# -gt 0 ]]
do
case "$1" in
"-D"|"--debug" ) debug=1;;
* ) user_args="${user_args} $1";;
esac
shift # expose next argument
done
if [[ ${debug} -eq 1 ]]
then
echo "* Debug Mode *"
fi
set +e # Ignore if the below which or grep results in error
rg_in_home=$(which -a ${rg_bin} | grep -E '^/home' 2>/dev/null)
set -e
if [[ ${rg_in_home} != "" ]] # If rg binary exists in a dir in user's home
then
rg_bin="${rg_in_home}"
fi
if [[ ${debug} -eq 1 ]]
then
echo "rg_in_home: ${rg_in_home}"
echo "rg_bin : ${rg_bin}"
fi
rg_cmd="${rg_bin} \
--follow \
--no-ignore-vcs \
--smart-case \
--type-add ss:sim.setup* \
--ignore-file /home/${USER}/.ignore"
if [[ ${debug} -eq 1 ]]
then
echo "rg base command: ${rg_cmd}"
fi
# https://gist.github.com/davejamesmiller/1966557
if [[ -t 0 ]] # Script is called normally - Terminal input (keyboard) - interactive
then
# rg_wrapper.sh foo
# rg_wrapper.sh foo | cat -
if [[ ${debug} -eq 1 ]]
then
echo "-- Input from terminal --"
fi
input_from_pipe_flag=0
else # Script is getting input from pipe or file - non-interactive
# echo bar | rg_wrapper.sh foo
# echo bar | rg_wrapper.sh foo | cat -
piped_data="$(cat)"
if [[ ${debug} -eq 1 ]]
then
echo "-- Input from pipe/file --"
fi
input_from_pipe_flag=1
fi
if [[ ${debug} -eq 1 ]]
then
echo "User Args: ${user_args}"
echo "Pipe Args: ${piped_data}"
fi
# http://stackoverflow.com/a/911213/1219634
if [[ -t 1 ]] # Output is going to the terminal
then
# rg_wrapper.sh foo
# echo bar | rg_wrapper.sh foo
if [[ ${debug} -eq 1 ]]
then
echo "-- Output to terminal --"
fi
output_to_pipe_flag=0
else # Output is going to a pipe, file?
# rg_wrapper.sh foo | cat -
# echo bar | rg_wrapper.sh foo | cat -
if [[ ${debug} -eq 1 ]]
then
echo "-- Output to a pipe --"
fi
output_to_pipe_flag=1
fi
# Update the ${SEARCHLOG} using a sub-process, when input is not from a pipe
if [[ -z ${SEARCHLOG+x} ]]
then
export SEARCHLOG="/tmp/${USER}_search.log"
fi
(
if [[ ${input_from_pipe_flag} -eq 0 ]]
then
${rg_cmd} --line-number ${user_args} > ${SEARCHLOG}
fi
)
# Below statements can be optimize to fewer if cases, but they are kept for
# learning purpose.
if [[ ${input_from_pipe_flag} -eq 0 && ${output_to_pipe_flag} -eq 0 ]]
then
# rg_wrapper.sh foo
${rg_cmd} ${user_args}
elif [[ ${input_from_pipe_flag} -eq 0 && ${output_to_pipe_flag} -eq 1 ]]
then
# rg_wrapper.sh foo | cat -
${rg_cmd} ${user_args}
elif [[ ${input_from_pipe_flag} -eq 1 && ${output_to_pipe_flag} -eq 0 ]]
then
# echo foo | rg_wrapper.sh
echo "${piped_data}" | ${rg_cmd} ${user_args}
elif [[ ${input_from_pipe_flag} -eq 1 && ${output_to_pipe_flag} -eq 1 ]]
then
# echo foo | rg_wrapper.sh | cat -
echo "${piped_data}" | ${rg_cmd} ${user_args}
fi |
@kaushalmodi Wow that's insane! I must confess that I don't have time to grok all of that but I'm glad you found something that sort-of works. :-) |
The "sort of works" is just because I still need to run That said, it now works 100% as I expected. Earlier I had: BAD CODE
The issue was that it did not work if I did
|
Hello,
Here is my typical workflow when running any command line search tool.
To speed this up with
rg
, I tried the below but it is inefficient as I need to runrg
twice:PS: While working with above, I realized that
--line-number
is off by default when piping the result to a file.Using above, I would do
I would see the results in color on stdout and also get the log file
${SEARCHLOG}
saved.Then I use
percol
to quickly jump to the result:Here's the alias if interested:
e
is my alias to open by default editor.Instead of running
rg
twice as above, I even tried the below:But that shows the results in black&white on stdout!
Questions:
rg
alias implementation above) in a more efficient way so thatrg
does not need to be run twice?--tee-results-file ${SEARCHLOG}
be feasible to be added so that I see the results in color on stdout (eat the cake_) and also get my${SEARCHLOG}
(have the cake).The text was updated successfully, but these errors were encountered: