How to use gnu readline library in program?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Edwards

    How to use gnu readline library in program?

    I'm trying to figure out how to use the gnu readline library so
    that when my program is prompting the user for input there is
    line editing and history support.

    I've read and re-read the documentation for the "readline"
    module in the standard library and I still can't figure out how
    to use the module or even if the module is intended to do what
    I want. The example code all seems to be about on how to
    modify the behavior of an interactive Python interpreter
    session so you have things like auto-completion of Python
    identifiers.

    What I want to do is replace sys.stdin.readl ine() with
    something that will provide the user with line editing and
    history recall. In other languages, one uses the Gnu readline
    library to do that, but my reading of the Python library
    documentation is that's not what the Python readline module is
    for. Am I wrong?

    --
    Grant Edwards grante Yow! On the road, ZIPPY
    at is a pinhead without a
    visi.com purpose, but never without
    a POINT.
  • Peter Otten

    #2
    Re: How to use gnu readline library in program?

    Grant Edwards wrote:
    I'm trying to figure out how to use the gnu readline library so
    that when my program is prompting the user for input there is
    line editing and history support.
    >
    I've read and re-read the documentation for the "readline"
    module in the standard library and I still can't figure out how
    to use the module or even if the module is intended to do what
    I want. The example code all seems to be about on how to
    modify the behavior of an interactive Python interpreter
    session so you have things like auto-completion of Python
    identifiers.
    >
    What I want to do is replace sys.stdin.readl ine() with
    something that will provide the user with line editing and
    history recall. In other languages, one uses the Gnu readline
    library to do that, but my reading of the Python library
    documentation is that's not what the Python readline module is
    for. Am I wrong?
    Here's a simple example:

    import readline

    for s in "alpha beta gamma".split():
    readline.add_hi story(s)

    candidates = "red yellow pink blue black".split()

    def completer(word, index):
    matches = [c for c in candidates if c.startswith(wo rd)]
    try:
    return matches[index] + " "
    except IndexError:
    pass


    readline.set_co mpleter(complet er)
    readline.parse_ and_bind("tab: complete")

    while 1:
    print raw_input("$ ")

    You may also consider using the cmd module.

    Peter

    Comment

    • Grant Edwards

      #3
      Re: How to use gnu readline library in program?

      >What I want to do is replace sys.stdin.readl ine() with
      >something that will provide the user with line editing and
      >history recall. In other languages, one uses the Gnu readline
      >library to do that, but my reading of the Python library
      >documentatio n is that's not what the Python readline module is
      >for. Am I wrong?
      >
      Here's a simple example:
      >
      import readline
      >
      for s in "alpha beta gamma".split():
      readline.add_hi story(s)
      >
      candidates = "red yellow pink blue black".split()
      >
      def completer(word, index):
      matches = [c for c in candidates if c.startswith(wo rd)]
      try:
      return matches[index] + " "
      except IndexError:
      pass
      >
      >
      readline.set_co mpleter(complet er)
      readline.parse_ and_bind("tab: complete")
      >
      while 1:
      print raw_input("$ ")
      Ah, thanks. It was far too simple. What I was looking for was
      simply:

      import readline

      then replace sys.stdin.readl ine() with raw_input()
      You may also consider using the cmd module.
      I'll have to keep the cmd module in mind for other applications
      (where the user is entering commands). Rather than commands,
      the user of this application is entering data that changes
      little (if any) from one line to the next, so raw_input() is
      exactly what I needed.

      --
      Grant Edwards grante Yow! I'm young ... I'm
      at HEALTHY ... I can HIKE
      visi.com THRU CAPT GROGAN'S LUMBAR
      REGIONS!

      Comment

      Working...