Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ruby command line application with pager?

I'm making a command line tool using Ruby. It will print a lot of text on screen. Currently, I'm using shell pipeline (may_app | more) to do so. But I think it's better to has a default pager.

It's just like what you see when execute git log . One can disable pager by using git --nopager log.

I've done quite much google work and find one gem: hirb , but it seems a little overkill.

After many tries, I'm current using shell wrapper to do so:

#!/bin/bash

# xray.rb is the core script
# doing the main logic and will
# output many rows of text on 
# screen
XRAY=$HOME/fdev-xray/xray.rb

if [ "--nopager" == "$1" ]; then
    shift
    $XRAY $*
else
    $XRAY $* | more
fi

It works. But is there a better way?

like image 787
qhwa Avatar asked Dec 13 '11 12:12

qhwa


1 Answers

You are doing it right. But instead using more you'd better get a pager from $PAGER environment variable, if any.

Some people prefer less to more for example, and others have their favorite parser options set in this var.

like image 124
zed_0xff Avatar answered Oct 14 '22 08:10

zed_0xff