|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Mar 10, 2009 7:59 am
Padding spaces, centering text, would like a better way... |
Taken from here.
Fang Xianfu wrote: |
Using any number:
%format(%concat("&",%1,"s"),%2)
For centre alignment, you can either require half the line length as a parameter, or just halve it.
Using any character:
%subregex(%format("&40s",%1),"^\s+",%2)
%subregex(%format("&-40s",%1),"\s+$",%2)
If you're sure that the string you're using will never contain spaces, you could just use %replace or %subchar instead. |
The topic was regarding spacing, and the OP said he wanted to do center spacing. I came up with a way to handle center spacing, and Fang pointed out the above works much better, except... it doesn't.
Currently, this is what I'm doing to center text, using any character as a marker:
Code: |
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
<func name="padcent" copy="yes">
<value>#RETURN %if(%len(%repeat($Character,%eval(($Length-%len($String))/2))$String%repeat($Character,%eval(($Length-%len($String))/2)))!=$Length,%repeat($Character,%eval(($Length-%len($String))/2))$String%repeat($Character,%eval(1+($Length-%len($String))/2)),%repeat($Character,%eval(($Length-%len($String))/2))$String%repeat($Character,%eval(($Length-%len($String))/2)))
</value>
<arglist>String, Length, Character</arglist>
</func>
</cmud>
|
As Fang mentioned, that is very horrible, indeed, but it's the only way I can see that does exactly what I want it to - pads it with whatever character I want it to and centers it perfectly between a variable length. Fang's way looked loads easier, but there's a problem.
Code: |
%subregex(" Test", "^\s+", "-") |
is only going to return:
-Test
when I'd want it to return:
----------Test
because it's only replacing the first space (as expected).
Now, I can't use %replace or %subchar because if the text contains spaces, then those will get removed, too. So, question is, how can we make this work? I'm open to any and all suggestions, and would really like to compact that function down quite a bit.
Thanks for answers!
Charneus |
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Tue Mar 10, 2009 10:40 am |
$Res=%repeat($Character,$Length)
$Res=%insert($String,$Res,($Length-%len($String))/2)
$Res=%left($Res,$Length)
#RETURN $Res |
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Mar 10, 2009 2:34 pm |
Ah... quite nice indeed. However, I did have to make one slight change.
I had to add 1 to the ($Length-$String/2) bit to make it centered.
Thanks!
Charneus |
|
|
|
calesta Apprentice
Joined: 07 Dec 2008 Posts: 102 Location: New Hampshire, USA
|
Posted: Tue Mar 10, 2009 3:52 pm |
I believe you can also still make the subregex work by using the length of the matched string as the number of times to repeat the desired character to form the replacement string:
Code: |
%subregex(" Test", "^(\s+)", "%repeat(-, %len('\1'))") |
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Mar 10, 2009 4:25 pm |
That doesn't work for centering, because what it does is takes:
#say @padcent("Testing",9,"-")
and instead of -Testing-, it returns:
---------Testing
(or some sort).
But nice idea. I had tried that already. ;)
Charneus |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Tue Mar 10, 2009 4:49 pm |
*feels stupid now* :P
|
|
|
|
calesta Apprentice
Joined: 07 Dec 2008 Posts: 102 Location: New Hampshire, USA
|
Posted: Tue Mar 10, 2009 5:47 pm |
charneus wrote: |
That doesn't work for centering, because what it does is takes:
#say @padcent("Testing",9,"-")
and instead of -Testing-, it returns:
---------Testing
(or some sort).
|
Yeah, Fang's original algorithm produced two strings though. You would use the first subregex I posted for the first string along with a second one using (\s+)$ for the pattern for the second string and then concatenate them just like Fang originally posted. It doesn't matter anyway though, Vijilante's is definitely a more efficient way to do it. |
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Tue Mar 10, 2009 6:16 pm |
Still doesn't work the way you're describing, calesta. Let's go by example.
#say @padcent("Testing",21,"-")
I'd want it to look like this:
-------Testing-------
which is centered among a 21 character line. Using what you suggested, it'd return:
---------------------Testing---------------------
unless I created the function to set it correctly by using %insert, like Viji did. :P
Charneus |
|
|
|
|
|
|
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
|
|