-2

I’m trying to write a piece of code which will accept a string as input and if the user has entered an specific word it will change the word's color to red but all in real time I mean as the user is entering the string not after he pressed the Enter . I know it should be done by threading but the problem is I don't know how to make the threads works Real-time. Could anybody help me please?

Mohammad Siavashi
  • 1,192
  • 2
  • 17
  • 48
  • What do you mean change the colour? Do you mean in the interpreter? On the command line? In some kind of GUI? – jonrsharpe Dec 12 '14 at 10:35
  • Here is how to read one character at a time: http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user/510364#510364 – bosnjak Dec 12 '14 at 10:37

1 Answers1

1

Here is the method for reading one char at a time from stdin, taken from this answer:

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()

getch = _Getch()

One possible usage of this code could be like below. You read characters and store them in a variable. Then you print this to the stdout and color it by colorama. This code is just an example, and it still has one thing not working properly: the handling of the "space". But if you try it out you will see that the words are being colored in real time. Additional tweaks are needed to properly output all the letters in real time, including space. But I leave that up to you, this is more than enough to get you started.

import sys
from colorama import Fore

special_words = ['bannanas', 'eggs', 'ham']
my_text = ''
while True:
    c = getch()
    if ord(c) == 13:  # newline, stop
        break 
    my_text += c    

    sys.stdout.write("\r")  # move to the line beginning
    for word in my_text.split():
        if word in special_words:
            sys.stdout.write(Fore.RED + word + " ")
        else:
            sys.stdout.write(Fore.RESET + word + " ")
    sys.stdout.flush()

# reset the color when done
print(Fore.RESET)
Community
  • 1
  • 1
bosnjak
  • 8,424
  • 2
  • 21
  • 47
  • thank you for you explanation . but i want to do it in the IDLE shell . not in a GUI . is it possible do to it there ? consider it with simple raw_input method – Mohammad Siavashi Dec 12 '14 at 12:26
  • can anyone help me how to use this code within my input ? i mean how to get input and highlight the word not just reading the characters with getch ? – Mohammad Siavashi Dec 15 '14 at 21:43
  • Have you even read my answer? I wrote 90% of code for you. I guess I've been spoonfeeding afterall.. – bosnjak Dec 16 '14 at 07:16