I have a bashrc setup where I run a script
command every time I open a session that writes to an output file as I write in the terminal.
The files that script
produces contain all interactions with the tty (so arrow keys, delete key…).
I am trying to write a script that will obtain the last command I wrote along with it’s ouptuts, but for this I need to convert the raw tty characters into readable text (so, for example, I remove all the bash color characters).
The problem I come up with, is that I can’t just remove the control characters, I need to apply them somehow. I know this is possible to do without hard-coding what each control character does (after all, if I do cat terminal_output
and hand copy the output I can simulate by hand what I want to achieve).
Example of the problem (I use my arrow keys to go back and add the first ” character):
$ script -q -O out
$ echo "arrow keys"
arrow keys
$ exit
If I cat the file
$ cat out
$ echo "arrow keys"
arrow keys
$ exit
If I just filter out the special characters:
$ cat out | perl -pe 's/e([^[]]|[.*?[a-zA-Z]|].*?a)//g' | col -b
$ echo 1@"ow keys"
arrow keys
$ exit
I want my tty to process the text with special characters and output the processed readable text so I can then use that instead.