Created
May 11, 2012 17:48
-
-
Save barn/2661319 to your computer and use it in GitHub Desktop.
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/ruby | |
# | |
require 'pp' | |
term_lines=`tput lines`.to_i | |
# tput doesn't exist. | |
if $? == 127 | |
exec "cat -" | |
end | |
input = ARGF.read( 65536 ) | |
exit if input.nil? | |
lines_of_input = input.split( "\n" ) | |
num_lines = lines_of_input.count | |
# So if the lines are longer than the size of the term, then bump up the number | |
# of lines too. | |
lines_of_input.each do |x| | |
num_lines += x.size / `tput cols`.to_i | |
end | |
if num_lines > term_lines | |
begin | |
IO.popen( "less -re" , "w" ) do |less| | |
less.puts "" | |
less.puts lines_of_input.join( "\n" ) | |
end | |
rescue Errno::EPIPE => e | |
exit | |
rescue Errno::EINVAL => e | |
exit | |
end | |
else | |
puts input | |
end | |
__END__ | |
if [ ${#input} -gt $LINES ] | |
then | |
( for x in "${input[@]}"; do | |
echo "$x" | |
done ) | less -re | |
# echo "${input[@]}" | less -re | |
else | |
echo "${input[@]}" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment