0

I'm looking for a good way to hide input entered for a chat-like system on WIndows

For now, i wrote this

import sys

name = input("Enter your message: ")
sys.stdout.write('\r')
print(message)

But it doesn't do anything.

I'd like to do something like this

Enter your message: Hello

Output:
Hello

That is way better then

Enter your message: Hello

Output:
Enter your message: Hello
Hello

So the final output, after some messages will be

Hello
How are you?
I'm fine thanks.

And not

Enter your message: Hello
Hello
Enter your message: How are you?
How are you?
Enter your message: I'm fine thanks.
I'm fine thanks. 

I've seen some questions like mine before asking but i didn't find anything working, i also can't use os because it has to contain more inputs, i also tried using \r but (as i used it) doesn't do what i want it to do, thanks in advance.

Shasa
  • 21
  • 2

4 Answers4

1

This is now a year old, but I faced the same issue and I think what you'd be looking for is using ANSI escape codes.

Specifically, I used '\033[1A' and '\033[K', I don't have as much experience on them but as far as I can tell they're really good for specific manipulations like this.

'[1A' would be used to go on the line above and '[K' to erase everything on the line. So if you were to print out your "Enter your message: " from your input function, save your text in a variable, use the escape codes and then print your text, I believe you'd get the desired result.

Here I made an infinite texting loop where you can get what I believe is what you're looking for:

while True:
    text = input("Enter your message: ")
    print('\033[1A' + '\033[K', end='')
    print(text)

From here on you can do all kinds of manipulation/validation of your text and such, for example a specific condition for breaking the loop.

While I was looking for an answer I found this question and bashBedlam's answer is particularly useful. They mention ANSI escape codes will not work the same on all terminals though, so it might be worth looking into if this is the solution you want.

Hope this helps!

0

Here is how you can do so if you are using command prompt to run your code;

from os import system
name = input('Enter your name: ')
system('cls')
print(f'Your name is {name}')

system('cls') clears the console after the input has been submitted as it is run immediately after the input is provided.

If you are using mac;

system('clear')
Irfan wani
  • 4,084
  • 2
  • 19
  • 34
  • As i said, i can't use os because it has to contain more input and prints, and if i use it it will erase everything, i'm looking for a way to erase only that line and not the entire program – Shasa Feb 04 '21 at 09:09
0

If you want to just hide the user input you could use the builtin getpass module. This is designed for passwords so it will not display what the user types on the terminal.

>>> import getpass
>>> name = getpass.getpass(prompt="name: ")
name:
>>> print(name)
Chris
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • I'm not trying to hide the input, i need to hide the entire input line right after input is submitted – Shasa Feb 04 '21 at 09:14
  • You might want to change the title of the question as it says "Hide entered input" – Chris Doyle Feb 04 '21 at 09:26
  • It explains itself in the description, i guess the title is not wrong – Shasa Feb 04 '21 at 09:29
  • 1
    explains it so well that two answers dont meet your requirement and the questions is downvoted......Maybe not as well as you think. – Chris Doyle Feb 04 '21 at 09:30
  • I'm sorry this is my first question here – Shasa Feb 04 '21 at 09:33
  • 1
    We all want to help you. So you want to delete the entire line which has the text about what to input and what the user types? is this on a specific OS? or has to be portable to work across a variety of OS – Chris Doyle Feb 04 '21 at 09:36
  • I re-wrote my question, now it says for good what i'm looking for. – Shasa Feb 04 '21 at 09:54
0

Use this code:

import os
l = []
def main():
    empty = ""
    while True:
        message = input("Enter message: ")
        os.system("cls")
        if message == empty or message.isspace() == True:
            break
        l.append(message)
    print("Output:")
    for i in l:
        print(i)
main()
NameError
  • 206
  • 1
  • 3
  • 19
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 26 '21 at 09:53