beyondgrep / ack2

**ack 2 is no longer being maintained. ack 3 is the latest version.**
https://github.com/beyondgrep/ack3/
Other
1.48k stars 140 forks source link

Replate tab with an appropriate number of spaces #287

Closed pengyu closed 11 years ago

pengyu commented 11 years ago

The following code demonstrate the indentation of a appears to different (due to the line number column) in the source code and in the ack output.

In test.txt, the first a is indented by 1 level (2 spaces) and the second a is indented by 4 level (a tab = 8 spaces).

But in the ack output, the first a appears to be indented by 2 spaces and the 2nd a appears to be indented by 3 spaces. This may be confusing to the users.

Therefore, I suggest adding an option to ack allowing users to change tab to a given number of spaces.

~/linux/test/gnu/ack-grep/tab$ ./main.sh 
{
  seq 1000
  printf "  a\n"
  printf "\ta\n"
} > test.txt
echo test.txt | ack -x a
test.txt
1001:  a
1002:   a

~/linux/test/gnu/ack-grep/tab$ cat main.sh 
#!/usr/bin/env bash

set -v
{
  seq 1000
  printf "  a\n"
  printf "\ta\n"
} > test.txt
echo test.txt | ack -x a
petdance commented 11 years ago

I don't think that ack should do any manipulation of the output. tab2space can take care of that.

pengyu commented 11 years ago

tab2space removes all the highlights. Also, it is can not take care of what I proposed. See below.

~/linux/test/gnu/ack-grep/tab$ cat main.sh 
#!/usr/bin/env bash

set -v
{
  seq 1000
  printf "  a\n"
  printf "\ta\n"
} > test.txt
echo test.txt | ack -x a | tab2space -t8

~/linux/test/gnu/ack-grep/tab$ ./main.sh 
{
  seq 1000
  printf "  a\n"
  printf "\ta\n"
} > test.txt
echo test.txt | ack -x a | tab2space -t8
test.txt:1001:  a
test.txt:1002:  a
petdance commented 11 years ago

I'm sorry that tab2space doesn't do what you want it to do, but it's not ack's job to manipulate the content that it puts out.

It's not tab2space that eats the highlight. It's that ack doesn't highlight when you pipe the output. You can force it on even when piped with --color.

pengyu commented 11 years ago

It seems that tab2space is OK (compared with the sed command). But why ack has each line prefixed with the filename and lineno when it is piped to tab2space? BTW, how to tell if stdout is a pipe or a file in perl?

~/linux/test/gnu/ack-grep/tab$  ./main.sh 
{
  seq 1000
  printf "  a\n"
  printf "\ta\n"
} > test.txt
cp test.txt test1.txt
printf "%s\n" test.txt test1.txt | ack --color -x a | sed 's/\t/        /g'
test.txt:1001:  a
test.txt:1002:        a
test1.txt:1001:  a
test1.txt:1002:        a
printf "%s\n" test.txt test1.txt | ack --color -x a | tab2space -t8
test.txt:1001:  a
test.txt:1002:        a
test1.txt:1001:  a
test1.txt:1002:       a
printf "%s\n" test.txt test1.txt | ack --color -x a
test.txt
1001:  a
1002:   a

test1.txt
1001:  a
1002:   a

printf "%s\n" test.txt test1.txt | ack -x a | sed 's/\t/        /g'
test.txt:1001:  a
test.txt:1002:        a
test1.txt:1001:  a
test1.txt:1002:        a
printf "%s\n" test.txt test1.txt | ack -x a | tab2space -t8
test.txt:1001:  a
test.txt:1002:  a
test1.txt:1001:  a
test1.txt:1002: a
printf "%s\n" test.txt test1.txt | ack -x a
test.txt
1001:  a
1002:   a

test1.txt
1001:  a
1002:   a
~/linux/test/gnu/ack-grep/tab$ cat main.sh 
#!/usr/bin/env bash

set -v
{
  seq 1000
  printf "  a\n"
  printf "\ta\n"
} > test.txt
cp test.txt test1.txt
printf "%s\n" test.txt test1.txt | ack --color -x a | sed 's/\t/        /g'
printf "%s\n" test.txt test1.txt | ack --color -x a | tab2space -t8
printf "%s\n" test.txt test1.txt | ack --color -x a

printf "%s\n" test.txt test1.txt | ack -x a | sed 's/\t/        /g'
printf "%s\n" test.txt test1.txt | ack -x a | tab2space -t8
printf "%s\n" test.txt test1.txt | ack -x a
petdance commented 11 years ago

But why ack has each line prefixed with the filename and lineno when it is piped to tab2space

Because that's what it does. I'm not going to explain every bit of ack behavior. If you don't want it, then use --no-filename. Reading ack --help should help.

pengyu commented 11 years ago

--no-filename completely disables both the filenames and the line numbers. I don't see an option to get back the line numbers.

You didn't answer why ack has each line prefixed with the filename and lineno when it is piped to tab2space. It is just a tautology. It like answering question "Why did you do A?" by "Because I did A".

hoelzro commented 11 years ago

@pengyu We unfortunately don't have an option to enable line numbers alone (see #196), but I believe the way it works is this: unless you specify otherwise, ack will print filenames and line numbers by default. The only thing that suppresses this is if exactly one regular file argument is provided on the command line as the search target. If ack is writing to a terminal or a pager (by use of --pager or ACK_PAGER), --group is enabled by default; otherwise, --group is off by default.