Skip to content

Commit

Permalink
Implement #41 - initial pager support
Browse files Browse the repository at this point in the history
  • Loading branch information
clach04 committed Mar 24, 2023
1 parent 4551e22 commit c48e035
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion puren_tonbo/tools/ptig.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import os
from optparse import OptionParser
import pydoc
import shlex
import sys
import time
Expand All @@ -33,6 +34,44 @@
is_py3 = sys.version_info >= (3,)
is_win = sys.platform.startswith('win')

# Python pager, do NOT use temporary files - could be used to monkey patch pydoc.pager
def getpager_no_temp_files():
"""Decide what method to use for paging through text.
Extracted and modified from pydoc.getpager()"""
if not hasattr(sys.stdin, "isatty"):
return pydoc.plainpager
if not hasattr(sys.stdout, "isatty"):
return pydoc.plainpager
if not sys.stdin.isatty() or not sys.stdout.isatty():
return pydoc.plainpager
use_pager = os.environ.get('MANPAGER') or os.environ.get('PAGER')
if use_pager:
"""
if sys.platform == 'win32': # pipes completely broken in Windows
return lambda text: tempfilepager(plain(text), use_pager)
"""
if os.environ.get('TERM') in ('dumb', 'emacs'):
return lambda text: pydoc.pipepager(plain(text), use_pager)
else:
return lambda text: pydoc.pipepager(text, use_pager)
if os.environ.get('TERM') in ('dumb', 'emacs'):
return pydoc.plainpager
if hasattr(os, 'system') and os.system('(pager) 2>/dev/null') == 0:
return lambda text: pydoc.pipepager(text, 'pager')
if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0:
return lambda text: pydoc.pipepager(text, 'less')

"""
try:
if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0: # under unix/linux; "more /dev/null" is a good substitute, without needing temp file writing
return lambda text: pydoc.pipepager(text, 'more')
else:
"""
return pydoc.ttypager

pager = getpager_no_temp_files()
print(pager)

class FakeOptions: # to match ptgrep (OptParse) options
display_full_path = True
count_files_matched = True # prefix filenames with a number, for easy reference/selection
Expand Down Expand Up @@ -202,7 +241,8 @@ def do_cat(self, line=None): # TODO pager/less/more option
try:
data = notes.note_contents(in_filename, password)
#print('%r' % data)
print('%s' % data) # TODO bytes instead of string? -- or simply refactor ptcat and call that....
#print('%s' % data) # TODO bytes instead of string? -- or simply refactor ptcat and call that....
pager(data) # TODO bytes instead of string? -- or simply refactor ptcat and call that....
except KeyboardInterrupt:
print('search cancelled')

Expand Down

0 comments on commit c48e035

Please sign in to comment.