0

Suppose I am using rlwrap like this: rlwrap --remember sml. That will start Standard ML of New Jersey:

Standard ML of New Jersey v110.79 [built: Sat Oct 26 12:27:04 2019]
- 

If I enter Jer at the prompt and press Tab, rlwrap will complete it to Jersey . I only want completion for the code I enter, not for the lines that contain version information (and/or copyright information). Is there a way to exclude such lines from rlwrap's completion when using --remember?

Flux
  • 9,805
  • 5
  • 46
  • 92

1 Answers1

0

This is beyond the capabilities of a "vanilla" rlwrap. You can, however, easily achieve what you want with a with an rlwrap filter

An rlwrap filter defines one or more callbacks ("handlers") that get called with e.g. the wrapped program's input and output lines. The filter can then modify those, or (in your case) feed them into the "completion list" (the list of possible completions). This makes it possible to fine-tune what to complete on:

#!/usr/bin/env python3

"""A demo filter dat does the same as rlwrap's --remember option 
   with a few extra bells and whistles

   Save this script as 'remember.py' sowewhere in RLWRAP_FILTERDIR and invoke as follows:
   rlwrap -z remember.py sml 
   N.B. Don't use the --remember option in this case!  
"""

import sys
import os

if 'RLWRAP_FILTERDIR' in os.environ:
    sys.path.append(os.environ['RLWRAP_FILTERDIR'])
else:
    sys.path.append('.')

import rlwrapfilter

filter = rlwrapfilter.RlwrapFilter()

filter.help_text = "Usage: rlwrap [-options] -z remember.py <command>\n"\
                   + "a filter to selectively add <command>s in- and output to the completion list "


def feed_into_completion_list(message):
  for word in message.split():
    filter.add_to_completion_list(word)

# Input handler: use everything
def handle_input(message):
  feed_into_completion_list(message)
  return message

filter.input_handler = handle_input

# Output handler: use every output line not containing "Standard ML ..."
def handle_output(message):
  if "Standard ML of New Jersey" not in message:
    feed_into_completion_list(message)
  return message

filter.output_handler = handle_output
    
# Start filter event loop:
filter.run()

In many use cases for --remember you might want to limit what goes into the completion list. If so, this is the way to do it.

Hans Lub
  • 5,513
  • 1
  • 23
  • 43
  • Will this filter break the functionality of `--break-chars`? – Flux Oct 27 '21 at 07:21
  • No. Pythons `split()` only splits on whitespace. `rlwrap` will then further break up whatever is fed into the completion list according to `--break-chars`. The manpage stays: _whitespace is always word-breaking_,. This should probably include `CR` as well, but currently doesn't The only reason the filter splits `message` before calling `filter.add_to_completion_list(word)` is to prevent spurious CRs (`^R`) from appearing in the completions. – Hans Lub Oct 27 '21 at 07:50
  • Why does the code check for `"Standard ML of New Jersey"` in the output handler but not in the input handler? – Flux Oct 27 '21 at 07:57
  • I suppose you will never enter the text `"Standard ML of New Jersey"` into the interpreter. If you do, `"Jersey"` will be a completion. – Hans Lub Oct 27 '21 at 08:03
  • Okay, I understand this now. What I really want is for rlwrap to remember all input but none of the output. Perhaps it should be added to rlwrap as an option (e.g. `--remember-input`). – Flux Oct 27 '21 at 08:20
  • Then simply omit the input handler in the filter above. If this is what you meant originally, maybe edit your question and I'll edit the answer - it will be useful to a lot more people than the original question and answer. – Hans Lub Oct 27 '21 at 08:32
  • When I use this plugin, `--break-chars` seems to be ignored. – Flux Oct 27 '21 at 11:07
  • Yes, you are right! Of course, one can replace the `message.split()` in the filter by e.g.`re.split('[\n\r .,;]', message)` but there is currently no way for filters to know what `--break-chars` is set to. I'll register this as a `rlwrap` bug. – Hans Lub Oct 27 '21 at 11:31
  • Thank you for the confirmation. Will this `--break-chars` problem be added as an issue on GitHub? https://github.com/hanslub42/rlwrap/issues – Flux Oct 28 '21 at 14:02