Jump to content

Overwriting StdOut in CUI compiled app


Recommended Posts

Didn't find anything that seemed relevant with several search attempts, hence the following:

I am using a ConsoleWrite('.') command to indicate progress in a cui compiled script.

However, when the job is long, I get "lots of dots" ..................................................................

I am looking for a way to overwrite the dot, such that my compiled cui application can provide a status

indicator without scrolling its way into oblivion.

edit: typo

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

ConsoleWrite(@CR) appears to behave differently in a CUI app then when outputting to an editor console.

Don't know if that's by accident or design.

Maybe you can adapt this

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
$ClearProgress = 0
$processing = "Processing"
$progress = ""

For $i = 1 to 100
    ConsoleWrite(@CR & $processing & $progress)
    Sleep(50)
    $ClearProgress +=1
  $progress &= "."
    If $ClearProgress = 25 then
        ConsoleWrite(@cr & "                                                                     ")
        $ClearProgress = 0
        $progress = ""
    EndiF
Next

ConsoleWrite(@CRLF & "DONE!")

Edit: removed extraneous line of code

Edited by ResNullius
Link to comment
Share on other sites

And here's another variation:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
$ClearProgress = 0

$progress = ""

$processing = "Processing ["
$PaddedProgress = ""

For $i = 1 To 50
    $ProgressLength = StringLen($progress)
    For $iPad = 1 To 10 - $ProgressLength
        $PaddedProgress &= " "
    Next
    ConsoleWrite(@CR & $processing & $PaddedProgress & "]")
    Sleep(50)
    $ClearProgress += 1
    $progress &= "."

    If $ClearProgress = 11 Then
        ConsoleWrite(@CR & "                                                                     ")
        ;       ConsoleWrite(@cr & "Processing.")
        $ClearProgress = 0
        $progress = ""
    EndIf
    $PaddedProgress = $progress
Next
$PaddedProgress = ".........."
ConsoleWrite(@CR & $processing & $PaddedProgress & "]")
ConsoleWrite(@CRLF & @CRLF & "DONE!")oÝ÷ Øêò¢ç(ºWcºË`¡ú+¶¨×%jË"p:,²§êÚºÚ"µÍÔYÚ[ÛÊXÝ]ÈÜX]YH]]Ò]ÕÜÑÕRH
Ð]]Ò]ÕÜÐÚ[ÙLÕRO^[[ÛÊXÝ]ÈÜX]YH]]Ò]ÕÜÑÕRH
ÌÍÐÛXÙÜÜÈHÌÍÜØÙÜÚ[ÈH    ][ÝÔØÙÜÚ[È   ][ÝÂÌÍÜÙÜÜÈH   ][ÝÉ][ÝÂ[H  ÌÍÜÜ[ÍOVÉÌÎNÉÎM
ÍÉÌÎNË ÌÎNËÉÌÎNË    ÌÎNÉÎM
ÌÉÌÎNË ÌÎNÉÌLÉÌÎNÈBÜ  ÌÍÚHHHÈÌQÜ    ÌÍÜÈHHÈXÝ[
    ÌÍÜÜ[KLBPÛÛÛÛUÜ]JÔ    [È ÌÍÜØÙÜÚ[È   [È ÌÍÜÜ[ÉÌÍÜ×JBÛY
L
BS^^ÛÛÛÛUÜ][ÈÔ   [È ][ÝÑÓIÌÌÎÉ][ÝÊ
Link to comment
Share on other sites

How cool is that..

Thanks for your elegant solution to this issue.

Out of curiosity, did you stumble upon this, or is this lore that is common implementation-wise?

Thanks again!

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

How cool is that..

Thanks for your elegant solution to this issue.

Out of curiosity, did you stumble upon this, or is this lore that is common implementation-wise?

Thanks again!

Just stumbled on it trying to find an answer to your question.

I tried a few old DOS tricks like writing CHR(08) which would normally cause a backspace, but didn't do anything here. By chance though I originaly had my "Done!" line with just an @CR at the beginning and I noticed it overwrote the "Progress" part :)

Anyway, here's one more kinda cool lookin' one:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
$ClearProgress = 0

$progress = "ùùùùùùùùùù";Chr(249)
$processing = "Processing ["

For $i = 1 To 3
    For $iPos = 1 To 10
        $DisplayProgress = StringReplace($progress, $iPos, " ", 1)
        ConsoleWrite(@CR & $processing & $DisplayProgress & "]")
        Sleep(50)
    Next
    For $iPos = 10 To 1 Step -1
        $DisplayProgress = StringReplace($progress, $iPos, " ", 1)
        ConsoleWrite(@CR & $processing & $DisplayProgress & "]")
        Sleep(50)
    Next
Next

$DisplayProgress = $progress

ConsoleWrite(@CR & $processing & $DisplayProgress & "]")
ConsoleWrite(@CRLF & "DONE!")

Of course the Sleeps() may not be needed depending on the length of time it takes your progress loop to be updated.

And if they are needed, they could probably be replaced with some of the Timer functions.

Edited by ResNullius
Link to comment
Share on other sites

Just for completeness,

The command interpreter appears to reset the cursor position to the leftmost position of the current line when @CR is the first character in the ConsoleWrite() command.

I tested with chr(1) to chr(255) in ANSI and UNICODE modes, with no variation of behavior (no other single chars appear to behave in any special ways.)

I sent a doc frq up on bug trac, Valik indicates that it ain't a bug, more of a happy accident, and isn't interested in officially documenting the behavior, as it is more a function of cmd.exe , not au3. The good news is that, while the technique isn't going

to be officially sanctioned, it won't be squashed (on purpose, anyway).

This will have to serve as the documentation for the community.

edit:

additional enviornment tests would be welcomed. PM Me, I'll add them here.

Esp. Vista, w2k,2003,2008

Curious if it works on win98 class machines under older versions of au3.

In addition:

Any reference to command processor documentation that describes this behavior would be added for completeness.

This has been tested on XPPSP2 with 3.2.10.0 and 3.2.11.2 ANSI/UNICODE CUI COMPILED.

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Thanks for that USEFULL info!

I vote for adding this info into Documetation at ConsoleWrite() too.

Here is my version of DOS console progressbar. It's similar to DOS ScanDisk progressbar:

#AutoIt3Wrapper_Change2CUI=y

#include <string.au3>

For $i = 1 to 79
    $part1 = _StringRepeat(Chr(219), $i)
    $part2 = _StringRepeat(Chr(177), 79 - $i)
    ConsoleWrite(@CR & $part1 & $part2)
    Sleep(50)
Next

; uncomment this if you want to preserve finished progressbar
;~ ConsoleWrite(@CRLF & "DONE!")

; comment this if you want to preserve finished progressbar
ConsoleWrite(@CR & _StringRepeat(' ', 79))
ConsoleWrite(@CR & "DONE!")
Edited by Zedna
Link to comment
Share on other sites

Another note about CUI with national characters: You must use CharToOem conversion

#AutoIt3Wrapper_Change2CUI=y

ConsoleWrite(Ansi2Oem('Řádek 1') & @CRLF)
ConsoleWrite(Ansi2Oem('Řádek 2: ěščřžýáíé') & @CRLF)
ConsoleWrite(Ansi2Oem('Řádek 3'))

Func Ansi2Oem($text)
    $text = DllCall('user32.dll','Int','CharToOem','str',$text,'str','')
    Return $text[2]
EndFunc
Link to comment
Share on other sites

Here is a modified version of a progress made by ResNullius:

#AutoIt3Wrapper_Change2CUI=y

Global Const    $FOREGROUND_Black       = 0x0000
Global Const    $FOREGROUND_Blue        = 0x0001
Global Const    $FOREGROUND_Green       = 0x0002
Global Const    $FOREGROUND_Cyan        = 0x0003
Global Const    $FOREGROUND_Red         = 0x0004
Global Const    $FOREGROUND_Magenta     = 0x0005
Global Const    $FOREGROUND_Yellow      = 0x0006
Global Const    $FOREGROUND_Grey        = 0x0007
Global Const    $FOREGROUND_White       = 0x0008

Global Const    $BACKGROUND_Black       = 0x0000
Global Const    $BACKGROUND_Blue        = 0x0010
Global Const    $BACKGROUND_Green       = 0x0020
Global Const    $BACKGROUND_Cyan        = 0x0030
Global Const    $BACKGROUND_Red         = 0x0040
Global Const    $BACKGROUND_Magenta     = 0x0050
Global Const    $BACKGROUND_Yellow      = 0x0060
Global Const    $BACKGROUND_Grey        = 0x0070
Global Const    $BACKGROUND_White       = 0x0080


$ClearProgress = 0
$processing = "Processing "
$progress = ""
Dim $spin[4]=['¦', '/', '-', '\' ]
$elipses = ""

$iCnt = 0
$iCnt2 = 16
For $i = 1 to 22
    For $s = 1 to Ubound($spin)-1
        _SetConsoleColor(BitOR($iCnt,8,$iCnt2,128))
        $iCnt += 1
        $iCnt2 += 16
        ConsoleWrite(@CR & $processing & $elipses & "[" &$spin[$s] & "]")
        $elipses &= "."
        If $iCnt = 9 Then $iCnt = 0
        If $iCnt2 = 112 Then $iCnt2 = 16
        Sleep(50)
    Next
Next

_SetConsoleColor(12)
ConsoleWrite(@CRLF & @CRLF & "DONE!"&@CRLF&@CRLF)


$asSplit = StringSplit("Using console colors is fun. ;-)","")
$iCnt2 = 1
For $iCnt = 1 to Ubound($asSplit)-1
    _SetConsoleColor(BitOR($iCnt2,8))
    $iCnt2 += 1
    ConsoleWrite($asSplit[$iCnt])
    If $iCnt2 = 9 Then $iCnt2 = 1
Next

sleep(5000)

Func _SetConsoleColor($iColor)
    Local $aRet, $aRet2
    $aRet = DllCall($hdllKernel32,"hwnd","GetStdHandle","int",-11);$STD_INPUT_HANDLE = -10,$STD_OUTPUT_HANDLE = -11,$STD_ERROR_HANDLE = -12
    If Not UBound($aRet) > 0 Then
        ConsoleWrite("!>Error.  GetStdHandle failed."&@LF)
        Return 0
    EndIf
    $aRet2 = DllCall($hdllKernel32,"int","SetConsoleTextAttribute","hwnd",$aRet[0],"ushort",$iColor)
    If Not UBound($aRet2) > 0 Then
        ConsoleWrite("!>Error.  SetConsoleTextAttribute failed."&@LF)
        Return 0
    EndIf
    ;Note: The StdHandle doesn't need to be closed because the handle wasn't opened.  It was gotten.
    If $aRet2 <> 0 Then 
        Return 1
    Else
        Return 0
    EndIf
EndFunc

Func OnAutoItStart()
    Global $hdllKernel32 = DllOpen("kernel32.dll")
    IF @error Then
        ConsoleWrite("!> Error.  Couldn't open kernel32.dll"&@LF)
        Exit
    EndIf
EndFunc

Func OnAutoItExit()
    DllClose($hdllKernel32)
EndFunc

It uses color. Enjoy.

- The Kandie Man ;-)

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

It uses color. Enjoy.

- The Kandie Man ;-)

What??? Colours in console output?!!!

What's next, little picture thingys that people can somehow activate with a moveable pointer so they don't have to type out the real commands to start a program? :)

Nice effects Kandie Man.

Link to comment
Share on other sites

What??? Colours in console output?!!!

Now, now. Even back in the day we had ansi.sys

And still I will use the console utilities below for fancy command line processing...

conset /?

Version 1.4, Copyright ©2001, 2002, Frank P. Westlake.

Displays, sets, or deletes cmd.exe environment variables, modifies console parameters, and performs floating point mathematics.

and

timemath /?

Version 0.91, Copyright ©2001, Frank P. Westlake.

Performs addition, subtraction, or comparison on a date, time, or both.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...