' String Word Wrap — wraps text to specified line width
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL inputText AS STRING
    LOCAL lineWidth AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL lineStart AS LONG
    LOCAL lastSpace AS LONG
    LOCAL ch AS STRING
    LOCAL currentLine AS STRING
    LOCAL totalLen AS LONG
    LOCAL indentLen AS LONG
    LOCAL indentStr AS STRING
    LOCAL spaceNeeded AS LONG
    LOCAL padPos AS LONG
    LOCAL outStr AS STRING
    PRINT "============================================"
    PRINT "          STRING WORD WRAP UTILITY"
    PRINT "============================================"
    PRINT ""
    inputText = "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs. How vexingly quick daft zebras jump. The five boxing wizards jump quickly."
    PRINT "Original text:"
    PRINT inputText
    PRINT ""
    lineWidth = 30
    PRINT "Wrapped at column " + STR$(lineWidth) + ":"
    PRINT ""
    totalLen = LEN(inputText)
    lineStart = 1
    DO WHILE lineStart <= totalLen
        IF totalLen - lineStart + 1 <= lineWidth THEN
            currentLine = MID$(inputText, lineStart, totalLen - lineStart + 1)
            PRINT currentLine
            EXIT DO
        END IF
        lastSpace = 0
        FOR i = lineStart TO lineStart + lineWidth - 1
            IF i <= totalLen THEN
                ch = MID$(inputText, i, 1)
                IF ch = " " THEN
                    lastSpace = i
                END IF
            END IF
        NEXT i
        IF lastSpace > 0 THEN
            currentLine = MID$(inputText, lineStart, lastSpace - lineStart)
            PRINT currentLine
            lineStart = lastSpace + 1
        ELSE
            currentLine = MID$(inputText, lineStart, lineWidth)
            PRINT currentLine
            lineStart = lineStart + lineWidth
        END IF
    LOOP
    PRINT ""
    lineWidth = 40
    PRINT "Wrapped at column " + STR$(lineWidth) + ":"
    PRINT ""
    totalLen = LEN(inputText)
    lineStart = 1
    DO WHILE lineStart <= totalLen
        IF totalLen - lineStart + 1 <= lineWidth THEN
            currentLine = MID$(inputText, lineStart, totalLen - lineStart + 1)
            PRINT currentLine
            EXIT DO
        END IF
        lastSpace = 0
        FOR i = lineStart TO lineStart + lineWidth - 1
            IF i <= totalLen THEN
                ch = MID$(inputText, i, 1)
                IF ch = " " THEN
                    lastSpace = i
                END IF
            END IF
        NEXT i
        IF lastSpace > 0 THEN
            currentLine = MID$(inputText, lineStart, lastSpace - lineStart)
            PRINT currentLine
            lineStart = lastSpace + 1
        ELSE
            currentLine = MID$(inputText, lineStart, lineWidth)
            PRINT currentLine
            lineStart = lineStart + lineWidth
        END IF
    LOOP
    PRINT ""
    PRINT "============================================"
    PRINT "       WORD WRAP WITH JUSTIFICATION"
    PRINT "============================================"
    PRINT ""
    inputText = "This is a short paragraph that demonstrates justified word wrapping where spaces are added between words to make each line the same width."
    lineWidth = 25
    PRINT "Justified at column " + STR$(lineWidth) + ":"
    PRINT ""
    totalLen = LEN(inputText)
    lineStart = 1
    DO WHILE lineStart <= totalLen
        IF totalLen - lineStart + 1 <= lineWidth THEN
            currentLine = MID$(inputText, lineStart, totalLen - lineStart + 1)
            PRINT currentLine
            EXIT DO
        END IF
        lastSpace = 0
        FOR i = lineStart TO lineStart + lineWidth - 1
            IF i <= totalLen THEN
                ch = MID$(inputText, i, 1)
                IF ch = " " THEN
                    lastSpace = i
                END IF
            END IF
        NEXT i
        IF lastSpace > 0 THEN
            currentLine = MID$(inputText, lineStart, lastSpace - lineStart)
            IF lastSpace < totalLen THEN
                spaceNeeded = lineWidth - LEN(currentLine)
                padPos = 1
                DO WHILE spaceNeeded > 0
                    IF padPos > LEN(currentLine) THEN
                        padPos = 1
                    END IF
                    IF MID$(currentLine, padPos, 1) = " " THEN
                        currentLine = MID$(currentLine, 1, padPos - 1) + " " + MID$(currentLine, padPos)
                        spaceNeeded = spaceNeeded - 1
                        padPos = padPos + 2
                    ELSE
                        padPos = padPos + 1
                    END IF
                LOOP
            END IF
            PRINT currentLine
            lineStart = lastSpace + 1
        ELSE
            currentLine = MID$(inputText, lineStart, lineWidth)
            PRINT currentLine
            lineStart = lineStart + lineWidth
        END IF
    LOOP
    PRINT ""
    PRINT "============================================"
    PRINT "       WORD WRAP WITH INDENT"
    PRINT "============================================"
    PRINT ""
    inputText = "This demonstrates word wrapping with a hanging indent applied to continuation lines for readability."
    lineWidth = 30
    indentStr = "    "
    indentLen = LEN(indentStr)
    PRINT "With hanging indent:"
    PRINT ""
    totalLen = LEN(inputText)
    lineStart = 1
    outStr = "first"
    DO WHILE lineStart <= totalLen
        IF outStr = "first" THEN
            IF totalLen - lineStart + 1 <= lineWidth THEN
                currentLine = MID$(inputText, lineStart, totalLen - lineStart + 1)
                PRINT currentLine
                EXIT DO
            END IF
            lastSpace = 0
            FOR i = lineStart TO lineStart + lineWidth - 1
                IF i <= totalLen THEN
                    ch = MID$(inputText, i, 1)
                    IF ch = " " THEN
                        lastSpace = i
                    END IF
                END IF
            NEXT i
            IF lastSpace > 0 THEN
                currentLine = MID$(inputText, lineStart, lastSpace - lineStart)
                PRINT currentLine
                lineStart = lastSpace + 1
            ELSE
                currentLine = MID$(inputText, lineStart, lineWidth)
                PRINT currentLine
                lineStart = lineStart + lineWidth
            END IF
            outStr = "cont"
        ELSE
            IF totalLen - lineStart + 1 <= lineWidth - indentLen THEN
                currentLine = indentStr + MID$(inputText, lineStart, totalLen - lineStart + 1)
                PRINT currentLine
                EXIT DO
            END IF
            lastSpace = 0
            FOR i = lineStart TO lineStart + lineWidth - indentLen - 1
                IF i <= totalLen THEN
                    ch = MID$(inputText, i, 1)
                    IF ch = " " THEN
                        lastSpace = i
                    END IF
                END IF
            NEXT i
            IF lastSpace > 0 THEN
                currentLine = indentStr + MID$(inputText, lineStart, lastSpace - lineStart)
                PRINT currentLine
                lineStart = lastSpace + 1
            ELSE
                currentLine = indentStr + MID$(inputText, lineStart, lineWidth - indentLen)
                PRINT currentLine
                lineStart = lineStart + lineWidth - indentLen
            END IF
        END IF
    LOOP
    PRINT ""
    PRINT "Word wrap operations complete"
    FUNCTION = 0
END FUNCTION
