* Copyright (c) Jeremy Campbell Apthorp 2006, no rights reserved
* This is PUBLIC DOMAIN

inst
* figure out how high the text is
new: simp 1 1 200 "whiteontransparentchars" 1 0 0
setv va97 hght
kill targ

new: comp 1 2 200 "blnk" 1 0 10000
attr 304
imsk 65
flto -10000 -10000
* figure out the dimensions of blnk
setv ov99 wdth
setv ov98 hght
* from earlier; the height of the text
setv ov97 va97

* ov00 = activation toggle
setv ov00 0

** history, \n-sep'd
*sets name "history" ""
*setv name "history_length" 0
*setv name "browsing_history?" 0
*setv name "history_pos" -1

*setv game "caos_cmdline_history_max_length" 40

* rel_y
setv va00 ov98
subv va00 ov97
* input
pat: text 1 "blnk" 0 14 va00 1 2121 "whiteontransparentchars"
* output
pat: fixd 2 "blnk" 0 0 0 2 "whiteontransparentchars"
* shell symbol: $
pat: fixd 3 "blnk" 0 0 va00 1 "whiteontransparentchars"
part 3
ptxt "$"
frmt 0 0 0 0 0 0 0

* fix frmt, etc.
mesg writ targ 3333

sets ov10 ""



* raw key down - catch C-S-c to appear/disappear the cmdline
scrp 1 2 200 73
	inst
	doif _p1_ eq 'C' and keyd 17 = 1 and keyd 16 = 1
		doif ov00 = 0
			* hihi, CAOS command line speaking

			setv ov00 1
			
			* fake a window resize to avoid code replication
			mesg writ ownr 123

			part 1 fcus
		else
			* disappear time
			setv ov00 0

			flto -10000 -10000
		endi
	elif _p1_ eq 'P' and keyd 17 = 1 and ov00 = 1
		* Previous - recall crappy one-line history
		part 1
		doif ptxt eq null or ptxt eq "" and ov10 ne ""
			ptxt ov10
		endi
	elif _p1_ eq 'U' and keyd 17 = 1 and ov00 = 1
		* clear line
		part 1
		ptxt ""
	elif _p1_ eq 'S' and keyd 17 = 1 and ov00 = 1
		* Swap - swap input line and history line
		part 1
		doif ptxt eq null
			sets va00 ""
		else
			sets va00 ptxt
		endi
		ptxt ov10
		sets ov10 va00
	elif _p1_ eq 'L' and keyd 17 = 1 and ov00 = 1
		* cLear - clear output screen
		part 2
		ptxt ""
*	elif _p1_ eq 38 and ov00 eq 1
*		* up
*		mesg writ ownr 3001
*	elif _p1_ eq 40 and ov00 eq 1
*		* down
*		mesg writ ownr 3002
	endi
endm

* window resize
scrp 1 2 200 123
	* only if we're visible
	inst
	doif ov00 = 1
		* refix frmt
		mesg writ ownr 3333

		* flto right spot
		setv va00 wndh
		subv va00 ov98
		flto 0 va00
	endi
endm

* refrmt the text parts
scrp 1 2 200 3333
	inst
	* ov99 x ov98 = wdth x hght of blnk
	* ov97 = hght of text

	* input part
	part 1
	* rght
	setv va00 wndw
	subv va00 ov99
	negv va00
	* bott
	setv va01 ov98
	subv va01 ov97
	* safety
	subv va01 1
	frmt 0 0 va00 va01 0 0 0

	* fixd part
	part 2
	* rght same as before, va00
	* bott is size of text, ov99
	* top
	setv va02 wndh
	subv va02 ov98
	subv va02 ov99
	negv va02
	* align to bottom, last page scroll
	frmt 0 va02 va00 ov99 0 0 20
endm

* enter press
scrp 1 2 200 2121
	inst
	* grab the text out of our input part
	part 1
	sets va00 ptxt
	ptxt ""
*	* add to history
*	mesg wrt+ ownr 3003 va00 0 0
	doif va00 <> ""
		sets ov10 va00
	endi
	part 2
	sets va01 ptxt
	adds va01 "\n\n$ "
	adds va01 va00
	ptxt va01

	setv va99 0
	sets va10 caos 0 0 0 0 va00 0 1 va99
	doif va10 = "***"
		sets va10 "Exception: sid "
		adds va10 vtos va99
	elif va10 = "###"
		sets va10 va99
	endi

	part 2
	sets va50 ptxt
	adds va50 "\n"
	adds va50 va10
	ptxt va50

	* delete old text
	doif npgs > 1
		outs "more pages than one\n"
		* delete first line in va50
		setv va99 0
		loop
			setv va10 sins va50 1 "\n"
			doif va10 lt 0
				setv va99 1
			else
				setv va80 strl va50
				subv va80 va10
				addv va10 1
				sets va50 subs va50 va10 va80
				ptxt va50
			endi

			doif npgs <= 1 setv va99 1 endi
		untl va99 = 1
	endi
endm


* HISTORY NOTE
* history_pos = 1: latest history entry
* history_pos = history_len: last history entry

* hist up
*scrp 1 2 200 3001
*	inst
*	doif name "history_pos" lt name "history_len"
*		part 1
*		* if we're not browsing the history, append the current text to the end of the history
*		doif name "browsing_history?" eq 0
*			adds name "history" "\n"
*			adds name "history" ptxt
*			setv name "history_pos" 0
*			setv name "browsing_history?" 1
*		endi
*
*		addv name "history_pos" 1
*
*		* now find the relevant bit of history
*		setv va00 name "history_len"
*		subv va00 name "history_pos"
*		addv va00 1
*
*		setv va01 1
*		loop
*			setv va01 sins name "history" va01 "\n"
*			doif va01 lt 0
*				* no newline, so start position is at start of name "history" & we'll break
*				setv va01 1
*				setv va00 0
*			else
*				addv va01 1
*				subv va00 1
*			endi
*		untl va00 le 0
*
*		* okay, start of our history entry is at va01. The end will either be at the next \n or at EOS
*		setv va02 sins name "history" va01 "\n"
*		doif va02 lt 0
*			* we need to get va01..EOS
*			setv va02 strl name "history"
*		endi
*		subv va02 va01
*
*		* va02 is now length
*		sets va10 subs name "history" va01 va02
*		outx va10
*		outs "\n"
*		ptxt va10
*	endi
*endm
*
** hist down
*scrp 1 2 200 3002
*endm
*
** hist add
*scrp 1 2 200 3003
*	doif _p1_ ne ""
*		doif name "history_length" > 0
*			adds name "history" "\n"
*		endi
*		adds name "history" _p1_
*		addv name "history_length" 1
*
*		doif name "history_length" > game "caos_cmdline_history_max_length"
*			* we've more history than we need, slice it
*			setv va99 0
*			loop
*				setv va00 sins name "history" 1 "\n"
*				doif va00 lt 0
*					setv va99 1
*				else
*					setv va01 strl name "history"
*					subv va01 va00
*					addv va00 1
*					sets name "history" subs name "history" va00 va01
*				endi
*
*				doif name "history_length" <= game "caos_cmdline_history_max_length"
*					setv va99 1
*				endi
*			untl va99 = 1 
*		endi
*	endi
*endm
