|
rebeldev Beginner
Joined: 12 Mar 2003 Posts: 14
|
Posted: Sun Feb 01, 2004 1:16 am
colorize alias |
I have been working on an alias to take a string that I enter on the command line and spit it out with color codes in it. An example of it's use would be something like:
colorize This is a colorized string
and have it output (for now)
^mTh^Di^ws^D ^mis^D ^wa^D ^mco^Dl^wo^Dr^miz^De^wd^D ^mst^Dr^wi^Dn^mg
The pattern of the colors are ^m(2)^D(1)^w(1)^D(1) repeating where the number in () is the number of characters from the original string to be colorized by that code.
Here is the alias that I have so far that does not entirely work. Using this alias I expect the output to be as above, but it is spitting it out as:
^mT^Dh^wi^Ds^m i^Ds^w ^Da^m c^Do^wl^Do^mri^Dz^we^Dd^m s^Dt^wr^Di^mng
It is almost correct except that the first ^m only has one character after it. Here is the code that generates that:
colorstring = %-1
skip = 0
len = %len( %-1)
#loop 0,%len( %-1) {
#math temp (%i 5)
#math pos ($skip + %i)
#if ($temp == 0) {
colorstring = %insert( "^m", $colorstring, $pos)
#add skip 2
}
#if ($temp == 2) {
colorstring = %insert( "^D", $colorstring, $pos)
#add skip 2
}
#if ($temp == 3) {
colorstring = %insert( "^w", $colorstring, $pos)
#add skip 2
}
#if ($temp == 4) {
colorstring = %insert( "^D", $colorstring, $pos)
#add skip 2
}
}
#echo $colorstring
I may be making this more difficult for myself than it has to be, but this is the only way I could think to do it. Any help on this would be much appreciated. |
|
|
|
LightBulb MASTER
Joined: 28 Nov 2000 Posts: 4817 Location: USA
|
Posted: Mon Feb 02, 2004 6:48 am |
The first position in the string is 1, not 0. Change the initial value of $skip to 1 and it should work. $len isn't used and can be omitted.
colorstring = %-1
skip = 1
#loop 0,%len( %-1) {
#math temp (%i 5)
#math pos ($skip + %i)
#if ($temp == 0) {
colorstring = %insert( "^m", $colorstring, $pos)
#add skip 2
}
#if ($temp == 2) {
colorstring = %insert( "^D", $colorstring, $pos)
#add skip 2
}
#if ($temp == 3) {
colorstring = %insert( "^w", $colorstring, $pos)
#add skip 2
}
#if ($temp == 4) {
colorstring = %insert( "^D", $colorstring, $pos)
#add skip 2
}
}
#echo $colorstring
I usually run substitution loops backwards. It's easier that way, since there's no need to account for added characters. |
|
|
|
rebeldev Beginner
Joined: 12 Mar 2003 Posts: 14
|
Posted: Thu Feb 12, 2004 6:35 am |
Ok, that fixed that problem, but now I have a new problem. If the string to be colorized has a comma in it then it seems to stop processing for some reason. For example, given the string: blah, blah blah it returns ^mbl^Da^wh^D, blah blah^x. I can't for the life of me figure out why it's doing that. I believe it has something to do with the way the %insert is being parsed, but I can't be certain of that.
On a side note: I see what you mean, Lightbulb, about running the loop backwards, I'll have to give that a shot as it seems it would make life a lot easier. Thanks for the suggestion and the help. :) |
|
|
|
LightBulb MASTER
Joined: 28 Nov 2000 Posts: 4817 Location: USA
|
Posted: Thu Feb 12, 2004 6:40 pm |
Since this alias is for user-input, the simplest solution is not to put commas in your text.
Comma is the character used to separate the parameters of a function. This makes it difficult for functions to handle text which contains commas, and the more functions which need to handle that text the more difficult it becomes.
In the case of this alias, there are only two functions involved, %len and %insert, and they aren't nested so it shouldn't be too difficult. Experiment with delimiters, such as double-quotes "", braces {}, brackets [], and parentheses (), until you find one which gives the desired results. You won't necessarily want the same delimiter in both functions. |
|
|
|
Danlo Magician
Joined: 28 Nov 2003 Posts: 313 Location: Australia
|
Posted: Fri Feb 13, 2004 1:54 am |
Try this:
#alias colorize {#var count 0;#var string {%-1};#LOOP %len($string) {#add count 1;#IF ($count=5) {#var count 1} {};#var newstring {%concat($newstring,%case($count,^m,^D,^w,^D),%copy($string,%i,1))}};say $newstring;#var newstring {%null}} |
|
|
|
rebeldev Beginner
Joined: 12 Mar 2003 Posts: 14
|
Posted: Fri Feb 13, 2004 6:46 pm |
Thanks so much Danlo ... with a minor modification (the pattern of colors was off since your alias only accounted for one character with the ^m color code when I wanted two) that did exactly what I needed it to and commas don't effect it.
*bows to the gurus* |
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|