Page 1 of 1

Alignment of data in a "text" box

Posted: 15 Aug 2014 00:44
by klownboy
Hi, this is probably a dorky question, but is there a technique for assuring alignment of data in a "text" box. I have information I want to provide to the user when rebuilding of thumbnails is complete using variables obtained in the script. I'm currently using tabs between the before and after data, but it's misaligned at times due to the size of the data. See capture below.

Code: Select all

   $DBsize = "[$T_size_sel]<crlf>Before: $thumbDBsize_1<tab><tab>After: $thumbDBsize_2<crlf>";
	$complete_DBsize = $complete_DBsize . $DBsize . "<crlf>";
   } 
   $complete_DBsize = "Folder [$path] Thumbnail Database Size before and after rebuild:" . "<crlf 2>" . "Thumbnail or Tile size:" . "<crlf 2>" . $complete_DBsize;
   text "$complete_DBsize", 650, 550, "Thumbnail Database Rebuild";
XY_thumbs_text alignment.PNG
Thanks,
Ken

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 01:25
by highend
What about doing it mathematically?
E.g. by storing the strings and results in variables and using strlen() on them
and then filling the space with spaces via strrepeat()...

Non working code:
$maxLength = 40;
$DBsize = "Before: $thumbDBsize_1";
$DBsize2 = "After: $thumbDBsize_2";
$DBsizeL = strlen($DBsize);
$DBsize2L = strlen($DBsize2);
$lineFillerSize1 = $maxLength - ($DBsizeL + $DBsizeL2);

$line1 = $DBsize . strrepeat(" ", $lineFillerSize1) . $DBsize2;

Just as an idea (it's late here...)

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 02:09
by klownboy
Thanks highend, I'll give it a try in the AM...I've been on the computer way too long already. :appl:

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 11:57
by klownboy
Thanks again highend that technique worked fine. I made only one adjustment other than variable names. Using it as you described will line up the end of the "After: DBsize2 number" as opposed to lining up the beginning of "After..." It wasn't necessary to use the After $DBsize2 strlen at all, only the before. Set maxline length to a value and subtract the strlen of $DBsize1. It ended up something like this...

Code: Select all

       $maxLength = 45;
       $DBsizeL1 = strlen($thumbDBsize_1);
       $DBlineFillerSize = $maxLength - $DBsizeL1;
       $DBline = [$T_size_sel]<crlf>Before: $thumbDBsize_1 . strrepeat(" ", $DBlineFillerSize) . After: $thumbDBsize_2 . <crlf>;
	    $complete_DBsize = $complete_DBsize . $DBline . "<crlf>"; 	
Thanks,
Ken

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 12:07
by highend
Yeah, you're right. Normally I test what I write but it was late (1:30 AM) and I was tired :)

This method requires only a few additional code lines but works for all case of formatting (tab stops aren't a reliable thing in xy dialogs...)

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 12:43
by PeterH
I have a method that's a bit different.

- define the length of field1 as $lg1=12; (you might have several fields of different length)
- for each line concatenate the text for the field with StrRepeat(' ', $lg1) :arrow: now length is at least $lg1
- get SubStr of this with Substr($v1 . StrRepeat(' ', $lg1), 0, $lg1) :arrow: length is exact $lg1
- if original string is too long it will be truncated :!:

Example:

Code: Select all

 $x = '1234567890';  // only demo to show column number
 $lg1 = 12;  // length of field1 (the only variable field here)

 $v1 = 'abc';   // list of values (of different length)
 $v2 = 'something';
 $v3 = 'too long string here';

 Text "$x$x$x<crlf 2>" 
    . 'LinePrefix: ' . Substr($v1 . StrRepeat(' ', $lg1), 0, $lg1) . "Field2<crlf>"
    . 'LinePrefix: ' . Substr($v2 . StrRepeat(' ', $lg1), 0, $lg1) . "Field2<crlf>"
    . 'LinePrefix: ' . Substr($v3 . StrRepeat(' ', $lg1), 0, $lg1) . "Field2<crlf>" ;
Sure you may build line for line in a loop and concatenate the lines, or whatever...
You might even use a subroutine for building a line. 8) (But XY and user subroutines - dream :P )

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 13:41
by klownboy
Hey that's cool PeterH, you've essentially boiled it down to a one-liner especially if you're in a one case scenario, you only need to plug in a number instead of establishing the $lg1 variable. So in my case...

Code: Select all

   $DBline = [$T_size_sel]<crlf> . Substr(Before: $thumbDBsize_1 . StrRepeat(' ', 45), 0, 45) . After: $thumbDBsize_2<crlf>;  // do this line and the next in the foreach loop to build the text box
   $complete_DBsize = $complete_DBsize . $DBline . "<crlf>";
Thank you,
Ken

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 17:52
by PeterH
klownboy wrote:Hey that's cool PeterH, you've essentially boiled it down to a one-liner especially if you're in a one case scenario, you only need to plug in a number instead of establishing the $lg1 variable. So in my case...

Code: Select all

   $DBline = [$T_size_sel]<crlf> . Substr(Before: $thumbDBsize_1 . StrRepeat(' ', 45), 0, 45) . After: $thumbDBsize_2<crlf>;  // do this line and the next in the foreach loop to build the text box
   $complete_DBsize = $complete_DBsize . $DBline . "<crlf>";
Thank you,
Ken
...where it is not neccessary for the padding to be as long as the field - it may be longer, and should be placed once in a variable. No difference for one reference, but for my example...

Code: Select all

 $x = '1234567890';  // only demo to show column number
 $pad = StrRepeat(' ', 100);  // padding for at least 100 chars
 $lg1 = 12;  // length of field1 (the only variable field here)

 $v1 = 'abc';   // list of values (of different length)
 $v2 = 'something';
 $v3 = 'too long string here';

 Text "$x$x$x<crlf 2>"
    . 'LinePrefix: ' . Substr($v1 . $pad, 0, $lg1) . "Field2<crlf>"
    . 'LinePrefix: ' . Substr($v2 . $pad, 0, $lg1) . "Field2<crlf>"
    . 'LinePrefix: ' . Substr($v3 . $pad, 0, $lg1) . "Field2<crlf>" ;
Especially makes sense if several fields of different length are used.

This way it's easyer to type, and faster for execution. (StrRepeat() only once.)
$v1 . $pad
might be replaced by
"$v1$pad"
but that's a question of taste :whistle:

EDIT: addon 8)
If it's expected for the variable to sometimes fill the whole field, it will be connected directly to the next string. in this case add a space character in front of the next string. :P

Re: Alignment of data in a "text" box

Posted: 15 Aug 2014 20:57
by klownboy
Thanks again PeterH now even better.