By registering with us, you'll be able to discuss, share and exchange private messages with other members of our community.

SignUp Now!

PrintCenter - Print text in center of console

If I wanted text to appear in the center of the console, this will do it. The math is not that complex :-) Take the console width in columns. Divide by 2. Take the text. Divide by 2. Subtract the latter from the former and add that many spaces to the front.

You can see what's it's doing under the covers by calling it with the /D switch. It does use a few of my other functions so you'll need those two.

:: PrintCenter [/W=CustomWidth] [/D] TextToCenter
:: Print the given text in the center of the console display using spaces.  If the
:: optional Width swith [/W] is given, use that as the width to center within starting
:: from the left.
:: /D will enter debug mode for the duration of the function
:: Example:   PrintCenter "This Is In The Center"
:: Example:   PrintCenter /W=20 "This Is In The Center"
PrintCenter {

    GetOpt %*

    iff defined Option_? .OR. not defined PARAM_1 then
        echo Usage:  PrintCenter [/W=CustomWidth] [/D] "TextToCenter"
        echo            /W: Custom width in columns
        echo            /D: Turn on DebugMode
        echo            /?: Display this help information
        goto EndProgram

    if defined OPTION_D set DEBUG=1

    :: Determine if the optional width parmameter has been provided.  If not use column width.
    iff defined OPTION_W then
        set CWidth=%OPTION_W
        set CWidth=%_Columns
    :: Determine how many spaces to add at the front to center text
    :: Logic:  Take 1/2 of width - 1/2 of text length
    set TextLen=%@Len[%@Unquote[%PARAM_1]]
    set SpacesToAdd=%@Int[%@Eval[(%CWidth / 2) - (%TextLen / 2)]]

    DebugPrint "Screen Width in Columns:  %CWidth"
    DebugPrint "String: %@Unquote[%PARAM_1]"
    DebugPrint "String Length: %TextLen
    DebugPrint "Spaces to Add at Front: %SpacesToAdd"
    DebugPrint "Formula:  int( (%CWidth / 2) - (%TextLen / 2) ) = %SpacesToAdd
    DebugPrint "                                                                                            1"
    DebugPrint "  1         2         3         4         5         6         7         8         9         0"
    DebugPrint "890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"

    echo %@repeat[ ,%SpacesToAdd]%@unquote[%PARAM_1]

    if defined OPTION_D unset DEBUG
Last edited:
for a real fun time you can print double-height VT100 style with ANSI codes