All Projects → piotrmurach → Tty Pager

piotrmurach / Tty Pager

Licence: mit
Terminal output paging - cross-platform, major ruby interpreters

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Tty Pager

Ruby jard
Just Another Ruby Debugger. Provide a rich Terminal UI that visualizes everything your need, navigates your program with pleasure, stops at matter places only, reduces manual and mental efforts. You can now focus on real debugging.
Stars: ✭ 669 (+1708.11%)
Mutual labels:  terminal, console, tty
Tty Pie
Draw pie charts in your terminal window
Stars: ✭ 125 (+237.84%)
Mutual labels:  terminal, console, ruby-gem
Zui
⬢ Zsh User Interface library – CGI+DHTML-like rapid application development with Zsh
Stars: ✭ 95 (+156.76%)
Mutual labels:  terminal, console, tty
Tty Font
Terminal fonts
Stars: ✭ 44 (+18.92%)
Mutual labels:  terminal, console, tty
tty-reader
A set of methods for processing keyboard input in character, line and multiline modes.
Stars: ✭ 73 (+97.3%)
Mutual labels:  console, ruby-gem, tty
Tty Prompt
A beautiful and powerful interactive command line prompt
Stars: ✭ 1,210 (+3170.27%)
Mutual labels:  terminal, tty, ruby-gem
Xterm.js
A terminal for the web
Stars: ✭ 12,019 (+32383.78%)
Mutual labels:  terminal, console, tty
Gritty
web terminal emulator
Stars: ✭ 63 (+70.27%)
Mutual labels:  terminal, console, tty
tty-editor
Opens a file or text in the user's preferred editor
Stars: ✭ 26 (-29.73%)
Mutual labels:  console, ruby-gem, tty
Finalcut
A text-based widget toolkit
Stars: ✭ 244 (+559.46%)
Mutual labels:  terminal, console, tty
Dte
A small, configurable console text editor (moved to https://gitlab.com/craigbarnes/dte)
Stars: ✭ 98 (+164.86%)
Mutual labels:  terminal, console, tty
Upterm
A terminal emulator for the 21st century.
Stars: ✭ 19,441 (+52443.24%)
Mutual labels:  terminal, console, tty
Galacritty
WIP GTK terminal emulator based on Alacritty
Stars: ✭ 136 (+267.57%)
Mutual labels:  terminal, console, tty
Tty Markdown
Convert a markdown document or text into a terminal friendly output.
Stars: ✭ 275 (+643.24%)
Mutual labels:  terminal, console, tty
Tty Spinner
A terminal spinner for tasks that have non-deterministic time frame.
Stars: ✭ 386 (+943.24%)
Mutual labels:  terminal, tty, ruby-gem
Python Progressbar
Progressbar 2 - A progress bar for Python 2 and Python 3 - "pip install progressbar2"
Stars: ✭ 682 (+1743.24%)
Mutual labels:  terminal, console
Terminaltables
Generate simple tables in terminals from a nested list of strings.
Stars: ✭ 685 (+1751.35%)
Mutual labels:  terminal, console
Ueberzug
ueberzug is a command line util which allows to display images in combination with X11
Stars: ✭ 711 (+1821.62%)
Mutual labels:  terminal, console
Terminus
Bring a real terminal to Sublime Text
Stars: ✭ 967 (+2513.51%)
Mutual labels:  terminal, console
Suplemon
🍋 Console (CLI) text editor with multi cursor support. Suplemon replicates Sublime Text like functionality in the terminal. Try it out, give feedback, fork it!
Stars: ✭ 734 (+1883.78%)
Mutual labels:  terminal, console
TTY Toolkit logo

TTY::Pager Gitter

Gem Version Actions CI Build status Maintainability Coverage Status Inline docs

A cross-platform terminal pager that works on all major Ruby interpreters.

TTY::Pager provides independent terminal pager component for TTY toolkit.

Installation

Add this line to your application's Gemfile:

gem 'tty-pager'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tty-pager

Overview

The TTY::Pager will automatically choose the best available pager on a user's system. Failing to do so, it will fallback on a pure Ruby version that is guaranteed to work with any Ruby interpreter and on any platform.

Contents

1. Usage

The TTY::Pager will pick the best paging mechanism available on your system when initialized:

pager = TTY::Pager.new

Then to start paginating text call the page method with the content as the first argument:

pager.page("Very long text...")

This will launch a pager in the background and wait until the user is done.

Alternatively, you can pass the :path keyword to specify a file path:

pager.page(path: "/path/to/filename.txt")

If instead you'd like to paginate a long-running operation, you could use the block form of the pager:

TTY::Pager.page do |pager|
  File.open("file_with_lots_of_lines.txt", "r").each_line do |line|
    # do some work with the line

    pager.write(line) # send it to the pager
  end
end

After block finishes, the pager is automatically closed.

For more control, you can translate the block form into separate write and close calls:

begin
  pager = TTY::Pager.new

  File.open("file_with_lots_of_lines.txt", "r").each_line do |line|
    # do some work with the line

    pager.write(line) # send it to the pager
  end
rescue TTY::Pager::PagerClosed
  # the user closed the paginating tool
ensure
  pager.close
end

If you want to use a specific pager you can do so by invoking it directly:

pager = TTY::Pager::BasicPager.new
# or
pager = TTY::Pager::SystemPager.new
# or
pager = TTY::Pager::NullPager.new

2. API

2.1 new

The TTY::Pager can be configured during initialization for terminal width, type of prompt when basic pager is invoked, and the pagination command to run.

For example, to disable a pager in CI you could do:

pager = TTY::Pager.new(enabled: false)

2.1.1 :enabled

If you want to disable the paging use the :enabled option set to false:

pager = TTY::Pager.new(enabled: false)

This will directly print all the content to the standard output. If the output isn't a tty device, the pager will return the content directly to the caller.

2.1.2 :command

To force TTY::Pager to always use a specific paging tool(s), use the :command option:

TTY::Pager.new(command: "less -R")

The :command also accepts an array of pagers to use:

pager = TTY::Pager.new(command: ["less -r", "more -r"])

If the provided pager command or commands don't exist on user's system, the pager will fallback automatically on a basic Ruby implementation.

To skip automatic detection of pager and always use a system pager do:

TTY::Pager::SystemPager.new(command: "less -R")

2.1.3 :width

Only the BasicPager allows you to wrap content at given terminal width:

pager = TTY::Pager.new(width: 80)

This option doesn't affect the SystemPager.

To directly use BasicPager do:

pager = TTY::Pager::BasicPager.new(width: 80)

2.1.4 :prompt

To change the BasicPager page break prompt display, use the :prompt option:

prompt = -> (page) { "Page -#{page_num}- Press enter to continue" }
pager = TTY::Pager.new(prompt: prompt)

2.2 page

To start paging use the page method. It can be invoked on an instance or a class.

The class-level page is a convenient shortcut. To page some text you only need to do:

TTY::Pager.page("Some long text...")

You can also include extra initialization parameters. For example, if you prefer to use a specific command do this:

TTY::Pager.page("Some long text...", command: "less -R")

The instance equivalent would be:

pager = TTY::Pager.new(command: "less -R")
pager.page("Some long text...")

Apart from text, you can page file content by passing the :path option:

TTY::Pager.page(path: "/path/to/filename.txt")

The final way is to use the class-level page with a block. After the block is done, the pager is automatically closed. For example, to read a file line by line with additional processing you could do:

TTY::Pager.page do |pager|
  File.foreach("filename.txt") do |line|
    # do some work with the line

    pager.write(line) # write line to the pager
  end
end

The instance equivalent of the block version would be:

pager = TTY::Pager.new
begin
  File.foreach("filename.txt") do |line|
    # do some work with the line

    pager.write(line) # write line to the pager
  end
rescue TTY::Pager::PagerClosed
ensure
  pager.close
end

2.3 write

To stream content to the pager use the write method.

pager.write("Some text")

You can pass in any number of arguments:

pager.write("one", "two", "three")

2.4 try_write

To check if a write has been successful use try_write:

pager.try_write("Some text")
# => true

2.5 puts

To write a line of text and end it with a new line use puts call:

pager.puts("Single line of content")

2.6 close

When you're done streaming content manually use close to finish paging.

All interactions with a pager can raise an exception for various reasons, so wrap your code using the following pattern:

pager = TTY::Pager.new

begin
  # ... perform pager writes
rescue TTY::Pager::PagerClosed
  # the user closed the paginating tool
ensure
  pager.close
end

Alternatively use the class-level page call with a block to automatically close the pager:

TTY::Pager.page do |pager|
  # ... perform pager writes
end

2.7 ENV

By default the SystemPager will check the PAGER environment variable. If the PAGER isn't set, the pager will try one of the searched commands like less, more or pg.

Therefore, if you wish to set your preferred pager you can either set up your shell like so:

PAGER=less -R
export PAGER

Or set PAGER in Ruby script:

ENV["PAGER"]="less -R"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-pager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

  1. Fork it ( https://github.com/piotrmurach/tty-pager/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Code of Conduct

Everyone interacting in the TTY::Pager project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright

Copyright (c) 2015 Piotr Murach. See LICENSE for further details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].