简体   繁体   中英

Strange result when binding Tab to readline.completion in Python 3.8

I am running Ubuntu 20.4 w/ Python 3.8.2 and coming across a strange behavior with the readline interface in Python.

My target is to provide an input facility that allows at a given location on the terminal to choose from a list of given strings, or - alternatively - simply inputting a desired string. My code does work (almost), until for some reasons for certain strings the result gets corrupted.

My code looks like this


    import readline
    class __readlineCompleter(object):
        """
        Cycles through a list of potential strings the user might want to enter
        """
    
        def __init__(self, options):
            self.options = list(options)  #sorted([item for item in options])
            self.index = 0
    
            return
    
        def complete(self, text, state):
            if state == 0:
                if text and not text in self.options:
                    self.options.append(text)
    
                self.index += 1
                if self.index >= len(self.options):
                    self.index = 0
    
                return self.options[self.index]
            else:
                return None
    
    def input_at(x_pos, y_pos, _text, _prefill, _choices):
        """
        positions the cursor at x_pos, y_pos, prints text if given and reads a string from stdin
        @param x_pos(int):          row to print at
        @param y_pos(int):          columns to print at
        @param _text(string):       text to print before the input field
        @param _prefill(string):    text as default input
        @param _choices(list):      list of strings as proposed input values
        @return: (string):          input given by user, or False
        """
    
        _myPrompt = "\033[{};{}H{}\033[0m".format(x_pos, y_pos, _text)
    
        if type(_choices) in (set, list) and len(_choices):
            _choices = set([x for x in _choices])
            readline.set_completer(__readlineCompleter(_choices).complete)
            readline.set_completer_delims('')
            readline.parse_and_bind('tab: menu-complete')
    
        try:
            _input = input(_myPrompt)
        except KeyboardInterrupt as EOFError:
            _input = False
    
        return _input

As I said this works pretty well, once pressing TAB the user gets presented a different option from _choices , pressing Enter returns the chosen entry. Alternatively the user can enter a string manually. So this was what I intended, until it comes to certain strings; so when I call above code like this


    _temp = [ 'Julius Papp', 'J‐Live', 'Mr. Electric Triangle', 'MullerAnustus Maximilian']
    _result = input_at(15, 0, "Name:   ", _temp[0], _temp)

The cli looks like this (after each line I pressed Tab and the next line shows the output)

Name:   
Name:    Julius Papp  
Name:    J-Live  
Name:    Mr. Electric Triangle  
Name:    MullerAnustus Maximilian  
Name:    MullerAnustJulius Papp
                    ***********      

(I inserted the stars in the latter output to show the "wrong" display) So somehow the final attempt destroyed the linebuffer ; any subsequent Tab works as expected. Nevertheless the resulting _input is a valid member of _choices , so this only seems to be a displaying issue.

Does anyone have an idea why the string MullerAnustus Maximilian behaves this strange?

I solved this issue myself, by using the rl 2.4 – GNU Readline Bindings library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM