Parse ANSI codes in text

I’d like to parse ANSI Escape Characters (Build your own Command Line with ANSI escape codes), for example, if the char is the ansi clear screen character, I want to call my custom function, if the character is something that colors the text that comes after it, I want to apply the richtext font color to the text after that Ansi Character

Has anyone already solved this or has a solution?

Maybe use some of the string functions?
For the clear screen, just use string.match or something and then call the function if the result isn’t nil.

For the color codes, I would approach it this way:
Use gmatch for the codes and iterate through them.
If the index isn’t 1, then add “” before adding “”.