简体   繁体   中英

How to get the current cursor position in python readline

I have a custom python readline completer function, in the completer function I call readline.get_line_buffer() which gives me the current text input so that I can complete the current word in a context-sensitive way.

What I am missing now is the possibility to get the current cursor position. For example, the position is required to determine the preceding command if possible arguments are to be completed.

In the python readline module documentation , I haven't found a way to determine the position, so how is it done?

I downloaded the pyreadline source code and figured this out. The following works in pyreadline (Windows port of readline) on Python 2.7:

from pyreadline import Readline
readline = Readline()
cursor = readline.mode.l_buffer.point

Note that this works in pyreadline (Windows), but not in GNU readline (per Binabik's comment below).

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