0

Is there a way to detect when a user enters for example

Input: users input here

to detect what they have wrote, users input here. Before they press enter?

kelvar
  • 11
  • 2
  • 1
    Why do you want to do this? Are you trying to validate the input? What actual problem are you trying to solve? – ekhumoro Aug 22 '21 at 16:41
  • @ekhumoro So I can make a program that makes a Suggestion on how they should input something. e.g. Input: help -i. This command in my program would be wrong, and I would like to correct them by it displaying: syntax is: help --i, Before they press enter. – kelvar Aug 22 '21 at 17:11
  • But how do you know when they've finished editing/typing their input? That's partly what pressing enter *means*, right? The normal way to handle validation is to print any errors/help after the input has been entered. No need to try to re-invent the wheel here. – ekhumoro Aug 22 '21 at 17:25
  • https://stackoverflow.com/questions/13207678/whats-the-simplest-way-of-detecting-keyboard-input-in-a-script-from-the-termina – pippo1980 Aug 22 '21 at 17:28

1 Answers1

0

The "prompt_toolkit" package might be what you're looking for. This package allows for auto completion while typing from inside a python script. You can configure the words that you want to autocomplete, but here is an example given on their docs (this package is very feature-rich, so be sure to check out the other many examples on their Github page):

import time

from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.shortcuts import CompleteStyle, prompt

WORDS = [
    "alligator",
    "ant",
    "ape",
    "bat",
    "bear",
    "beaver",
    "bee",
    "bison",
    "butterfly",
    "cat",
    "chicken",
    "crocodile",
    "dinosaur",
    "dog",
    "dolphin",
    "dove",
    "duck",
    "eagle",
    "elephant",
    "fish",
    "goat",
    "gorilla",
    "kangaroo",
    "leopard",
    "lion",
    "mouse",
    "rabbit",
    "rat",
    "snake",
    "spider",
    "turkey",
    "turtle",
]


class SlowCompleter(Completer):
    """
    This is a completer that's very slow.
    """

    def __init__(self):
        self.loading = 0

    def get_completions(self, document, complete_event):
        # Keep count of how many completion generators are running.
        self.loading += 1
        word_before_cursor = document.get_word_before_cursor()

        try:
            for word in WORDS:
                if word.startswith(word_before_cursor):
                    time.sleep(0.2)  # Simulate slowness.
                    yield Completion(word, -len(word_before_cursor))

        finally:
            # We use try/finally because this generator can be closed if the
            # input text changes before all completions are generated.
            self.loading -= 1


def main():
    # We wrap it in a ThreadedCompleter, to make sure it runs in a different
    # thread. That way, we don't block the UI while running the completions.
    slow_completer = SlowCompleter()

    # Add a bottom toolbar that display when completions are loading.
    def bottom_toolbar():
        return " Loading completions... " if slow_completer.loading > 0 else ""

    # Display prompt.
    text = prompt(
        "Give some animals: ",
        completer=slow_completer,
        complete_in_thread=True,
        complete_while_typing=True,
        bottom_toolbar=bottom_toolbar,
        complete_style=CompleteStyle.MULTI_COLUMN,
    )
    print("You said: %s" % text)


if __name__ == "__main__":
    main()

The above code works like this: enter image description here

Docs: https://python-prompt-toolkit.readthedocs.io/en/stable/

Autocompletion examples: https://github.com/prompt-toolkit/python-prompt-toolkit/tree/master/examples/prompts/auto-completion

Kailash E
  • 151
  • 5