///|
const USAGE =
  #|usage: ansi_text  [-minimize]
  #|
  #|Transforms text from stdin that contains ANSI escape codes.
  #|
  #|  visualize [-minimize]  show the escape codes in a human-readable form
  #|  minimize               use as few escape codes as possible
  #|  strip                  remove the escape codes
  #|

///|
async fn run(args : ArrayView[String]) -> Int32 {
  // Each line is transformed on its own, so a line's styles don't depend on the
  // lines before it.
  let transform : ((Bytes) -> Bytes)? = match args {
    ["visualize"] => Some(@text.visualize)
    ["visualize", "-minimize"] =>
      Some(input => @text.visualize(@text.minimize(input)))
    ["minimize"] => Some(@text.minimize)
    ["strip"] => Some(@text.strip)
    _ => None
  }
  match transform {
    Some(transform) => {
      for line in @bstr.split_lines(@sys.read_stdin()) {
        @sys.print_stdout(transform(line) + b"\n")
      }
      0
    }
    None => {
      @sys.print_stderr(@bstr.of_string(USAGE))
      1
    }
  }
}

///|
async fn main {
  let argv = @env.args()
  let code = run(argv[1:]) catch {
    err => {
      @sys.print_stderr(@bstr.of_string("\{err}\n"))
      2
    }
  }
  @sys.exit(code)
}