CMake Logo

Небольшой кусочек кода, который позволяет выводить текст с возможностью выравнивания по центру, по правому и левому краям.

function(pretty_fill TITLE MAXLEN FILLER ALIGN)
    string(LENGTH ${TITLE} TITLE_LENGTH)
    math(EXPR FILLER_COUNT "${MAXLEN} - ${TITLE_LENGTH}")
    if (${FILLER_COUNT} GREATER 0)
        if (${ALIGN} STREQUAL "-")
            math(EXPR LEFT_COUNT "${FILLER_COUNT} / 2")
            math(EXPR RIGHT_COUNT "${FILLER_COUNT} - ${LEFT_COUNT}")
            string(REPEAT ${FILLER} ${LEFT_COUNT} L)
            string(REPEAT ${FILLER} ${RIGHT_COUNT} R)
            set(RESULT "${L}${TITLE}${R}" PARENT_SCOPE)
        elseif(${ALIGN} STREQUAL ">")
            string(REPEAT ${FILLER} ${FILLER_COUNT} F)
            set(RESULT "${F}${TITLE}" PARENT_SCOPE)
        else()
            string(REPEAT ${FILLER} ${FILLER_COUNT} F)
            set(RESULT "${TITLE}${F}" PARENT_SCOPE)
        endif()
    else()
        set(RESULT "${TITLE}" PARENT_SCOPE)
    endif()
endfunction()

function(pretty_print LEFT TITLE RIGHT FILLER ALIGN)
    set(HEADER_SIZE 50) # max header size

    string(LENGTH "${LEFT}${RIGHT}" BORDER_SIZE)
    math(EXPR COUNT "${HEADER_SIZE} - ${BORDER_SIZE}")
    pretty_fill("${TITLE}" ${COUNT} ${FILLER} ${ALIGN})

    message(STATUS "${LEFT}${RESULT}${RIGHT}")
endfunction()

Пример использования для вывода текста по центру с заполнением символом «*» и шириной 50:

pretty_print("/" " Text on center " "\\" "*" "-")
pretty_print("| " " Left aligned 1 " " |" "." "<")
pretty_print("|" "Left aligned 2" "|" "." "<")
pretty_print("|" " Left aligned 3 " " |" " " "<")
pretty_print("| " " Right aligned 1 " "|" "+" ">")
pretty_print("|" "Right aligned 2" "|" "+" ">")
pretty_print("| " "Right aligned 3" " |" " " ">")
pretty_print("\\" "*" "/" "*" "-")
/**************** Text on center ****************\
| Left aligned 1 ............................... |
|Left aligned 2..................................|
| Left aligned 3                                 |
| ++++++++++++++++++++++++++++++ Right aligned 1 |
|+++++++++++++++++++++++++++++++++Right aligned 2|
|                                Right aligned 3 |
\************************************************/