A FUNction to Align Text
February 1, 2006 Hey, Ted
I read with interest your article on the IIF function. I have a function called alignText that I use in just about every program. I use it primarily to center headings, but have found it useful for right adjust as well. I have had this for a long time, so it is in traditional form, not freeform. Notice that there are three optional parameters. The alignment defaults to centering, and then two additional parameters are used to change the display attribute if I want to.
— Gordon Larkins, ASK Senior Technical Consultant
It was nice of Gordie to share his routine with the rest of us. Centering text is a common task in green-screen and report programming. Everybody needs one of these routines in the old toolbox.
Here’s the procedure prototype for alignText. There are several ways to handle the storage of prototypes, but I think the easiest is to place the prototype in a member of a source physical file called PROTOTYPES. All callers will need to use a /COPY or /INCLUDE directive to bring the prototype in at compilation.
D alignText pr 132 D inText 132 value D inLen 10i 0 value D inAlign 1 value options(*nopass) D inBegAtt 1 value options(*nopass) D inEndAtt 1 value options(*nopass)
Here’s the source code for an ALIGNTEXT module.
H nomain D/copy prototypes,alignText P alignText b export D alignText pi 132 D inText 132 value D inLen 10i 0 value D inAlign 1 value options(*nopass) D inBegAtt 1 value options(*nopass) D inEndAtt 1 value options(*nopass) D outText s 132 D cx s 10i 0 C Do C If inText = *Blank C or inLen < %Len(%Trim(inText)) C Eval outText = inText C Leave C Endif C Select C When %Parms > 4 C Eval outText = inBegAtt + %Trim(inText) C + inEndAtt C When %Parms > 3 C Eval outText = inBegAtt + %Trim(inText) C Other C Eval outText = %Trim(inText) C Endsl * Right Align Text C If %Parms > 2 and inAlign = 'L' C Eval cx = inLen - %Len(%Trim(outText)) + 1 C Eval %Subst(outText : cx) = outText C Eval %Subst(outText : 1 : cx - 1) = *Blank C Leave C Endif * Center Text C Eval cx = (inLen - %Len(%Trim(outText))) / 2 C ' ' Cat(p) outText : cx outText C Enddo C Return outText P align Text e
I recommend that the module be converted to a service program. Anyone who already has a service program for the manipulation of character strings might want to add this routine to that service program.
Gordie also sent a couple of code fragments showing how to call alignText. The first one does not use my IIF routine.
D hdrOne s 45a D parmInqMnt s 1a D/copy prototypes,alignText /free *inlr = *on; hdrOne = 'Customer Master'; if parmInqMnt = 'M'; hdrOne = %Trim(hdrOne) + ' Maintenance'; else; hdrOne = %Trim(hdrOne) + ' Inquiry'; endif; hdrOne = alignText(hdrOne : %Len(hdrOne));
The second fragment uses IIF, which shortens the code considerably. This is a good demonstration of how the creation of well-designed, reusable subprocedures can reduce the programming effort and thereby increase productivity.
D hdrOne s 45a D parmInqMnt s 1a D/copy prototypes,alignText D/copy prototypes,iif /free *inlr = *on; hdrOne = 'Customer Master'; hdrOne = alignText('Customer Master ' + iif(parmInqMnt = 'M' : 'Maintenance' : 'Inquiry') : %Len(hdrOne));
These program fragments center the text. For right-adjust, pass an L into the third parameter.
hdrOne = alignText(hdrOne : %Len(hdrOne):'L');
To learn more about the P-codes that can be passed into the fourth and fifth parameters, see the discussion of P-field values in the online DDS reference manual. The following command causes the hdrOne variable to blink on monochrome terminals and to be displayed in red on color terminals.
hdrOne = alignText(hdrOne : %Len(hdrOne):'C': x'28': x'20');