Colourful Scrolling Text Window

Post your working scripts, libraries and tools
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Colourful Scrolling Text Window

20 Nov 2015, 17:41

Four small functions to create & manage a simple scrolling text log with colours.
AHK_ID := text_log( x, y, w, h [, windowtitle ] )
Creates always-on-top window at x,y,w,h with title windowtitle.
Returns AHK_ID of window created.
error := write( text [, fontcolour, linenumber, backgroundcolour ] )
Writes a line of text on line linenumber with background backgroundcolour.
Returns error = TRUE if line number is not in the window.
log( Text [, Fontcolour ] )
Scrolls up one line.
Writes a line of text at the bottom of the window in fontcolour
scroll( [lines] )
Scrolls up lines lines.

Code: Select all

; Peabianjay's Colourful Scrolling Text Log

; Feel free to copy, edit and distribute willy nilly


; ==========================   Some sample calls =======================

LogID := text_log( 622, 131, 705, 800 )
error :=    write(  "Test Write Line", 0x00ff00, 45, "0xFF00FF" )
              log( "Text Log Line 1: " error "|" LogID "|" A_Index, "red" )
           scroll( 5 )

log( "Text Log Line 2: " error "|" LogID "|" A_Index, "red" )
return
exitapp

; ======================== The Four Funtion Definitions =====================

text_log( x, y, w, h, title := "Peabianjay's Colourful Scrolling Text Log" )			; create scrolling text log window
{
    global
    local  b := "0x9ABCDE", c := 0, q, GuiID
    GUI_LINE_COUNT := ( h - 6 ) // 12
    list := array()
    Gui, ScrollingTextLog:New 
    Gui, ScrollingTextLog:Color, %b%, FE00FE
    Gui, ScrollingTextLog:Font, S10, Lucida Console

    loop, %GUI_LINE_COUNT%
    {
		list.push( { txt:"", colour:c, back:b } )
		y1 := A_Index * 12 - 10
		Gui, ScrollingTextLog:Add, Progress, Disabled Background%b% h12 w%w% x0 y%y1% , 0
		gui, ScrollingTextLog:Add, Text, BackgroundTrans xm yp wp c%c%, % list[A_Index].txt
    }
    Gui, ScrollingTextLog:Show, x%x% y%y% w%w% h%h%, %title%
    WinGet, GuiID, ID, A
    WinSet, AlwaysOnTop, On, ahk_id %GuiID%
    return GuiID
}

write( txt, colour := "0x0", line := 0, back := "0x9ABCDE" )					; write a line of text 
{
    global list, GUI_LINE_COUNT
    if ( !line )
		line := GUI_LINE_COUNT
    else if ( line < 0 or line > GUI_LINE_COUNT )
		return TRUE

    list[line] := { txt:txt, colour:colour, back:back }
    guicontrol, ScrollingTextLog:+Background%back%, msctls_progress32%line%
    guicontrol, ScrollingTextLog:+c%colour% , static%line%
    guicontrol, ScrollingTextLog:, Static%line%, % list[line].txt
    Gui, ScrollingTextLog:Submit, NoHide
    return FALSE
}

log( newline, newcolour := 0 )										; add a line of text at the bottom
{
    scroll( 1 )
    write( newline, newcolour )
}

scroll( num := 1 )												; scroll up
{
    global list, GUI_LINE_COUNT
    list.RemoveAt(1,num)
    loop, %num%
	list.push( { txt:"", colour:0, back:"0x9ABCDE" } )

    loop, %GUI_LINE_COUNT%
    {
		c :=  list[A_index].colour
		back := list[A_index].back
		guicontrol, ScrollingTextLog:+Background%back%, msctls_progress32%A_Index%
		guicontrol, ScrollingTextLog:+c%c%, static%A_Index%				; this works too!
		guicontrol, ScrollingTextLog:, static%A_Index%, % list[A_Index].txt		; experiment
    }
    Gui, ScrollingTextLog:Submit, NoHide
}

; ================= Non critical but handy stuff ==============

esc::exitapp
f12::reload
Pause::
    Pause, Toggle, 1
Return
#singleinstance force

I use these to monitor all those things I like to monitor in my AHK scripts. Since I tend to SPAM myself, I needed easy-to-use functions to add anywhere in my code, and colourful output, so I can quickly see stuff I wanna quickly see.
Yes, it is technically a GUI, but there isn't really any user interfacing, so I just call it a window. What's here is just to display info, not to do anything with it.
Works for me! Perhaps, it'll be useful to you.

Future Enhancements

None, probably. At least, none by me. I've posted small, functional & easy to use code. Nothing fancy, but does what I need it to do. However...

1) Save to file: I have this option, but it's not so bug-free, and I rarely use it. Good enough for me, but not good enough to share. :-)
2) View buttons: I added a few buttons so 1, or all but 1, type of logged information is visible. Works well enough, but it's "ugly" code. Used a hammer to drive in screws, ya know?
3) Clear/Close buttons
4) Vertical scroll bar
5) Options for controlling default background, font, font size, etc.
6) Option for scale-able window

Happy to hear suggestions on improving existing code, better ways of doing what I did, or features it aughta have. (Not that I'm planning on posting updates...but ya never know.)
Also happy to hear praise, adulation, fawning, and generally excessive reverence.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Qriist and 20 guests