Giter VIP home page Giter VIP logo

pypager's Introduction

pypager

Build Status Latest Version License

A $PAGER in pure Python

pip install pypager

Pypager can be used as a stand-alone application, or as a library.

What is a pager?

A pager is a terminal program that can be used to view the content of a file, or the output stream from another application.

For instance, when we run man vim, the actual content is displayed in a pager, according to the $PAGER environment variable.

Important for a pager is that the input can be streamed. For instance when we execute find / | pypager; we don't want to wait for the whole file system to be traversed, before displaying anything. Data is only read from the input pipe, when it needs to be displayed.

Popular pager applications are more, less and most.

Features

  • Highlighting of text [0].
  • Searching.
  • Many key bindings from less are implemented.

[0] (It understands the output of man pages, ANSI escape codes and further, it can use Pygments to highlight about any file.)

Usage

# Install it.
pip install pypager

# Tell the environment to use this pager. Put the following line in
# ~/.bashrc if you like.
export PAGER=pypager

# Following commands, and many others should pick up the pager.
man vim
git diff

# View a file, using this pager.
pypager some_source_code.py

As a library

from pypager.source import GeneratorSource
from pypager.pager import Pager


def generate_a_lot_of_content():
    """
    This is a function that generates content on the fly.
    It's called when the pager needs to display more content.

    This should yield prompt_toolkit `(style_string, text)` tuples.
    """
    counter = 0
    while True:
        yield [("", 'line: %i\n' % counter)]
        counter += 1


if __name__ == '__main__':
    p = Pager()
    p.add_source(GeneratorSource(generate_a_lot_of_content())
    p.run()

pypager's People

Contributors

bobotig avatar jonathanslenders avatar mauricesvp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pypager's Issues

paging a string

I have a big multiline string. Can I page it without a generator?

Pager sources argument

As the "sources" Pager argument has been removed here, either the docstring of the class and the example in the README should be updated, or the argument should be reintroduced (It would be interesting to know why it was removed in the first place?).

Character "w" cannot be used in search mode

Character "w" cannot be used in search mode.
Upon entering the character in search mode, it fails to appear on the screen.

Issue appears to be resolved after changing line 306 of key_bindings.py
from: @handle("w")
to: @handle("w", filter=default_focus)

Pypager Not Showing UTF-8 characters.

I've been working on a little project that I can parse the name, chapter or chapters and verses or no verses of the bible, and I am using clint to color my text, but When I pipe it to pypager the "çèéêôâãõ" characters of my source are not even being shown.
image
On the Image where it shows Gnesis it should be Gênesis.
If you want to try yourself to see if I'm being just dumb here's the repo where this project is: https://github.com/Werner1201/PyBible
I think I don't need to say to create a virtual env and download the requirements, since you're much more experienced than me. If you help me with that I'd be grateful.

Spurious characters printed on terminal

When quitting from pypager, sometimes I see characters like these on terminal:

112;26M35;111;26M35;110;26M35;109;26M35;107;27M35;105;27M35;103;27M35;102;28M35;100;28M35;99;29M35;98;29M35;96;29M
^[[<35;56;35M^[[<35;56;

I cannot reproduce this. It happens randomly.

On Ubuntu using xfce4-terminal 0.8.10-1.

Viewing Large Files

Hi,

First just wanted to say great job on this app. The code here as well as in prompt-toolkit is exactly the sort of idiomatic code I thought must be out there somewhere, and I'm glad I put off my less rewrite until I found this. There's only one thing missing from your pager that I need from less; viewing large files.

Less, when you press G, does a seek to the end of the file. Then it tries to calculate line numbers for you if enabled, but you can CTRL+C to stop that and just show the last screenful of the file. Reading the code, it seems that it keeps a sort of linked list of loaded portions of the file, for quick jumping around, e.g. G gg is near instant no-matter the size of the file. Additionally, pressing G takes you to the end of the file. In pypager, I've found that pressing G takes me to some point deep in the file, I assume when a read-timeout has finished, and pressing G again takes me deeper still, but not yet to the end. From reading the code I haven't seen any special approach for handling large files, it seems the file is just treated as one big text buffer. I'd like to implement low-resource reading of large files (Unix-only for me), and I was wondering if you had any thoughts on where to get started or gotchas that make it exceedingly difficult. I'm thinking I will start with trying a simple mmapped file, so that G does in fact go to the end of file, then see how searching performance works. I think mmap's caching will be enough to get a 90% improvement. Then if that's not good enough I can look at the more advanced semantic caching that less does, plus my own scheme that I think less should do but it doesn't.

TypeError: <lambda>() missing 1 required positional argument: 'strike'

$  pip install pypager
Collecting pypager
  Downloading pypager-3.0.0-py3-none-any.whl (18 kB)
Requirement already satisfied: pygments in /usr/local/lib/python3.9/site-packages (from pypager) (2.10.0)
Collecting prompt-toolkit<3.1.0,>=3.0.0
  Downloading prompt_toolkit-3.0.20-py3-none-any.whl (370 kB)
     |████████████████████████████████| 370 kB 2.1 MB/s 
Requirement already satisfied: wcwidth in /usr/local/lib/python3.9/site-packages (from prompt-toolkit<3.1.0,>=3.0.0->pypager) (0.2.5)
Installing collected packages: prompt-toolkit, pypager
Successfully installed prompt-toolkit-3.0.20 pypager-3.0.0
$ pypager 1
Traceback (most recent call last):
  File "/usr/local/bin/pypager", line 8, in <module>
    sys.exit(run())
  File "/usr/local/lib/python3.9/site-packages/pypager/entry_points/run_pypager.py", line 49, in run
    pager.add_source(FileSource(filename, lexer=lexer))
  File "/usr/local/lib/python3.9/site-packages/pypager/source.py", line 327, in __init__
    super().__init__(self.fp.fileno(), lexer=lexer, name=filename)
  File "/usr/local/lib/python3.9/site-packages/pypager/source.py", line 86, in __init__
    self._attrs = Attrs(
TypeError: <lambda>() missing 1 required positional argument: 'strike'

Used to stream the status of fabric?

Hello!,

It isn't an issue but a question,

I need to execute some Fabric commands in one web app and show the results of the execution in the Dashboard (Web), it is working but I need to wait until subprocess.check_output finish to have the result back.

I would like to streaming the output of Fabric as soon as it is printed in the subprocess, and in the other hand, send it back to webapp buffering too.

Could pypager help me in any step? If not, do you know some way to do it?

Today I use this code to call a new Fabric process:

def exec_command(list_commands):

    abs_file = os.path.abspath(__file__)

    up = os.path.dirname(abs_file)
    up = os.path.dirname(up)

    manager_path = '{0}/manager'.format(up)

    results = subprocess.check_output(list_commands, cwd=manager_path)

    return results.decode('utf-8').strip().split("\n")

And that is the way I get the result back and send to the dashboard:

@manager_app.route('/git.pull/<worker_ip:re:[\.0-9]+>\:<worker_port:int>', method='GET')
@auth_basic(check_pass)
def git_pull_worker(worker_ip, worker_port):

    bottle.response.content_type = 'application/json'

    results = exec_command(['/usr/local/bin/fab', "git.pull:hosts={ip}".format(ip=worker_ip)])

    return {
        'worker_ip': worker_ip,
        'worker_port': worker_port,
        'results': results
    }

Thank you

TypeError: __new__() missing 1 required positional argument: 'strike'

I am having following error:

Traceback (most recent call last):
  File "/home/alper/venv/bin/pypager", line 8, in <module>
    sys.exit(run())
  File "/home/alper/venv/lib/python3.8/site-packages/pypager/entry_points/run_pypager.py", line 21, in run
    pager = Pager.from_pipe()
  File "/home/alper/venv/lib/python3.8/site-packages/pypager/pager.py", line 152, in from_pipe
    self.add_source(PipeSource(fileno=sys.stdin.fileno(), lexer=lexer))
  File "/home/alper/venv/lib/python3.8/site-packages/pypager/source.py", line 86, in __init__
    self._attrs = Attrs(
TypeError: __new__() missing 1 required positional argument: 'strike'

How can I resolve it?

pypager sometimes crops content on Windows

I was testing pypager with our tool, mssql-cli, and for the most part it works great! I'd love to incorporate it into our product.

I noticed, however, that pypager will crop content with some of our query results on Windows. For example, I see the following when executing the same query on Windows and macOS:

Windows:
pypager-win

macOS (what it should look like):
pypager-mac

FYI--the results come from querying a SQL db with select * from sys.databases.

Example in README does not work

The example in dir examples works.

p = Pager()
p.add_source(GeneratorSource(generate_a_lot_of_contents())
p.run()

However, the example in README.md does not work:

p = Pager(GeneratorSource(generate_a_lot_of_contents())
p.run()

The error is

File "/usr/local/Cellar/[email protected]/3.9.0_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/weakref.py", line 405, in __getitem__
    return self.data[ref(key)]
KeyError: <weakref at 0x104c7e720; to 'DummySource' at 0x103fa6e80>

It is tested on MacOS python3.7, python3.9.

Cannot copy pypager output over ssh

Using pypager as a library in an application when there is paged output on the screen the copy function (Cmd-C on Mac, Ctrl-C on Windows) causes the terminal to beep instead of copying the displayed text.
Is there any way to enable this ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.