#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL ansi_data AS STRING
    LOCAL i AS LONG
    LOCAL c AS LONG
    LOCAL escape_mode AS LONG
    LOCAL buf AS STRING
    LOCAL params AS STRING
    LOCAL param_str AS STRING
    LOCAL p AS LONG
    LOCAL pcount AS LONG
    LOCAL nums(10) AS LONG
    LOCAL j AS LONG
    LOCAL num_str AS STRING
    LOCAL num_val AS LONG
    LOCAL neg_flag AS LONG
    LOCAL digit_char AS LONG
    LOCAL row AS LONG
    LOCAL col AS LONG
    LOCAL total_esc AS LONG
    LOCAL temp_char AS STRING
    LOCAL temp_str AS STRING
    LOCAL temp_int AS LONG
    LOCAL sgr_buf AS STRING

    ansi_data = ""
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[2J"
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[H"
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[36m"
    ansi_data = ansi_data + "    Cyan text on default"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[31m"
    ansi_data = ansi_data + "  Red text here"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[32m"
    ansi_data = ansi_data + "  Green text here"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[33m"
    ansi_data = ansi_data + "  Yellow text"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[35m"
    ansi_data = ansi_data + "  Purple text"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[36m"
    ansi_data = ansi_data + "  Line with "
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[1m"
    ansi_data = ansi_data + "BOLD"
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[22m"
    ansi_data = ansi_data + " text"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[37m"
    ansi_data = ansi_data + "  Demo text"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)
    ansi_data = ansi_data + CHR$(27)
    ansi_data = ansi_data + "[0m"
    ansi_data = ansi_data + "  Plain reset text"
    ansi_data = ansi_data + CHR$(13)
    ansi_data = ansi_data + CHR$(10)

    i = 1
    escape_mode = 0
    buf = ""
    params = ""
    p = 1
    pcount = 0
    row = 1
    col = 1

    WHILE i <= LEN(ansi_data)
        temp_char = MID$(ansi_data, i, 1)
        c = ASC(temp_char)

        IF escape_mode = 0 THEN
            IF c = 27 THEN
                escape_mode = 1
                buf = ""
                params = ""
                pcount = 0
            ELSEIF c = 10 THEN
                row = row + 1
                col = 1
                buf = buf + CHR$(c)
            ELSE
                buf = buf + temp_char
                col = col + 1
            END IF
        ELSEIF escape_mode = 1 THEN
            IF c = 91 THEN
                escape_mode = 2
                params = ""
                pcount = 0
            ELSE
                escape_mode = 0
            END IF
        ELSEIF escape_mode = 2 THEN
            IF c >= 48 AND c <= 57 THEN
                params = params + temp_char
            ELSEIF c = 59 THEN
                num_str = params
                num_val = 0
                j = 1
                WHILE j <= LEN(num_str)
                    temp_char = MID$(num_str, j, 1)
                    digit_char = ASC(temp_char) - 48
                    num_val = num_val * 10 + digit_char
                    j = j + 1
                WEND
                nums(pcount) = num_val
                pcount = pcount + 1
                params = ""
            ELSE
                IF params <> "" OR pcount > 0 THEN
                    num_str = params
                    num_val = 0
                    j = 1
                    WHILE j <= LEN(num_str)
                        temp_char = MID$(num_str, j, 1)
                        digit_char = ASC(temp_char) - 48
                        num_val = num_val * 10 + digit_char
                        j = j + 1
                    WEND
                    nums(pcount) = num_val
                    pcount = pcount + 1
                END IF

                IF c = 109 THEN
                    sgr_buf = "<SGR"
                    j = 0
                    WHILE j < pcount
                        IF j > 0 THEN
                            sgr_buf = sgr_buf + ";"
                        END IF
                        sgr_buf = sgr_buf + STR$(nums(j))
                        j = j + 1
                    WEND
                    sgr_buf = sgr_buf + ">"
                    buf = buf + sgr_buf
                ELSEIF c = 74 THEN
                    buf = buf + "<CLS>"
                ELSEIF c = 72 THEN
                    IF pcount >= 2 THEN
                        row = nums(0)
                        col = nums(1)
                    ELSE
                        row = 1
                        col = 1
                    END IF
                END IF
                escape_mode = 0
                params = ""
                pcount = 0
            END IF
        END IF

        i = i + 1
    WEND

    PRINT "ANSI Art Viewer - Processed Output:"
    PRINT "===================================="
    PRINT ""
    PRINT buf
    PRINT ""
    temp_str = STR$(LEN(ansi_data))
    PRINT "Total input length: " + temp_str + " bytes"
    temp_str = STR$(LEN(buf))
    PRINT "Total output length: " + temp_str + " chars"

    total_esc = 0
    i = 1
    WHILE i <= LEN(ansi_data)
        temp_char = MID$(ansi_data, i, 1)
        IF ASC(temp_char) = 27 THEN
            total_esc = total_esc + 1
        END IF
        i = i + 1
    WEND
    temp_str = STR$(total_esc)
    PRINT "Total ESC characters: " + temp_str
    PRINT ""
    PRINT "ANSI Viewer complete."

    FUNCTION = 0
END FUNCTION
