Skip to content

Commit

Permalink
Add support for external pagers via \pager command
Browse files Browse the repository at this point in the history
mfussenegger committed Jan 9, 2024
1 parent 809be31 commit 23a8dc6
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ Changes for crash
Unreleased
==========

- Added support for setting an external pager via ``\pager <executable>``

- Added ROLE keyword for autocomplete & autocapitalize

2023/11/23 0.30.1
8 changes: 8 additions & 0 deletions crate/crash/commands.py
Original file line number Diff line number Diff line change
@@ -93,6 +93,13 @@ def __call__(self, cmd, fmt=None):
fmt, ', '.join(cmd.output_writer.formats))


class SetPager(Command):
""" set an external pager. Use without argument to reset to internal paging """

def __call__(self, cmd, pager=None):
cmd.output_writer.pager = pager


class ToggleAutocompleteCommand(Command):
""" toggle autocomplete """

@@ -235,4 +242,5 @@ def __call__(self, cmd, check_name=None, **kwargs):
'autocapitalize': ToggleAutoCapitalizeCommand(),
'verbose': ToggleVerboseCommand(),
'check': CheckCommand(),
'pager': SetPager(),
}
30 changes: 25 additions & 5 deletions crate/crash/outputs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import csv
import json
import subprocess
import sys

from colorama import Fore, Style
@@ -51,6 +52,7 @@ def __init__(self, writer, is_tty):
self._json_lexer = JsonLexer()
self._formatter = TerminalFormatter()
self.writer = writer
self.pager = None
self._output_format = 'tabular'
self._formats = {
'tabular': self.tabular,
@@ -84,11 +86,29 @@ def to_json_str(self, obj, **kwargs):

def write(self, result):
output_f = self._formats[self.output_format]
output = output_f(result)
if output:
for line in output:
self.writer.write(line)
self.writer.write('\n')
if self.pager:
# Change tty to avoid colorizing output for pager
tty = self.is_tty
self.is_tty = False
try:
output = output_f(result)
if output:
with subprocess.Popen(self.pager, shell=True, stdin=subprocess.PIPE) as p:
encoding = "utf-8"
for line in output:
p.stdin.write(line.encode(encoding, "replace"))
p.stdin.write("\n".encode(encoding))
p.stdin.close()
else:
self.writer.write('\n')
finally:
self.is_tty = tty
else:
output = output_f(result)
if output:
for line in output:
self.writer.write(line)
self.writer.write('\n')

def raw(self, result):
duration = result.duration
1 change: 1 addition & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -620,6 +620,7 @@ def test_help_command(self):
'\\connect connect to the given server, e.g.: \\connect localhost:4200',
'\\dt print the existing tables within the \'doc\' schema',
'\\format switch output format',
'\\pager set an external pager. Use without argument to reset to internal paging',
'\\q quit crash',
'\\r read and execute statements from a file',
'\\sysinfo print system and cluster info',

0 comments on commit 23a8dc6

Please sign in to comment.