It's tricky.
I've got a binary somewhere which I wrote to report the current cursor position on the console for exactly this sort of thing.
To be honest, it's usually easier to keep track of the position yourself. The only tricky bit is that you need to deal with lines that wrap.
If you really want to get into managing text windows, I wrote an entire library in KiXtart to allow you to create and destroy windows, complete with up and down scrolling and stuff.
In practice this is probably more than you need, and simply tracking the cursor will do:
Code:
Break ON
$=SetOption("Explicit","ON")
GLOBAL $_CursorY
$_CursorY=7
CLS
udfShowText(23,"First line of text")
udfShowText(23,"Second line of text")
udfShowText(23,"Third line of text")
Function udfShowText($CursorX,$s)
At($_CursorY,$CursorX) $s
$_CursorY=$_CursorY+1
EndFunction
If you want to fully manage independant areas of the console, here is a demo script:
Code:
; vim600:ts=3 sw=3 ai filetype=kix
$=SetOption("Explicit","ON")
$=SetOption("HideCursor","OFF")
CLS
; Pane demo script.
;
; This demo shows two scrolling panes, displaying the contents of the Windows and System 32 directories.
;
Dim $paneWin,$paneSys
Dim $sWinFile, $sSysFile
Dim $iWinPass, $iSysPass
$iWinPass=1
$iSysPass=1
Box(3,9,12,60,"double")
Box(13,9,22,60,"double")
$paneWin=PaneCreate(4,10,8,50)
$paneSys=PaneCreate(14,10,8,50)
At(3,12) "Files present in "+%SYSTEMROOT%
At(13,12) "Files present in "+%SYSTEMROOT%+"\System32"
$sWinFile=Dir(%SYSTEMROOT%,0)
$sSysFile=Dir(%SYSTEMROOT%+"\System32",1)
While Not KBHit()
$paneWin=PaneWrite($paneWin,@CRLF+@TIME+" "+$iWinPass+" "+$sWinFile)
$PaneSys=PaneScrollDown($paneSys)
$paneSys=PaneAt($paneSys,0,0,@TIME+" "+$iSysPass+" "+$sSysFile)
PaneUpdate($paneWin,"w+/n")
PaneUpdate($paneSys,"g+/n")
$sWinFile=Dir("",0)
If @ERROR $iWinPass=$iWinPass+1 $sWinFile=Dir(%SYSTEMROOT%,0) EndIf
$sSysFile=Dir("",1)
If @ERROR $iSysPass=$iSysPass+1 $sSysFile=Dir(%SYSTEMROOT%+"\System32",1) EndIf
Sleep(0.2)
Loop
Get $
Exit 0
; Cut here 8<----------------------------------------------------------------------------------->8
; Begin Pane Functions
Function PaneCreate($iTop, $iLeft, $iHeight, $iWidth)
If Not IsDeclared($_Pane_Top)
Global $_Pane_Top ; Pane position offset from top of console
Global $_Pane_Left ; Pane position offset from left of console
Global $_Pane_Height ; Pane height
Global $_Pane_Width ; Pane width
Global $_Pane_Cursor_X ; Cursor vertical (Row)
Global $_Pane_Cursor_Y ; Cursor horizontal (Column)
Global $_Pane_Blank ; Enough spaces to blank a line
Global $_Pane_Change_X_Low ; First row with a change on it.
Global $_Pane_Change_X_High ; Last row with a change on it.
Global $_Pane_Content ; Actual pane content
$_Pane_Top=0
$_Pane_Left=1
$_Pane_Height=2
$_Pane_Width=3
$_Pane_Cursor_X=4
$_Pane_Cursor_Y=5
$_Pane_Blank=6
$_Pane_Change_X_Low=7
$_Pane_Change_X_High=8
$_Pane_Content=9
EndIf
Dim $P
ReDim $P[$_Pane_Content+1]
$P[$_Pane_Top]=CInt($iTop)
$P[$_Pane_Left]=CInt($iLeft)
$P[$_Pane_Height]=CInt($iHeight)
$P[$_Pane_Width]=CInt($iWidth)
$P[$_Pane_Cursor_X]=0
$P[$_Pane_Cursor_Y]=0
$P[$_Pane_Change_X_Low]=0
$P[$_Pane_Change_X_High]=$iHeight-1
$P[$_Pane_Content]=""
; Create the blank line field
While Len($P[$_Pane_Blank])<$iWidth $P[$_Pane_Blank]=Left($P[$_Pane_Blank]+" ",$P[$_Pane_Width]) Loop
; Blank the pane
While $iHeight $P[$_Pane_Content]=$P[$_Pane_Content]+$P[$_Pane_Blank] $iHeight=$iHeight-1 Loop
$PaneCreate=$P
EndFunction
Function PaneAt($P, $iRow, $iColumn, Optional $sText)
$P[$_Pane_Cursor_X]=CInt($iRow)
$P[$_Pane_Cursor_Y]=CInt($iColumn)
If $sText<>"" $P=PaneWrite($P,$sText) EndIf
$PaneAt=$P
EndFunction
Function PaneWrite($P,$sText)
Dim $X,$Y,$C,$s,$POS
$X=$P[$_Pane_Cursor_X]
$Y=$P[$_Pane_Cursor_Y]
$C=$P[$_Pane_Content]
While $sText<>""
$s=Left($sText,1)
Select
Case $s=Chr(10)
$X=$X+1
If $X=$P[$_Pane_Height]
$X=$X-1
$P[$_Pane_Content]=$C
$P=PaneScrollUp($P)
$C=$P[$_Pane_Content]
EndIf
Case $s=Chr(13)
$Y=0
Case 1
$Y=$Y+1
If $Y=$P[$_Pane_Width]
$Y=0
$sText="X"+Chr(10)+$sText
Else
$POS=$X*$P[$_Pane_Width]+$Y+1
$C=CStr(SubStr($C,1,$POS-1))+$S+SubStr($C,$POS+1)
EndIf
EndSelect
$sText=SubStr($sText,2)
Loop
$P[$_Pane_Cursor_X]=$X
$P[$_Pane_Cursor_Y]=$Y
$P[$_Pane_Content]=$C
$PaneWrite=$P
EndFunction
Function PaneScrollUp($P)
$P[$_Pane_Content]=SubStr($P[$_Pane_Content],$P[$_Pane_Width]+1)+$P[$_Pane_Blank]
$PaneScrollUp=$P
EndFunction
Function PaneScrollDown($P)
$P[$_Pane_Content]=Left($P[$_Pane_Blank]+$P[$_Pane_Content],$P[$_Pane_Width]*$P[$_Pane_Height])
$PaneScrollDown=$P
EndFunction
Function PaneUpdate($P, Optional $sColor)
Dim $sCursorState,$i
$sCursorState=SetOption("HideCursor","ON")
For $i=0 To $P[$_Pane_Height]
If $sColor Color $sColor EndIf
At($P[$_Pane_Top]+$i, $P[$_Pane_Left])
SubStr($P[$_Pane_Content],1+$P[$_Pane_Width]*$i,$P[$_Pane_Width])
Next
At($P[$_Pane_Top]+$P[$_Pane_Cursor_X], $P[$_Pane_Left]+$P[$_Pane_Cursor_y])
$sCursorState=SetOption("HideCursor",$sCursorState)
EndFunction