This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.
Last Updated: 2024-11-21
Say I want to hard-code that my program's output is to be paged via the more
command. (i.e. do this in Ruby etc. instead of ad-hoc on the command line)
Then I need to:
tty
. Why? Because there is
no point in using a pager if you are feeding this program's output into another program
that humans do not need to read (e.g. via a pipe
)$stdout
to be the pipe, such that calling puts
sends data to pager pipeif $stdout.tty?
# Take stock of this way of opening a write pipe!
pager = open("|more", "w")
# Be careful to close pipe
at_exit { pager.close }
$stdout = pager
end
puts text