Make a live updating counter to show the number of remaining characters WHILE the user inputs the text in CLI (windows + python)

Prathamesh Ghatole

Basically the headline.

In Python, I want to take max 280 characters of input from the user, and show a live updating counter on the CLI as the user types the input.(Similar to a Progress bar)

Getting some text to update on the screen is simple, but I don't know how to count the characters as the user is inputting them.

P.S. First-time StackOverflow user, please go easy on me. :)

EDIT: Codebase: https://github.com/Prathamesh-Ghatole/100DaysOfCode-Writer/blob/master/main.py

I don't have a specific code snippet where I want to implement it yet.

But I basically want to take character input from the user in a loop where each iteration does the following:

  1. Take single character input.
  2. update a variable that counts the total number of input characters.
  3. subtract the number of input characters from the character number limit.
  4. Trigger a flag when the character limit is exceeded.
Tushar Ghige

Using pynput package this seems to work.

from pynput.keyboard import Key, Listener
import os

length = 0
char_count = dict()
text = str()


def on_press(key):
    try:
        global text
        global char_count
        global length

        if length >= 280:
            print("[+] Character Limit Excided!")
            return False

        elif key == Key.enter:
            return False

        else:
            os.system("cls")
            if key == Key.space:
                text += ' '
            else:
                text += f"{key.char}"
            char_count[text[-1]] = char_count.get(text[-1], 0) + 1
            length += 1
            print(f"Enter Text: {text}")
            print(f"Characters Left: {280 - length}")
            print(f"Char Count {char_count}")
    except:
        exit()

def main():
    os.system("cls")
    with Listener(on_press=on_press) as listner:
        print("Enter Text: ")
        listner.join()


if __name__ == '__main__':
    main()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Show how many characters remaining in a HTML text box using JavaScript

How to make sure that a user inputs a number?

Javascript - Display remaining characters of text area

Python - How to remove spaces between Chinese characters while remaining the spaces in between a character and a number?

Show remaining Text on hover

Show div element when user inputs text in a different field

Adding text on number counter

How do i make the user input a number, and if the user inputs a letter it returns an error - VB.net

How to take exact number of inputs from the user using Python?

Updating user inputs based on prompts C

How to Make Windows Show Unicode Characters Properly?

show a number of inputs type text equal to the number of selected files in my input file without send form

Change Jenkins from running as windows service to CLI, while remaining the jobs

Updating a counter in a python list of dictionaries

Show placeholder until user inputs text without spaces

How to show remaining characters if the textarea is prefilled?

Python Beginner (need assistance) - How do I make a While loop only accept certain user inputs?

How to make a countdown timer while waiting for multiple user inputs

How to pick only first N number of characters and drop remaining while matching pattern in LEX/FLEX

c++ program that inputs a sentence from the user and counts the number of words and characters in the sentence

python: how to wait in a for loop for user inputs in popup windows

Convert different text user inputs to integers in a while-try loop in python

change number of digits user inputs

Updating useEffect api call url with user inputs

user inputs not show in database table?

How to make the span to fill empty space on current line and continue to show remaining text on next line

flutter textformfield counter text not updating

How to shift letters in python after the user inputs a shift number and a message?

counter characters from a text file in python

TOP Ranking

HotTag

Archive