So I'm trying to make an HTML rainbow chat script where I ask for user input with read-host and then it takes that input (E.g "Testers will test") then it takes every letter and adds something before and after it that way I can have something like
[HTMLCODE]T</>[HTMLCODE]e</>[HTMLCODE]s</>[HTMLCODE]t</>
If the user inputted Test^
So far I have the worst script ever that only allows 50 characters and you have to use the 50 characters:
$FinalString = $rainbow.ToCharArray()
$Final = $r $FinalString[0] $y $FinalString[1] $g $FinalString[2] $b $FinalString[3] $p $FinalString[4] $r $FinalString[5] $r $FinalString[7] $y $FinalString[8] $g $FinalString[9] $b $FinalString[10] $p $FinalString[11] $r $FinalString[12] $r $FinalString[13] $y $FinalString[14] $g $FinalString[15] $b $FinalString[16] $p $FinalString[17] $r $FinalString[18] $r $FinalString[19] $y $FinalString[20] $g $FinalString[21] $b $FinalString[22] $p $FinalString[23] $r $FinalString[24] $r $FinalString[25] $y $FinalString[26] $g $FinalString[27] $b $FinalString[28] $p $FinalString[29] $r $FinalString[30] $r $FinalString[31] $y $FinalString[32] $g $FinalString[33] $b $FinalString[34] $p $FinalString[35] $r $FinalString[36] $r $FinalString[37] $y $FinalString[38] $g $FinalString[39] $b $FinalString[40] $p $FinalString[41] $r $FinalString[42] $r $FinalString[43] $y $FinalString[44] $g $FinalString[45] $b $FinalString[46] $p $FinalString[47] $r $FinalString[48] $r $FinalString[49] $y $FinalString[50]
Write-Host $Final"
I have no idea if this is even possible and I feel like everything I thought I knew about PowerShell is gone. Even a point in the right direction would help I'm sure I'm missing something obvious.
EDIT: Here is what I made in Python as an example of what I'm trying to do but I need this in PS I've been trying to learn PowerShell
import pyperclip
def main():
t = input("What is the text you would like to colorfy!?: ")
c = "rainbow"
f = ['</>']
if c == ("rainbow"):
d = 0
e = list(t)
for i in range (0,len(e)):
if d == 4:
f.append("<HTMLCODEEXAMPLE>" e[i] "</>")
d = 0
elif d == 3:
f.append("<HTMLCODEEXAMPLE>" e[i] "</>")
d = 4
elif d == 2:
f.append("<HTMLCODEEXAMPLE>" e[i] "</>")
d = 3
elif d == 1:
f.append("<HTMLCODEEXAMPLE>" e[i] "</>")
d = 2
elif d == 0:
f.append("<HTMLCODEEXAMPLE>" e[i] "</>")
d = 1
f.append("<HTMLCODEEXAMPLE></>")
G = str(f)
G = G.replace("[","")
G = G.replace("]","")
G = G.replace("'","")
G = G.replace("[","")
G = G.replace(", ","")
print(G)
pyperclip.copy(G)
f.clear()
main()
main()
CodePudding user response:
If you want to cycle through a fixed number of items, use the remainder operator % to "wrap around" and go back to zero:
$word = 'Rainbow'
$colors = -split 'Red Orange Yellow Green Blue Indigo Violet'
$htmlBuilder = [System.Text.StringBuilder]::new()
for($i = 0; $i -lt $word.Length; $i )
{
# pick color, use % to wrap around at the end of the $colors array
$color = $colors[$i % $colors.Length]
# append html fragment
$htmlBuilder = $htmlBuilder.AppendFormat('<span style="color: {0}">{1}</span>', $color, $word[$i])
}
# output html string
$htmlBuilder.ToString()
Which produces:
<span style="color: Red">R</span><span style="color: Orange">a</span><span style="color: Yellow">i</span><span style="color: Green">n</span><span style="color: Blue">b</span><span style="color: Indigo">o</span><span style="color: Violet">w</span>
To show the color picker wrapping around and going back to red, here's the output with $word = 'StackOverflow':
<span style="color: Red">S</span><span style="color: Orange">t</span><span style="color: Yellow">a</span><span style="color: Green">c</span><span style="color: Blue">k</span><span style="color: Indigo">O</span><span style="color: Violet">v</span><span style="color: Red">e</span><span style="color: Orange">r</span><span style="color: Yellow">f</span><span style="color: Green">l</span><span style="color: Blue">o</span><span style="color: Indigo">w</span>
CodePudding user response:
Give this a try, there is no need for a for loop in this case. You can use a foreach loop (.foreach(..) method in this case) to loop over each character and add your $x and $y tags on each element.
If you want all as single string instead of a multi-line string, use the .Append(..) method instead of .AppendLine(..) method of the StringBuilder.
Note, this assumes that $x and $y have the same number of elements.
$string = "Testers will test"
$x = '<tag1>', '<tag2>', '<tag3>', '<tag4>'
$y = '</tag1>', '</tag2>', '</tag3>', '</tag4>'
$i = 0
$builder = [System.Text.StringBuilder]::new()
$string.ToCharArray().ForEach({
process {
$null = $builder.AppendLine(('{0}{1}{2}' -f $x[$i], $_, $y[$i ]))
if($i -eq $x.Count) { $i = 0 }
}
end {
$builder.ToString()
}
})
