"""
Allows you to set the title bar in xterms (gnome-terminal, putty, 
...) for the textui and in the Tk title bar for the tkui with status 
text formed of a series of name/value pairs.

Currently implemented:
  - (jmberne) optional sort key so status is in the right order
  - allows you to set and change a name value pair
  - allows you to unset a name value pair
  - detects whether you're using the textui or the tkui and 
    works appropriately

Currently not implemented:
  - we should have a more global way of setting the title bar
    in all ui's and this should call that instead


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

Patches provided by:
 - Josh Berne (optional sort key)
"""
__author__ = "Will Guaraldi <willg@bluesock.org>"
__version__ = "1.2 (15 January, 2004)"
__description__ = "Basic statusbar for textui (under Xterm) and tkui."

import sys, os
from lyntin import exported
from lyntin.modules import modutils

commands_dict = {}

status = {}

def statusbar_cmd(ses, args, input):
  """
  Allows you to set the title bar in xterms (gnome-terminal, putty, 
  ...) for the textui and in the Tk title bar for the tkui with status 
  text formed of a series of name/value pairs.

  examples:

    #setstatus hps 400

  adds "hps: 400" to the title bar.

    #setstatus hps

  removes the hps thing from the title bar.

  category: wbgstatusbar
  """
  global status

  name = args["name"]
  value = args["value"]
  sortkey = args["sortkey"]

  if not sortkey:
    sortkey = name

  ui = str(exported.get_engine().getUI().__class__)

  if not value:
    if status.has_key(name):
      del status[name]
  else:
    status[name] = (value,sortkey)

  output = []
  for mem,(value,sortkey) in status.items():
    output.append( (sortkey, mem, mem + ": " + value + " "))

  output.sort()

  output = [ x[2] for x in output ]

  if ui.find("Textui") != -1:
    sys.stdout.write("\33]2;" + "".join(output) + "\07")
    sys.stdout.flush()
  elif ui.find("Tkui") != -1:
    exported.get_engine().getUI().settitle("".join(output))
  else:
    exported.write_error("setstatus: textui and tkui support only.")

commands_dict["setstatus"] = (statusbar_cmd, "name value= sortkey=")

def load():
  """ Initializes the module by binding all the commands."""
  modutils.load_commands(commands_dict)

def unload():
  """ Unload things."""
  modutils.unload_commands(commands_dict)