Colorize terminal input?

Is it possible to colorize terminal input on the fly with plain Rust?
I want to build a program where users can enter text and if it is a known word/program, it will be highlighted immediately with a specific color in the terminal. I know there a plenty of crates out there for colorizing terminal output, but I didn’t found any for colorizing terminal input on the fly.

1 Like

Input and output is something that your shell manages. What you want is kind of complicated and weird.
Let me try to describe something that could work.

You can check the text on the terminal and if the user presses space (for simplicity) you check the last word that was entered (from space to space I would suggest).
If it is something you want to highlight, you have to set the shell cursor to the position before the word and then colourize the word and print it.

f
fo
foo
foo (space)

[colored]foo[/colored]
  • First character typed in, no space, no work here
  • Second character typed in, no space, no work here
  • Third character typed in, no space, no work here
  • Space, lets check if the word is recognized
  • yes, let’s remove it and replace it by a colorized version

For the cursor movement you can read https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html for colourization you can read https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html

4 Likes

I’ve feared someone would say that. :confused: Well, I could work with something like crossterm and check each entered character, but that is such a pain and so much overhead, I’ve hoped I could avoid that and instead somehow manipulate the already printed text. But thanks for the links, I’ll read that.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.