*! 1.2  MBH 16 June 2022
*!   a) corrects the timestamp to work on earlier versions of Stata (14 and up)
*! 1.1  MBH 13 June 2022
*!   a) improvements to the regular expression used to "deansify" files
*!   b) file write routines which are more robust to lines with special characters
*! 1.0  MBH 29 May 2022
*! this is a subprogram that is part of the shell wrapper package -inshell-
*! it is meant to process files generated by shell commands that produce output
*! containing ANSI escape sequences. This program removes that formatting,
*! generating a new, "cleaned" file for -inshell- to parse

capture program drop _inshell_deansify

program define _inshell_deansify, rclass

syntax, File(string) [ ERRor TYpe ]
version 14

local ansi_regex_1 "[\x1b]*\[[?0-9;]*[CADJKlmhsu]"
local ansi_regex_2 "[\x1b]*\[[^@-~]*[@-~]"
local timestamp `=clock("`c(current_time)'","hms")'

tempname  deansify   deansify_nosmcl
tempfile  deansifyf  deansifyf_nosmcl
quietly file open `deansify' using "`deansifyf'", write replace
local ln = 0
tempname filen
quietly file open `filen' using "`file'", read
if "`error'" != "" {
  quietly file open `deansify_nosmcl' using "`deansifyf_nosmcl'", write replace
  file write `deansify' "{err}{...}" _n
}
file read `filen' line
local lines_with_errors ""
while r(eof) == 0 {
  if `:strlen local line' != 0 {
    local ++ln
    capture file write `deansify' (ustrregexra(`"`macval(line)'"', "`ansi_regex_1'" , "")) _n
    if _rc {
      capture file write `deansify' (ustrregexra("`macval(line)'", "`ansi_regex_1'" , "")) _n
      if _rc {
        capture file write `deansify' (ustrregexra(`""`macval(line)'""', "`ansi_regex_1'" , "")) _n
        if _rc {
          file write `deansify' ("THIS LINE WAS NOT CAPTURED") _n
          local lines_with_errors "`lines_with_errors',`ln'"
        }
      }
    }
    if "`error'" != "" {
      capture file write `deansify_nosmcl' (ustrregexra(`"`macval(line)'"', "`ansi_regex_1'" , "")) _n
      if _rc {
        capture file write `deansify_nosmcl' (ustrregexra("`macval(line)'", "`ansi_regex_1'" , "")) _n
        if _rc {
          capture file write `deansify_nosmcl' (ustrregexra(`""`macval(line)'""', "`ansi_regex_1'" , "")) _n
          if _rc {
            file write `deansify_nosmcl' ("THIS LINE WAS NOT CAPTURED") _n
          }
        }
      }
    }
  }
  file read `filen' line
}

return local deansify_errors = "`:subinstr local lines_with_errors "," "" '"
capture file close `filen'
capture file close `deansify'
local outf "`c(tmpdir)'inshell_deansified_`timestamp'.txt"
copy "`deansifyf'" "`outf'"
return local outfile "`outf'"

if "`error'" != "" {
  capture quietly file close `deansify_nosmcl'
  local outf_nosmcl "`c(tmpdir)'inshell_deansified_`timestamp'_nosmcl.txt"
  capture quietly copy "`deansifyf_nosmcl'" "`outf_nosmcl'"
  return local outfile_nosmcl "`outf_nosmcl'"
}

// use this option for testing purposes
if !missing("`type'") {
  noisily type "`outf'"
}

end