<div>Not sure this is the right forum but here we go:</div><div><br></div>When using unix command 'script' to log the terminal input/output commands, it includes special ansi control escape characters.<div>I'd like to filter out the generated script file from those character sequences, so that it preserves the content (including newlines) but removes escape codes for coloring, etc.</div>
<div>That way I can apply tools like grep as if those characters were absent.</div><div><br></div><div>Here's an example, after a short script session where I typed 1234 BS BS BS BS 6789 (ie 4 backspaces), saved into log.txt.</div>
<div>Typing 'cat log.txt|grep 1234' returns '6789', even though 1234 doesn't appear once we cat the file in a terminal, because cat-ing in a terminal replays the backspace sequence, but 1234 really is in the file.</div>
<div><br></div><div>So I'd like help on writing a D utility function that will convert a string s1 into a string s2 such that:</div><div>* writeln(s1) prints the same as writeln(s2) (modulo removing colors from escape sequences)</div>
<div>* s2 doesn't contain any escape sequence (as given by std.uni.isControl)</div><div><br></div><div>Note, I'm NOT just looking into filtering out escape the 'isControl' characters (that's easy), because that leaves all the '[33m' garbage in the string; also the behavior of backspace needs to be emulated.</div>
<div><br></div><div>Here's a first stab at the problem, but it's incomplete (ie doesn't deal with backspaces which should erase a char from the string etc).</div><div><div>string remove_terminal_escape_codes(string a){</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>string pattern=(){</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>import std.uni;</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>import std.conv;</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>import std.range;</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>string pattern="[";</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>foreach(char ci; 0..255){</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>if(isControl(ci)){</div><div><span class="Apple-tab-span" style="white-space:pre">                           </span>pattern~=<a href="http://ci.to">ci.to</a>!dchar;</div><div><span class="Apple-tab-span" style="white-space:pre">                       </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>pattern~="]";</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>return pattern~`\[(\d{2}m|\dm|\d\d;\d\dm|J|K|\d\d[A-Z])`;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>}();</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>import std.regex;</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>return replace(a,regex(pattern,"g"),"");</div>
<div>}</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>More details:</div><div><br></div><div><div>cat -v log.txt:</div><div>-------------------------</div><div>Script started on Fri May 31 17:32:55 2013</div>
<div>^[[1m^[[7m%^[[27m^[[1m^[[0m                                                                                                                                                                                                                                                    ^M ^M^M^[[0m^[[27m^[[24m^[[J^[[32mprompt:M ^[[33m~/shortcuts ^[[00m%^[[K^[[199C^[[33mprompt_end:^[[1m17:32^[[0m ^[[1m#27378^[[0m^[[222D1^H1234^H ^H^H ^H^H^H1 ^H^H ^H6^H6789^M^M</div>
<div>^[[1m^[[7m%^[[27m^[[1m^[[0m                                                                                                                                                                                                                                                    ^M ^M^M^[[0m^[[27m^[[24m^[[J^[[32mprompt:M ^[[33m~/shortcuts ^[[00m%^[[K^[[199C^[[34mprompt_end:Err 1^[[37m ^[[1m#27378^[[0m^[[222D^M^M</div>
<div><br></div><div>Script done on Fri May 31 17:33:08 2013</div></div><div><div>-------------------------</div></div><div><br></div><div><div>cat log.txt:</div><div>-------------------------</div><div>Script started on Fri May 31 17:32:55 2013</div>
<div>prompt:M ~/shortcuts %6789                                                                                                                                                                                                   prompt_end:17:32 #27378</div>
<div>prompt:M ~/shortcuts %                                                                                                                                                                                                       prompt_end:Err 1 #27378</div>
<div><br></div><div>Script done on Fri May 31 17:33:08 2013</div></div><div><div>-------------------------</div></div><div><br></div><div>I can attach the file as .txt if forum allows.</div><div><br></div><div><br></div>