/*
 * Set of helper functions for pretty printing
 */


/**
 * Print a timestamp.
 */
function timestamp()
{
	var timestamp = new Date().toLocaleString();
	var spaces = (termsize().width - timestamp.length) / 2;
	timestamp = repeatChar(" ", spaces) + "\03303" + timestamp;
	print(timestamp);
}
/**
 * Find out how wide the left column needs to be.
 */
function calculateUserWidth()
{
	var people = wholist();
	for (i = 0 ; i<people.length ; i++)
	{
		if(people[i].username.length > userWidth)
		{
			userWidth = people[i].username.length;
		}
	}
}
function stripUser(user, text)
{
	if(!isGlobal())
	{
		return text.substr(user.length);
	}
	else
	{
		var roomLength = text.split(":")[0].length + 1;
		return text.substr(roomLength + user.length);
	}
	
}
function isUser(user, text)
{
	if(!isGlobal())
	{
		return text.substr(0,user.length) == user;
	}
	else
	{
		return text.split(":")[1].substr(0,user.length) == user;
	}
}
/** 
 * Check if the user is in global mode
 */
function isGlobal()
{
	var people = wholist();
	for (i = 0 ; i<people.length ; i++)
	{
		if(people[i].username == whoami)
		{
			return people[i].chatmodes.search("o") >= 0;
		}
	}
}
/**
 * Find a sensible place to split a string (just before the last word)
 */
function findWordSplit(text, start)
{
	//Try scanning back
	for(var i=start ; i>1 ; i--)
	{
		if(text.charAt(text.letterToChar(i)) == ' ')
		{
			return i;
		}
	}
	//Try scanning forwards
	for(var i=start ; i<text.strlen() ; i++)
	{
		if(text.charAt(text.letterToChar(i)) == ' ')
		{
			return i;
		}
	}
	return text.strlen();
}

String.prototype.strlen = function()
{
	return this.replace(/\033../g,"").length;
}


String.prototype.isEscapeChar = function(i)
{
	return this.charAt(i) == '\033';
}

String.prototype.isEscapeSequence = function(i)
{
	if(this.isEscapeChar(i))
	{
		return true;
	}
	if(--i >= 0 && this.isEscapeChar(i))
	{
		return true;
	}       
	if(--i >= 0 && this.isEscapeChar(i))
	{
		return true;
	}       
	return false;
}

String.prototype.plainCharBefore = function(i)
{
	while(i>0 && this.isEscapeChar(i))
	{
		i--;
	}
	return i;
}
String.prototype.letterToChar = function(i)
{
	var c = 0;
	while(i > 0)
	{
		while(this.isEscapeSequence(++c)) {}
		i--;
	}
	return c;
}
String.prototype.strsubstr = function(s, l)
{
	var start = this.letterToChar(s);
	var end = this.letterToChar(s + l);
	return this.substr(start, end-start);
}

/**
 * Display text with nice word wrapping
 */
function wrap(user, prefix, text)
{
	var width = termsize().width - (userWidth);
	var pad = repeatChar(" ", userWidth);
	pad = colourUser(user, pad);
	var splitWidth = width;
	if(text.strlen() > splitWidth)
	{
		splitWidth = findWordSplit(text,splitWidth);
	}
	print(prefix + text.strsubstr(0, splitWidth));
	for(var i=splitWidth; i<text.strlen() ; i+=splitWidth)
	{
		if(text.strlen() > i + width)
		{
			splitWidth = findWordSplit(text.substr(i), width);
		}
		else
		{
			splitWidth = width;
		}
		var line  = text.strsubstr(i, splitWidth);
		print(pad + line);
	}
}
function repeatChar(c, times)
{
	if(times == 0)
	{
		return "";
	}
	var s = c;
	while(s.length < times)
	{
		s += c;
	}
	return s;
}
