/*
   Copyright 2010 Aaron J. Radke

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cc.drx

/** Ansi escape help
 *  note: on windows use org.fusesource.jansi.AnsiConsole.systemInstall() before calling any console.println functions
 * */
object Ansi{
  val EscapePat = "(\u009b|\u001b\\[)[0-?]*[ -\\/]*[@-~]".r  //from fansi!https://github.com/lihaoyi/fansi from stackoverflow!http://stackoverflow.com/a/33925425/871202 from ecma!http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
  val Field  = "\u001f"
  val Record = "\u001e"
  val Space  = "\u0020"
  val Escape = "\u001b"
  val Colon  = ":"

  def strip(str:String):String = EscapePat.replaceAllIn(str,"")

  private def move(kind:Char,n:Int):String = s"$Escape[$n$kind"
  def up(n:Int):String = move('A',n)
  def down(n:Int):String = move('B',n)
  def right(n:Int):String = move('C',n)
  def left(n:Int):String = move('D',n)
  // def printtmp(str:String):Unit = print(str + left(str.size))
  def printtmp(x:Any):Unit = printtmp(x,100)
  def printtmp(x:Any,numCols:Int):Unit = {val str = x.toString.fit(numCols); print(str + left(numCols))}

  def kson(root:Any, kvs:(Any,Any)*):String = Green(Format(root)) + " " + kvs.map{case (k,v) =>
    Blue(Format(k)) + ":" + Format(v)
  }.mkString(" ")
}