"""
A pretty printer module for making it a bit easier to code and
debug complex macros.

Currently implemented:
  - a quick pretty printer
  - handles escaped { } and ;

Currently not implemented:
  - colorizing?

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright 2002, 2003, 2004 Will Guaraldi
"""

__author__ = "Will Guaraldi <willg@bluesock.org>"
__version__ = "1.3 (16 January, 2004)"
__description__ = "Pretty printer for manager data."

from lyntin import exported, utils
from lyntin.modules import modutils

def pprint(text):
  """ 
  Takes in a Lyntin command string and pretty prints it.  This
  makes it easier to read what's going on in particularly long
  and involved macros.

  @param text: the incoming string to pretty print
  @type  text: string

  @returns: the string
  @rtype: string
  """
  out = []
  depth = 0

  for i in range(0, len(text)):
    mem = text[i]
    if i == 0:
      memm1 = ""
    else:
      if i == 1:
        memm1 = text[i-1]
      else:
        if text[i-2] == "\\" and text[i-1] == "\\":
          memm1 = ""
        else:
          memm1 = text[i-1]

    if mem == "{" and memm1 != "\\":
      depth += 1
      out.append("{\n" + ("  " * depth))
    elif mem == "}" and memm1 != "\\":
      depth -= 1
      out.append("\n" + ("  " * depth) + "}")
    elif mem == ";" and memm1 != "\\":
      out.append(";\n" + ("  " * depth))
    else:
      out.append(mem)

  return "".join(out)

commands_dict = {}

def pprint_cmd(ses, args, input):
  """
  Pulls the variable/alias/action from the appropriate manager and
  pretty-prints it making it easier to see.

  ex:

    #pprint {alias} {killall}

    will pretty print the killall alias from the alias manager for 
    this session.

  category: wbgpprint
  """
  managername = args["manager"]
  item = args["item"]

  manager = None

  try:
    manager = exported.get_manager(managername)
  except:
    pass

  if not manager:
    exported.write_error("No manager of that name: '%s'" % managername)
    return

  data = manager.getInfo(ses, item)

  datanew = []
  for mem in data:
    datanew.append(pprint(mem))

  datanew = "\n".join(datanew)

  exported.write_message(("pretty printed %s:\n" % managername) + datanew)

commands_dict["pprint"] = (pprint_cmd, "manager item")

def load():
  modutils.load_commands(commands_dict)

def unload():
  modutils.unload_commands(commands_dict.keys())