-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Print Range of Lines #159
Comments
Thank you for your feedback!
I'd like Concerning However there are a few things I would like to discuss:
I did the same thing yesterday 😄. I'd be curious to see why you would like to have a
Could you please open a new ticket to discuss this? |
Sorry. I didn't explain my use case in sufficient detail. I would use bat as a replacement of or component in the preview.sh file. So I would pipe the output of ripgrep to fzf and use a script incorporating bat to print the result of ripgrep with some now prettified context. So in the above image the right hand panel would print the output of bat. That is why I also brought up the idea of highlighting a single line differently as well.
You make a good point. I do still need to look into a combination of other tools. If I find something sufficient for the task I will update here.
In my specific use case I wouldn't need to specify multiple ranges but since the git hunks feature will be doing something so similar it might not hurt to include multiple ranges as an option. As to what it might look like, my first thoughts would be to have one flag and multiple ranges separated by commas. or Have a range separated by a colon. For partial ranges I like the idea of saying And I don't really see a use for single lines at the moment but who am I to judge :)
This is a little muddy but my first thoughts are that the same range(s) would be displayed from every file if possible. |
Ok, sounds good! If you would like to work on this, that would very much appreciated.
That sounds pretty awesome.. I have never thought of that. I'd be interested to see the results, once this works. |
Released in bat 0.5. |
Hi! Edited: alias cat='batcat'
alias head='head_bat'
alias tail='tail_bat'
function head_bat() {
# E.g.
# file01="$HOME/.vimrc"
# head_bat $file01 # with default -n
# head_bat -n 20 $file01
# head_bat -A $file01 # with default -n
# head_bat -n 20 -A $file01 # -A show tabs, spaces, ...
# head_bat -A -n 20 $file01 # WRONG!! '-n' must be 1st flag (if any)
# head_bat $file01 $HOME/.bashrc # RIGHT multiple files
local lines2show=10
while test $# -gt 0; do
# printf "%-15s --> %s\n" "\$1" "$1"
case "$1" in
-n)
# '-n' must be 1st flag (if any)
shift
local lines2show=$1
shift
;;
*)
# call 'bat' passing:
# - '--line-range''s value
# - and rest of current fcn arguments ($@) unaltered: other 'bat' flags, and final filename/s
bat --line-range :$lines2show $@ # E.g.: $ bat --line-range :10 [filename/s]
return 0 # break all (switch, while and current fcn)
shift
;;
esac
done
}
function tail_bat() {
# E.g.
# file01="$HOME/.vimrc"
# tail_bat $file01 # with default -lines
# tail_bat -lines 20 $file01
# tail_bat -A $file01 # with default -lines
# tail_bat -lines 20 -A $file01 # -A show tabs, spaces, ...
# tail_bat -A -lines 20 $file01 # RIGHT '-lines' can be 1st flag (if any)
# tail_bat $file01 $HOME/.bashrc
# tail_bat -lines 20 $file01 $HOME/.bashrc
# tail_bat -lines 20 -A $file01 $HOME/.bashrc
# tail_bat -A -lines 20 $file01 $HOME/.bashrc # RIGHT! '-lines' can be 1st flag (if any)
local arr_orig=("${*}") #copy all params
declare -a arr_files
declare -a arr_flags
count=0
for i in ${arr_orig[@]}; do
if test -f "$i"; then
# file exists
arr_files+=($i)
else
arr_flags+=($i)
fi
((count++))
done
local lines2show=10
local arr_flags_tmp=("${arr_flags[@]}") #copy all params (shift will unset cells)
index=0
for k in ${arr_flags_tmp[@]}; do
# printf "%-15s --> %s\n" "\$1" "$1"
case "$k" in
-lines)
# '-n' must be 1st flag (if any)
unset arr_flags_tmp[$index]
arr_flags_tmp=("${arr_flags_tmp[@]}")
local lines2show=${arr_flags_tmp[$index]}
unset arr_flags_tmp[$index]
arr_flags_tmp=("${arr_flags_tmp[@]}")
;;
*)
((index++))
;;
esac
done
for i in ${arr_files[@]}; do
local file01=${i}
local lines_total=$(wc -l <$file01)
# either lines2show is default or explicit
local var_range_start=$(($lines_total - $lines2show + 1))
local var_range_start=$(($var_range_start < 1 ? 1 : $var_range_start))
# call 'bat' passing:
# - calculated '--line-range'
# - and rest of current fcn arguments ($arr_flags_tmp) unaltered: other 'bat' flags, and filename
bat --line-range $var_range_start: $arr_flags_tmp $file01 # E.g.: $ bat --line-range 990: [filename]
done
}
|
@juanMarinero thank you for sharing your tail_bat implementation. I have made a few tiny improvements: handle #!/usr/bin/env bash
# A tail replacement script that uses bat to output the content
#
# Source: https://github.com/sharkdp/bat/issues/159#issuecomment-754166034
function tail_bat() {
# E.g.
# file01="$HOME/.vimrc"
# tail_bat $file01 # with default -lines
# tail_bat -lines 20 $file01
# tail_bat -A $file01 # with default -lines
# tail_bat -lines 20 -A $file01 # -A show tabs, spaces, ...
# tail_bat -A -lines 20 $file01 # RIGHT '-lines' can be 1st flag (if any)
# tail_bat $file01 $HOME/.bashrc
# tail_bat -lines 20 $file01 $HOME/.bashrc
# tail_bat -lines 20 -A $file01 $HOME/.bashrc
# tail_bat -A -lines 20 $file01 $HOME/.bashrc # RIGHT! '-lines' can be 1st flag (if any)
local arr_orig=("${*}") #copy all params
declare -a arr_files
declare -a arr_flags
count=0
for i in ${arr_orig[@]}; do
if test -f "$i"; then
# file exists
arr_files+=($i)
else
arr_flags+=($i)
fi
((count++))
done
local lines2show=10
local arr_flags_tmp=("${arr_flags[@]}") #copy all params (shift will unset cells)
index=0
for k in ${arr_flags_tmp[@]}; do
# printf "%-15s --> %s\n" "\$1" "$1"
case "$k" in
--lines) ;&
-n)
# '-n' must be 1st flag (if any)
unset arr_flags_tmp[$index]
arr_flags_tmp=("${arr_flags_tmp[@]}")
local lines2show=${arr_flags_tmp[$index]}
unset arr_flags_tmp[$index]
arr_flags_tmp=("${arr_flags_tmp[@]}")
;;
-F) ;&
-f)
/usr/bin/tail "${@}"
exit $?
;;
*)
((index++))
;;
esac
done
for i in ${arr_files[@]}; do
local file01=${i}
local lines_total=$(wc -l <$file01)
# either lines2show is default or explicit
local var_range_start=$(($lines_total - $lines2show + 1))
local var_range_start=$(($var_range_start < 1 ? 1 : $var_range_start))
# call 'bat' passing:
# - calculated '--line-range'
# - and rest of current fcn arguments ($arr_flags_tmp) unaltered: other 'bat' flags, and filename
bat --line-range $var_range_start: $arr_flags_tmp $file01 # E.g.: $ bat --line-range 990: [filename]
done
}
tail_bat "$@" |
I've made a few improvements since my last version: head_batImprovements:
#!/usr/bin/env bash
# A head replacement script that uses bat to output the content
#
# Source: https://github.com/sharkdp/bat/issues/159#issuecomment-754166034
function head_bat() {
# E.g.
# file01="$HOME/.vimrc"
# head_bat $file01 # with default -n
# head_bat -n 20 $file01
# head_bat -A $file01 # with default -n
# head_bat -n 20 -A $file01 # -A show tabs, spaces, ...
# head_bat -A -n 20 $file01 # WRONG!! '-n' must be 1st flag (if any)
# head_bat $file01 $HOME/.bashrc # RIGHT multiple files
#
# Pipe examples:
# command cat $file01 | head_bat # with default -n
# command cat $file01 | head_bat -n 20
# command cat $file01 | head_bat -A # with default -n
local lines2show=10
local bat_args=()
while test $# -gt 0; do
case "$1" in
-n)
# '-n' must be 1st flag (if any)
shift
local lines2show=$1
shift
;;
*)
bat_args+=("$1")
shift
;;
esac
done
# Check if we're receiving piped input
if [ ! -t 0 ]; then
# Handle piped input with any additional bat arguments
command cat - | command bat --line-range ":$lines2show" "${bat_args[@]}"
else
# Handle file arguments
# call 'bat' passing:
# - '--line-range''s value
# - and rest of current fcn arguments stored in bat_args
command bat --line-range ":$lines2show" "${bat_args[@]}" # E.g.: $ bat --line-range :10 [filename/s]
fi
}
head_bat "$@" tail_batImprovements:
#!/usr/bin/env bash
# A tail replacement script that uses bat to output the content
#
# Source: https://github.com/sharkdp/bat/issues/159#issuecomment-754166034
function tail_bat() {
# E.g.
# file01="$HOME/.vimrc"
# tail_bat $file01 # with default -lines
# tail_bat -n 20 $file01
# tail_bat -A $file01 # with default -lines
# tail_bat -n 20 -A $file01 # -A show tabs, spaces, ...
# tail_bat $file01 $HOME/.bashrc
# tail_bat -n 20 $file01 $HOME/.bashrc
# tail_bat -n 20 -A $file01 $HOME/.
#
# Follow mode:
# tail_bat -f $file01
#
# Pipe examples:
# command cat $file01 | tail_bat # with default -lines
# command cat $file01 | tail_bat -n 20
# command cat $file01 | tail_bat -A # with default -lines
local lines2show=10
local arr_flags=()
local arr_files=()
local is_following=false
local original_args=("${@}")
# Process all arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--lines | -n)
shift
lines2show="$1"
shift
;;
-F | -f)
is_following=true
shift
;;
*)
if test -f "$1"; then
arr_files+=("$1")
else
arr_flags+=("$1")
fi
shift
;;
esac
done
# Handle follow mode with grc if available
if [[ "$is_following" = true ]]; then
if command -v grc >/dev/null 2>&1; then
command grc tail "${original_args[@]}"
else
command tail "${original_args[@]}"
fi
exit $?
fi
# Function to process a file and display with bat
function bat_tail_file() {
local file="$1"
local lines_total=$(wc -l < "$file")
local var_range_start=$(($lines_total - $lines2show + 1))
local var_range_start=$(($var_range_start < 1 ? 1 : $var_range_start))
command bat --line-range $var_range_start: "${arr_flags[@]}" "$file"
}
# Check if we're receiving piped input
if [ ! -t 0 ]; then
# Store input in temp file to count lines
local tmpfile=$(mktemp)
command cat - > "$tmpfile"
bat_tail_file "$tmpfile"
rm "$tmpfile"
return
fi
# Process files
for file in "${arr_files[@]}"; do
bat_tail_file "$file"
done
}
tail_bat "$@" |
This might be outside of the scope of bat since one of the projects goals is to be a drop in for cat, but at the same time bat is compared to source-highlight which has the --line-range flag.
The use case I have in mind is using bat as the preview command provided to fzf. (https://github.com/junegunn/fzf#preview-window)
Also along these same lines it would be nice to have the option to highlight a particular line differently than the rest of the syntax highlighting.
If these features are in scope, I wouldn't mind working them myself.
The text was updated successfully, but these errors were encountered: