Last active
February 19, 2023 22:12
-
-
Save rrbutani/c393184da7ad7866d152137f2252044a to your computer and use it in GitHub Desktop.
line lim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upto_n_lines() { | |
local line_lim=${1-20} | |
local char_lim=${2-400} | |
local buffer="" | |
while read -r -n${char_lim} l; do | |
buffer+="${l}" | |
if [[ ${#l} -lt ${char_lim} ]]; then | |
buffer+="\n" | |
fi | |
if [[ ${#l} -eq ${char_lim} ]] || [[ $((line_lim--)) -eq 0 ]]; then | |
local f | |
f="$(mktemp)" | |
echo -en "${buffer}" > "$f" | |
cat - >> "$f" & | |
local p=$! | |
echo "..." | |
echo "<snipped>" | |
echo | |
echo "continued in ${f}" | |
wait $p | |
return | |
fi | |
echo -e "${l}" | |
done | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# https://twitter.com/weeble/status/1492079215445176320 | |
# this kind of approach probably wastes less memory than the tee based approach | |
# below (doesn't have tee keep dumping bytes into a pipe that we stop reading | |
# from) but w/e | |
upto_n_lines() { | |
local line_lim=${1-20} | |
local char_lim=${2-400} | |
local buffer="" | |
while read -r -n${char_lim} l; do | |
buffer+="${l}" | |
if [[ ${#l} -lt ${char_lim} ]]; then | |
buffer+="\n" | |
fi | |
if [[ ${#l} -eq ${char_lim} ]] || [[ $((line_lim--)) -eq 0 ]]; then | |
local f | |
f="$(mktemp)" | |
cat - >> "$f" & | |
local p=$! | |
echo "..." | |
echo "<snipped>" | |
echo | |
echo "continued in ${f}" | |
# echo $x | |
# case $x in | |
# [yY][eE][sS]|[yY]) | |
# less +F "$f"; | |
less "$f"; | |
# break;; | |
# esac | |
# { less +F /dev/fd/2; } 2<&0 | |
# { | |
# echo -en "${buffer}" > "$f" | |
# cat - | |
# } | tee "$f" | less - | |
wait $p | |
return | |
fi | |
echo -e "${l}" | |
done | |
} | |
upto_n_lines_messy() { | |
local line_lim=${1-20} | |
local char_lim=${2-400} | |
local buffer="" | |
while read -r -n${char_lim} l; do | |
buffer+="${l}" | |
if [[ ${#l} -lt ${char_lim} ]]; then | |
buffer+="\n" | |
fi | |
if [[ ${#l} -eq ${char_lim} ]] || [[ $((line_lim--)) -eq 0 ]]; then | |
local f | |
f="$(mktemp)" | |
echo -en "${buffer}" > "$f" | |
cat - >> "$f" & | |
local p=$! | |
echo "..." | |
echo "<snipped>" | |
echo | |
echo "continued in ${f}" | |
# wait $p | |
# cat $f | |
# cat - | { pstree -lsp $$; } | |
# shopt -m | |
# jobs | |
# /bin/ps -eo pid,args | grep "$TAIL_CMD" | grep -v grep | awk '{ print $1 }' | |
# kill -SIGSTOP $last | |
# kill -SIGSTOP $$ | |
# local t=$(tty) | |
local t=s021 | |
# set -x | |
# ps -d | grep "$t" | tee /dev/stderr | cut -d' ' -f1 | while read pid; do | |
# echo $pid | |
# kill -SIGTSTP $pid | |
# done | |
# look for the entry after $$? | |
ps -d | grep "sleep 1337" | tee /dev/stderr | cut -d' ' -f1 | while read pid; do | |
echo $pid | |
kill -SIGSTOP $pid | |
done | |
# ??? how to | |
# suspend | |
return | |
fi | |
echo -e "${l}" | |
done | |
} | |
# we don't handle stdout and stderr separately, sorry | |
# couldn't get this to work; I think you just have to make your own file to route | |
# stuff through | |
upto_n_lines2() { | |
local line_lim=${1-20} | |
local char_lim=${2-400} | |
# tee /dev/fd/2 | |
{ | |
{ | |
tee /dev/fd/5 4<&- | { | |
while read -r -n "${char_lim}" l; do | |
if [[ ${#l} -eq ${char_lim} ]] || [[ ${line_lim} -eq 0 ]]; then | |
local f | |
f="$(mktemp)" | |
cat /dev/fd/4 >"$f" & | |
local p=$! | |
echo "..." | |
echo "<snipped>" | |
echo | |
echo "continued in ${f}" | |
wait $p | |
return | |
fi | |
echo -e "[x] ${l}" | |
((--line_lim)) || : | |
done | |
} 5<&- >&2 | |
} 4<&0 5<&1 <&3 3<&- | |
} 3<>/dev/fd/0 # can't write to stdin, can't reuse this fd, etc. | |
return | |
{ | |
tee /dev/fd/3 | { | |
while read -r -n "${char_lim}" l; do | |
if [[ ${#l} -eq ${char_lim} ]] || [[ ${line_lim} -eq 0 ]]; then | |
local f | |
f="$(mktemp)" | |
cat <&3 >"$f" & | |
local p=$! | |
echo "..." | |
echo "<snipped>" | |
echo | |
echo "continued in ${f}" | |
wait $p | |
return | |
fi | |
echo -e "j ${l}" | |
((--line_lim)) || : | |
done | |
} | |
} 3<&0 | |
return | |
# # duplicate the input to fd 2 (which we'll dup to fd 3 in this scope), in | |
# # case we need to write stuff out to a file | |
# { | |
# tee /dev/fd/5 4<&- | { | |
# cat /dev/fd/4 | |
# # while read -r -n "${char_lim}" l; do | |
# # if [[ ${#l} -eq ${char_lim} ]] || [[ ${line_lim} -eq 0 ]]; then | |
# # local f | |
# # f="$(mktemp)" | |
# # cat <&3 >"$f" & | |
# # local p=$! | |
# # echo "..." | |
# # echo "<snipped>" | |
# # echo | |
# # echo "continued in ${f}" | |
# # wait $p | |
# # return | |
# # fi | |
# # echo -e "${l}" | |
# # ((--line_lim)) || : | |
# # done | |
# } 5>&- | |
# } 3<>/dev/stderr 4<&3 5>&3 3<&- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment