#!/bin/bash
##################################################################################
## Bashish, a console theme engine
## Copyright (C) 2010 Thomas Eriksson
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
########################################################
##
## Detect if term is xterm title compatible, must run in bash
##
########################################################

# only run in bash
## if the terminal interprets titles (most do: ie xterm, some not, ie linux
## console) this string should not print out on
## the screen, and thus the terminal would not report the cursor being moved
test x$BASH != x && _bashish_prompt_isxterm ()
{
	## cygwin-terminal is xterm compatible, but does not support terminal responses
	test "x${TEST_TERM}" = xcygwin && return 0

	## run only once
	unset -f _bashish_prompt_isxterm
	printf "\033]0;     \007\033[6n"

	i=0
	until test "$i" = 9
	do
		CHAR=""
		read -t1 -n1 CHAR
		case "$CHAR" in
		|[|[0-9]|\;) CURPOS="$CURPOS$CHAR";let i++;;
		R)CURPOS="$CURPOS$CHAR";break;;
		esac
	done
	## clear terminal response
	printf "\[A[20D                    [20D[0m" >/dev/tty
	
	_bashish_title() { :;}
	case "$CURPOS" in
	*";1R")
		return 0
	;;
	esac
	return 1
}
_bashish_prompt_isxterm
