' Progress Spinner — demonstrates console spinner animation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL spinner AS STRING
    LOCAL bar AS STRING
    LOCAL percent AS LONG
    LOCAL filled AS LONG
    LOCAL empty AS LONG
    LOCAL barLen AS LONG
    LOCAL total AS LONG
    LOCAL phase AS STRING
    LOCAL indent AS LONG
    LOCAL s AS LONG
    LOCAL sp AS STRING
    LOCAL p AS LONG
    LOCAL tick AS LONG
    LOCAL labelText AS STRING
    labelText = "Processing"
    spinner = "|/-\|/-\"
    barLen = 40
    total = 100
    PRINT "============================================"
    PRINT "          PROGRESS SPINNER DEMO"
    PRINT "============================================"
    PRINT ""
    PRINT "Spinner animation"
    FOR i = 0 TO 19
        phase = MID$(spinner, (i MOD 8) + 1, 1)
        PRINT "  " + phase + " Working... " + STR$(i * 5) + "%"
    NEXT i
    PRINT ""
    PRINT "Progress bar with percentage"
    FOR i = 0 TO total
        percent = i
        filled = (percent * barLen) / 100
        empty = barLen - filled
        bar = "["
        FOR j = 1 TO filled
            bar = bar + "#"
        NEXT j
        FOR j = 1 TO empty
            bar = bar + "."
        NEXT j
        bar = bar + "]"
        IF percent < 10 THEN
            indent = 2
        ELSE
            IF percent < 100 THEN
                indent = 1
            ELSE
                indent = 0
            END IF
        END IF
        PRINT "  " + bar + " " + STR$(percent) + "%"
    NEXT i
    PRINT ""
    PRINT "Multiple spinners"
    s = 0
    FOR i = 0 TO 29
        sp = ""
        FOR j = 0 TO 3
            phase = MID$(spinner, ((i + j * 3) MOD 8) + 1, 1)
            sp = sp + phase
            sp = sp + " "
        NEXT j
        PRINT "  " + sp + " Tasks: " + STR$(i * 3) + "/90"
    NEXT i
    PRINT ""
    PRINT "Download simulation"
    FOR i = 0 TO 100
        tick = i
        phase = MID$(spinner, (i MOD 4) + 1, 1)
        p = (i * 30) / 100
        bar = ""
        FOR j = 1 TO p
            bar = bar + "="
        NEXT j
        bar = bar + ">"
        FOR j = p + 1 TO 30
            bar = bar + " "
        NEXT j
        IF i < 10 THEN
            indent = 2
        ELSE
            IF i < 100 THEN
                indent = 1
            ELSE
                indent = 0
            END IF
        END IF
        PRINT "  " + phase + " [" + bar + "] " + STR$(i) + "%"
    NEXT i
    PRINT ""
    PRINT "Animated status lines"
    FOR i = 0 TO 49
        phase = MID$(spinner, (i MOD 8) + 1, 1)
        IF i < 10 THEN
            PRINT "  " + phase + " Stage 1: Initializing..."
        ELSE
            IF i < 20 THEN
                PRINT "  " + phase + " Stage 2: Loading data..."
            ELSE
                IF i < 30 THEN
                    PRINT "  " + phase + " Stage 3: Processing..."
                ELSE
                    IF i < 40 THEN
                        PRINT "  " + phase + " Stage 4: Finalizing..."
                    ELSE
                        PRINT "  " + phase + " Stage 5: Complete!    "
                    END IF
                END IF
            END IF
        END IF
    NEXT i
    PRINT ""
    PRINT "Indeterminate progress"
    FOR i = 0 TO 39
        phase = MID$(spinner, (i MOD 4) + 1, 1)
        s = i MOD 20
        bar = ""
        FOR j = 0 TO 19
            IF j = s THEN
                bar = bar + "O"
            ELSE
                IF j = (s + 1) MOD 20 THEN
                    bar = bar + "o"
                ELSE
                    IF j = (s + 2) MOD 20 THEN
                        bar = bar + "."
                    ELSE
                        bar = bar + " "
                    END IF
                END IF
            END IF
        NEXT j
        PRINT "  " + phase + " [" + bar + "]"
    NEXT i
    PRINT ""
    PRINT "Dots animation"
    FOR i = 0 TO 29
        p = i MOD 4
        bar = ""
        FOR j = 0 TO 3
            IF j <= p THEN
                bar = bar + "."
            ELSE
                bar = bar + " "
            END IF
        NEXT j
        PRINT "  Loading" + bar
    NEXT i
    PRINT ""
    PRINT "Pulse bar"
    FOR i = 0 TO 39
        s = i MOD 20
        bar = ""
        FOR j = 0 TO 39
            k = j - s
            IF k < 0 THEN
                k = 0 - k
            END IF
            IF k < 3 THEN
                bar = bar + "#"
            ELSE
                IF k < 6 THEN
                    bar = bar + "="
                ELSE
                    bar = bar + "-"
                END IF
            END IF
        NEXT j
        PRINT "  [" + bar + "]"
    NEXT i
    PRINT ""
    PRINT "Complete progress"
    phase = MID$(spinner, 1, 1)
    bar = ""
    FOR j = 1 TO 40
        bar = bar + "#"
    NEXT j
    PRINT "  " + phase + " [" + bar + "] 100%"
    PRINT ""
    PRINT "Done"
    FUNCTION = 0
END FUNCTION
