Jump to content

ConsoleWrite and ANSI color codes?

Recommended Posts

So, I have a program that reads the STDOUT of from an application I did not make (a coin miner), watches for certain events, and also prints the application's output back to the screen.

The miner program uses ANSI color codes to produce a pretty output, which I'd like to retain, but if I ConsoleWrite() the data I grabbed from STDOUT back to the screen, the "escape character" (first character of the ANSI code) is malformed and causes the ANSI code to not work, resulting in the codes appearing as text in the console window. I can't get ANSI codes to work at all through ConsoleWrite() actually. Is there a way to do this? If not, what's the quickest way to strip all ANSI codes from the stream so they don't clutter the output?

Even an example script with an ANSI code doesn't work:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

; the character before the [ is supposed to be an ESC character. Neither of the below produce desired output.
ConsoleWrite("�[0;36m This should be in Cyan"&@LF)
ConsoleWrite(Chr(27) & "[0;36m This should be in Cyan"&@LF)

(Program needs to be compiled and run in a command prompt, I know SciTe doesn't support it)

Edited by wolstech
Link to post
Share on other sites

you could embed in your gui a Browsercontrol equipped with a javascript xterminal component capable of correctly manage ANSI control codes.
Then you could send to it the stdout of your coin miner application

here is a quick (and dirty) example on how to setup the stuff. (It should download the needed components automatically to the script path)

Global $ohJS
_JS_Environment() ; setup the Javascript environment and the xterm component to be used within AutoIt;

; the character before the [ is supposed to be an ESC character
$ohJS.term.write(" This should be in Cyan" & @CRLF)
$ohJS.term.write(Chr(27) & "[0;36m This should be in Cyan" & @CRLF & @CRLF & @CRLF)

For $i = 30 To 37
    $ohJS.term.write(Chr(27) & "[0;" & $i & "m sample text" & @TAB & Chr(27) & "[1;" & $i & "m sample text" & @TAB & Chr(27) & "[0;" & $i + 10 & "m sample text" & @CRLF)
MsgBox(0, "Debug", "Pause")

; a sample of ANSI artwork downloaded from here http://artscene.textfiles.com/ansi/
$sURL = "http://artscene.textfiles.com/ansi/artwork/face_2.ans"
$sANSI = BinaryToString(InetRead($sURL))
MsgBox(0, "Debug", "The end")

Func _JS_Environment() ; setup Javascript engine with  the "xTerm" library embeded
    _CheckDependencies() ; check the presence of needed files and download if necessary
    ; *** create an 'html' page listing for the browser
    Local $sHTML = "<!DOCTYPE html>" & @CRLF
    $sHTML &= "<HTML><HEAD>" & @CRLF
    $sHTML &= '<meta http-equiv="X-UA-Compatible" content="IE=edge" />' & @CRLF
    $sHTML &= '<link rel="stylesheet" href="' & @ScriptDir & '\xterm.css" />' & @CRLF
    $sHTML &= "<script>" & @CRLF ; Javascripts goes here
    $sHTML &= 'var JSglobal = (1,eval)("this");' & @CRLF ; the 'JSglobal' variable gets an handle to the javascript global object
    $sHTML &= "</script>" & @CRLF
    $sHTML &= '<script src="' & @ScriptDir & '\xterm.js"></script>' & @CRLF
    $sHTML &= '<script src="' & @ScriptDir & '\fit.js"></script>' & @CRLF
    $sHTML &= "</HEAD>" & @CRLF
    $sHTML &= "<body>" & @CRLF
    $sHTML &= '<div id="xterminal" style="position:absolute;' & @CRLF
    $sHTML &= "                   top:3px;" & @CRLF
    $sHTML &= "                   left:3px;" & @CRLF
    $sHTML &= "                   width:794px;" & @CRLF
    $sHTML &= "                   height:494px;" & @CRLF
    $sHTML &= '                   margin: 0px 0px 0px 0px">' & @CRLF
    $sHTML &= "</div>" & @CRLF
    $sHTML &= " <script>" & @CRLF
    $sHTML &= "    var term = new Terminal();" & @CRLF
    $sHTML &= "    term.open(document.getElementById('xterminal'));" & @CRLF
    $sHTML &= "    term.fit();" & @CRLF
    $sHTML &= " </script>" & @CRLF
    $sHTML &= "</body>" & @CRLF
    $sHTML &= "</HTML>" & @CRLF ; html closing tags
    ; *** end of html page listing
    $oIE = ObjCreate("Shell.Explorer.2") ; a BrowserControl
    GUICreate("", 800, 500, 10, 10) ; place the gui on screen
    GUICtrlCreateObj($oIE, 0, 0, 800, 500) ; embed and render $oIE usable
    GUISetState(@SW_SHOW) ; show the GUI
    While Not String($oIE.readyState) = 'complete' ; wait for about:blank
    ; this waits till the document is ready to be used (portion of code from IE.au3)
    While Not (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4)
    $oIE.document.Write($sHTML) ; inject lising directly to the HTML document:
    $oIE.document.close() ; close the write stream
    ; this waits till the injected page is ready
    While Not String($oIE.readyState) = 'complete'
    ; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
    $ohJS = $oIE.document.parentwindow.JSglobal ; $ohJS is a reference to the javascript Global Obj
    ; now the javascript engine and loaded modules can be used in our AutoIt script using the $ohJS reference
EndFunc   ;==>_JS_Environment

Func _CheckDependencies()
    Local $iCheck = 0
    $iCheck += FileExists(@ScriptDir & '\xterm.js')
    $iCheck += FileExists(@ScriptDir & '\xterm.css')
    $iCheck += FileExists(@ScriptDir & '\fit.js')
    ConsoleWrite("Debug check: " & $iCheck & @CRLF)
    If $iCheck <> 3 Then
        InetGet("https://github.com/xtermjs/xterm.js/archive/2.9.2.zip", "2.9.2.zip")
        _Unzip(@ScriptDir & '\2.9.2.zip', @ScriptDir, 'xterm.js-2.9.2\dist\xterm.js')
        _Unzip(@ScriptDir & '\2.9.2.zip', @ScriptDir, 'xterm.js-2.9.2\dist\xterm.css')
        _Unzip(@ScriptDir & '\2.9.2.zip', @ScriptDir, 'xterm.js-2.9.2\dist\addons\fit\fit.js')
EndFunc   ;==>_CheckDependencies

; https://www.experts-exchange.com/questions/26321098/How-to-unzip-a-single-file-from-a-zipped-subdirectory.html
Func _Unzip($sZIPfile, $sDestFolder, $sSingleFileName = '')
    Local $oApp = ObjCreate("Shell.Application")
    If $sSingleFileName <> "" Then
EndFunc   ;==>_Unzip

have a look to following links for a better use:

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...