# Pagination in Readline

**URL:** <https://www.ruby-forum.com/t/pagination-in-readline/172576>\
**Category:** Ruby\
**Created:** [July 23, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/pagination-in-readline/172576 "2009-07-23T14:34:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [July 23, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/pagination-in-readline/172576/1 "2009-07-23T14:34:33Z")

</div>

Hello All,

I’m writing a Ruby command-line terminal program using Readline, and in  
some cases the output from the commands ran by the user overflows the  
height of the terminal. Thus, I’d like to be able to have pagination in  
the program. Any ideas how to do this?

I found the following blog post  
([http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby](http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby)) that  
describes how to to Git-style pagination in a command-line program, but  
it wasn’t meant for a terminal-style program. I tried switching the  
suggested code around such that I could redirect STDOUT back to the  
original IO and could kill the child process used to print the output,  
but that didn’t work either.

Any ideas on how to do pagination in Readline would be greatly  
appreciated!

–  
Thanks!  
Bryan

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [July 23, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/pagination-in-readline/172576/2 "2009-07-23T20:31:02Z")

</div>

Hi Brian,

> I’m writing a Ruby command-line terminal program using Readline, and in  
> some cases the output from the commands ran by the user overflows the  
> height of the terminal. Thus, I’d like to be able to have pagination in  
> the program. Any ideas how to do this?

I’ve written hirb, [Tagaholic - hirb](http://tagaholic.me/hirb/), which does conditional  
stdout pagination  
among other things. For your particular case you’d do this:

require ‘hirb’  
Hirb::View.enable  
Hirb::View.capture\_and\_render { puts output }

If the output exceeds your console height, it should default to a  
system pager.  
If this doesn’t work you may need to pass your console’s width and  
height explicitly to Hirb:  
Hirb::View.resize(width, height)

If Hirb doesn’t do exactly what you want, you can also just rip out  
the pagination stuff + tweak it.

Gabriel

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [July 23, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/pagination-in-readline/172576/3 "2009-07-23T22:45:30Z")

</div>

Awesome Gabe! This worked PERFECTLY! Thank you so much for pointing  
out  
your project!

–  
Bryan

2009/7/23 cldwalker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![roxah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@roxah](https://www.ruby-forum.com/u/roxah)\
**Post date:** [July 24, 2009, 8:21am UTC](https://www.ruby-forum.com/t/pagination-in-readline/172576/4 "2009-07-24T08:21:09Z")

</div>

On Jul 23, 4:44 pm, Bryan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Note: parts of this message were removed to make it a legal post.]
> 
> Awesome Gabe! This worked PERFECTLY! Thank you so much for pointing out  
> your project!

Glad it worked out 🙂  
Gabriel
