How to move cursor back above, NO WAY @ COLS MOD 8 = 1

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
neorobin
Posts: 47
Joined: 01 May 2012 12:18

How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#1 Post by neorobin » 22 Sep 2016 07:18

In cmd window, there has a method to control cursor back above beyond some lines:
[Tested on 64bit Win7]

echo;<TAB><Backspaces>

<TAB> means a only one horizontal tabulation character -- ASCII value is 9.
<Backspaces> means some Backspaces -- Backspace is the character that ASCII value is 8.

When the cursor's X coor is 0 -- the most left position in a line,
the least count of backspaces can calculate by a formula:

cntBS = 2 + ceil( buffWidth / 8 ) * linesWantBackAbove

or

cntBS = 2 + (buffWidth + 7) / 8 * linesWantBackAbove

BUT when the screen buff width is a number that mod by 8 is 1,
this method will fail, eg. screen buff width is 121, 81, 73, 65, ...


Image
For the formula, demonstration code:

Code: Select all

@echo off
> nul chcp 437
setlocal enabledelayedexpansion

set "TAB=$" & REM Replace the left $ to a real TAB character -- ASCII value 9

for %%w in (90 80 70 60 50) do (

    set /a buffwid              =   %%w
    set /a linesWantBackAbove   =   3
    set /a "cntBS = 2 + (buffwid + 7) / 8 * linesWantBackAbove"

    mode !buffwid!

    echo;Now screen buff width is !buffwid!
    echo;Control cursor back above !linesWantBackAbove! lines
    echo;  needs count of backspaces:
    echo;  2 + ^(!buffwid! + 7^) / 8 * !linesWantBackAbove! = !cntBS!
    echo;

    for /L %%a in (7 -1 1) do echo;%TAB%%TAB%%TAB%This is last %%ath line.

    set "BSs="
    for /L %%a in (1 1 !cntBS!) do set "BSs=!BSs!"

    echo;%TAB%!BSs!
    <nul set /p "=Now cursor is here:"

    >nul pause

)

<nul set /p "=any key to exit.."
>nul pause
exit
Last edited by neorobin on 22 Sep 2016 11:34, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#2 Post by aGerman » 22 Sep 2016 10:19

That doesn't work for me either. I checked in a HEX editor that tab (09) and backspace (08) are correctly in the code.
That's my output on Win10 Home, 32 Bit

Code: Select all

Now screen buff width is 90
Control cursor back above 3 lines
  needs count of backspaces:
  2 + (90 + 7) / 8 * 3 = 38

                        This is last 7th line.
                        This is last 6th line.
                        This is last 5th line.
                        This is last 4th line.
                        This is last 3th line.
                        This is last 2th line.
                        This is last 1th line.

Now cursor is here:

Same result with all the other settings in the loop.

Steffen

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#3 Post by neorobin » 22 Sep 2016 11:21

aGerman

Oh, sorry, I tested only on 64bit WIN7

Image

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#4 Post by aGerman » 22 Sep 2016 12:28

That's impressive! Although I fear it's only a feature or some kind of undefined behavior within your Chinese Windows environment :( We try to work around that limitation for ages. It seems the only reliable way is using a 3rd party program.
viewtopic.php?t=4762
viewtopic.php?t=7129

Steffen

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#5 Post by Aacini » 22 Sep 2016 18:02


einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#6 Post by einstein1969 » 07 Oct 2016 05:21

works on my win7 32bit. cp850

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#7 Post by Aacini » 23 May 2017 08:57

I just tested this method in Windows 8.1 Spanish and works well... See this answer.

Antonio

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: How to move cursor back above, NO WAY @ COLS MOD 8 = 1

#8 Post by Compo » 23 May 2017 10:51

I have no idea what I'm supposed to be confirming! If the prompt line is not supposed to change it's position from three lines above the last visible line of text then I can confirm it works. Not that I can see any point in it just yet!

I run this code and a cmd.exe, (Windows 7, x64, English, Code Page 850), window opens which is too tall to fit on a screen with a resolution of 1920,1080, (even if it did open at the top of the screen).
I then hit enter a number of times, each time it appears to look like the animation you've posted, (the window width reduces a few times and that's it).

Post Reply