But please don't build a custom pager, but use $PAGER (unless this is supposed to turn into a full browser ...) so it works as expected (key bindings etc )
Indeed and it would be so much better to use termcap (eg through ncurses) instead of blasting ANSI codes without checking :) some of us still use terminals