[Gui] How to slice words from a merged string to get colored text output ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

[Gui] How to slice words from a merged string to get colored text output ?

15 Sep 2018, 07:50

Hello,
Here is a gui with 3 layered control text and monospaced terminal font.

layer 0 is white
layer 1 red
layer 2 green

How to slice "red words" and "green words" from the white text in order to display a colored output ? :think:

layer0:= white text
layer1:=["worry", "face", "head", "late" , "trouble", "bed", "down"] ; red layer words
layer2:=["happy","song", "smile", "life"] ; green layer words

thanks

Code: Select all


#singleinstance, force

string=
(
Here is a little song I wrote
You might want to sing it note for note
Don't worry be happy
In every life we have some trouble
When you worry you make it double
Don't worry, be happy

Ain't got no place to lay your head
Somebody came and took your bed
Don't worry, be happy
The land lord say your rent is late
He may have to litigate
Don't worry, be happy
Look at me I am happy
Don't worry, be happy

Here I give you my phone number
When you worry call me
I make you happy
Don't worry, be happy
Ain't got no cash, ain't got no style
Ain't got not girl to make you smile
But don't worry be happy
Cause when you worry
Your face will frown
And that will bring everybody down
So don't worry, be happy (now)

There is this little song I wrote
I hope you learn it note for note
Like good little children
Don't worry, be happy
Listen to what I say
In your life expect some trouble
But when you worry
You make it double
Don't worry, be happy
Don't worry don't do it, be happy
Put a smile on your face
Don't bring everybody down like this
Don't worry, it will soon past
Whatever it is
Don't worry, be happy

lyric: Bob MARLEY
)

gui, font, s18 cwhite, terminal
gui, add,text, W500 H900 backgroundtrans vlayer0, % string			; white layer
gui, font, s18 cred, terminal
gui, add,text, xp yp W500 H900 backgroundtrans vlayer1, % string	; red layer
gui, font, s18 clime, terminal
gui, add,text, xp yp W500 H900 backgroundtrans vlayer2, % string 	; green layer

layer1:=["worry", "face", "head", "late" , "trouble", "bed", "down"] ; red layer
layer2:=["happy","song", "smile", "life"] 							 ; green layer

gui, color, black

gui, show

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Gui] How to slice words from a merged string to get colored text output ?

15 Sep 2018, 14:05

SpeedMaster wrote:Hello,
Here is a gui with 3 layered control text and monospaced terminal font.

layer 0 is white
layer 1 red
layer 2 green

How to slice "red words" and "green words" from the white text in order to display a colored output ? :think:

layer0:= white text
layer1:=["worry", "face", "head", "late" , "trouble", "bed", "down"] ; red layer words
layer2:=["happy","song", "smile", "life"] ; green layer words
You just need to change all characters except for the words (and vertical tabs) you want in the colored layers to spaces.

Code: Select all

#singleinstance, force

string=
(
Here is a little song I wrote
You might want to sing it note for note
Don't worry be happy
In every life we have some trouble
When you worry you make it double
Don't worry, be happy

Ain't got no place to lay your head
Somebody came and took your bed
Don't worry, be happy
The land lord say your rent is late
He may have to litigate
Don't worry, be happy
Look at me I am happy
Don't worry, be happy

Here I give you my phone number
When you worry call me
I make you happy
Don't worry, be happy
Ain't got no cash, ain't got no style
Ain't got not girl to make you smile
But don't worry be happy
Cause when you worry
Your face will frown
And that will bring everybody down
So don't worry, be happy (now)

There is this little song I wrote
I hope you learn it note for note
Like good little children
Don't worry, be happy
Listen to what I say
In your life expect some trouble
But when you worry
You make it double
Don't worry, be happy
Don't worry don't do it, be happy
Put a smile on your face
Don't bring everybody down like this
Don't worry, it will soon past
Whatever it is
Don't worry, be happy

lyric: Bob MARLEY
)

LayerGreen:="happy|song|smile|life"			 ; green layer needle
LayerRed:="worry|face|head|late|trouble|bed|down"	 ; red layer needle

X:=1, String_Green := ""
while (X := RegExMatch(String, "(.*?)(" LayerGreen "|\v)", M, X+StrLen(M)))
	String_Green .= Spaces(StrLen(M1)) M2

X:=1, String_Red := ""
while (X := RegExMatch(String, "(.*?)(" LayerRed "|\v)", M, X+StrLen(M)))
	String_Red .= Spaces(StrLen(M1)) M2

gui, font, s18 cwhite, terminal
gui, add,text, W500 H900 backgroundtrans vlayer0, % String			; white layer
gui, font, s18 cred, terminal
gui, add,text, xp yp W500 H900 backgroundtrans vlayer1, % String_Red	; red layer
gui, font, s18 clime, terminal
gui, add,text, xp yp W500 H900 backgroundtrans vlayer2, % String_Green 	; green layer

gui, color, black

gui, show

Esc::ExitApp

Spaces(x)
{
	Loop %x%
		Result .= " "
	return Result
}
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: [Gui] How to slice words from a merged string to get colored text output ?

15 Sep 2018, 15:04

You might look at just using an ActiveX control that then allows you to use HTML, which gives you much easier control over appearance.

Code: Select all

Html = 
(
<font size="3" color="red">This is some red text!</font><br>
<font size="2" color="blue">This is some blue text!</font><br>
<font face="verdana" color="green">This is some green text!</font><br> 
<font color="red">red</font><font color="blue">blue</font><font color="green">green</font><br> 

)

Gui, Add, ActiveX, w200 h200 vDisplay, HTMLFile
ComObjConnect(Display, "Doc_")
Display.body.innerHTML := Html
Gui, Show

; Demonstrate how easy it is to change the text in an ActiveX control
Loop
{
	Html .= "<br>" A_Index
	Sleep 1000
	Display.body.innerHTML := Html
}
return

GuiClose:
ExitApp
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [Gui] How to slice words from a merged string to get colored text output ?

16 Sep 2018, 18:41

FanaticGuru wrote: You just need to change all characters except for the words (and vertical tabs) you want in the colored layers to spaces.
It's exactly what I was looking for. Thank you. :clap: :thumbup:
I made a function with it. SliceWord(text, words) It works well but it still relies on the Space() function. Can you merge the 2 functions ? :think:

Here is a working example script

Code: Select all

string=
(
Here is a little song I wrote
You might want to sing it note for note
Don't worry be happy
In every life we have some trouble
When you worry you make it double
Don't worry, be happy

Ain't got no place to lay your head
Somebody came and took your bed
Don't worry, be happy
The land lord say your rent is late
He may have to litigate
Don't worry, be happy
Look at me I am happy
Don't worry, be happy

Here I give you my phone number
When you worry call me
I make you happy
Don't worry, be happy
Ain't got no cash, ain't got no style
Ain't got not girl to make you smile
But don't worry be happy
Cause when you worry
Your face will frown
And that will bring everybody down
So don't worry, be happy (now)

There is this little song I wrote
I hope you learn it note for note
Like good little children
Don't worry, be happy
Listen to what I say
In your life expect some trouble
But when you worry
You make it double
Don't worry, be happy
Don't worry don't do it, be happy
Put a smile on your face
Don't bring everybody down like this
Don't worry, it will soon past
Whatever it is
Don't worry, be happy

lyric: Bob MARLEY
)

msgbox, slice : "note" "is" "style" "trouble"
msgbox, % sliceword(string, "note|is|style|trouble")
msgbox, slice : letter "n"
msgbox, % sliceword(string, "n")
msgbox, slice : "But"
msgbox, % sliceword(string, "But")
msgbox, slice : Empty
msgbox, % sliceword(string, "")

SliceWord(text, words) {
X:=1, output := ""
if (text) && (!words)
return regexreplace(text, "(?!\n).", " ")

while (X := RegExMatch(text, "(.*?)(" words "|\v)", M, X+StrLen(M)))
	output .= Spaces(StrLen(M1)) M2
return output
}

Spaces(x)
{
	Loop %x%
		Result .= " "
	return Result
}

I'm also looking for a line slicing function sliceLine(text, Lines) or a function that allow two operations at same time
slice(text, words, lines) Any idea on how to do that ?
Merging back two sliced sentences would be also great.
FanaticGuru wrote: You might look at just using an ActiveX control that then allows you to use HTML, which gives you much easier control over appearance.
Thank you for your suggestion and interesting example. How to change the background color to black in the html file?
I'm trying to simulate a fake colored console with a dynamic response using only gui controls. I doubt I can do it in html.
Here is an example of what I'm trying to achieve:
(Any suggestions on how to improve this code are welcome) :D

Code: Select all

#SingleInstance, force

Menu=
(
Use the arrow keys to select a fruit

Fruits Menu:
************
Apple
Pear
Banana
Orange

)
j1=Jake came rushing in to see his Dad. `n"Dad!" he puffed, "is it true that an apple a day keeps the doctor away?" `n"That s what they say", said his Dad. `n"Well, give me an apple quick ?" `n"I have just broken the doctor's window!"
j2=Why can't you connect 2 speakers at the same time to an apple device? `n`nThey work in pears.
j3:="What do you say if someone steps on a banana peel?`nWell I guess he didn't find that appealing!" 
j4:="Why did the blonde girl stare at the carton of orange juice? `n`nIt said concentrate."

fruits:=["Apple", "Pear", "Banana", "Orange"]
jokes:=[j1,j2,j3,j4]
colors:=["red", "green", "yellow", "F9812A"]

gui, font, s18 cwhite, terminal
gui, add,text, W500 H500 backgroundtrans vlayer0,  % strreplace(Menu, "_", " ")			; white layer
gui, font, s18 cred, terminal
gui, add,text, xp yp Wp Hp backgroundtrans -hidden vlayer1,	
gui, font, s18 clime, terminal
gui, add,text, xp yp Wp Hp backgroundtrans vlayer2, 

gui, color, black
gui, +resize
gui,  show

drawtext("layer1", SliceWord(menu, fruits[1]), "red")

i:=1
up::
(i>1) ? --i 
drawtext("layer2")
drawtext("layer1", SliceWord(menu, fruits[i]), "red")
return

down::
(i<fruits.maxindex()) ? ++i ;: (i:=1)
drawtext("layer2")
drawtext("layer1", SliceWord(menu, fruits[i]), "red")
return

~enter::
drawtext("layer1", sliceword(menu, fruits[i]), "fuchsia")
;drawtext("layer2", sliceword(menu,"") "`nYou selected " fruits[i], colors[i])
drawtext("layer2", sliceword(menu,"") "`n" jokes[i], colors[i])
return


SliceWord(text, words) {
X:=1, output := ""
if (text) && (!words)
return regexreplace(text, "(?!\v).", " ")

while (X := RegExMatch(text, "(.*?)(" words "|\v)", M, X+StrLen(M)))
	output .= Spaces(StrLen(M1)) M2
return output
}

Spaces(x)
{
	Loop %x%
		Result .= " "
	return Result
}

drawtext(varname, texttodraw:="", color:="")
{
 global
guicontrol,, %varname%, %texttodraw%
if (color)
ColorText(varname, color)
}

ColorText(cell_to_paint, color:="red")
{
 GuiControl, +c%color%  , %cell_to_paint%
 GuiControl, MoveDraw, % cell_to_paint
}

guiclose: 
esc:: 
exitapp 
return
Cheers
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: [Gui] How to slice words from a merged string to get colored text output ?

17 Sep 2018, 11:42

thank you for the examples
maybe Fanaticguru , a question about HTML and ACTIVEx
I woul'd like to make a GUI with ACTIVEX to play a youtube video

<iframe width="1280" height="720" src="https://www.youtube.com/embed/yKbzBGntI8Q" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ccqcl, Descolada, Google [Bot], inseption86, mebelantikjaya and 337 guests