#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL line_count AS LONG
    LOCAL i AS LONG
    LOCAL page_size AS LONG
    LOCAL current_line AS LONG
    LOCAL start_line AS LONG
    LOCAL end_line AS LONG
    LOCAL total_pages AS LONG
    LOCAL search_term AS STRING
    LOCAL search_result AS LONG
    LOCAL found_count AS LONG
    LOCAL found_lines(100) AS LONG
    LOCAL found_arr_idx(100) AS LONG
    LOCAL fi AS LONG
    LOCAL temp_str AS STRING
    LOCAL page AS LONG
    LOCAL pages_to_show AS LONG

    line_count = 200

    page_size = 20
    total_pages = (line_count + page_size - 1) / page_size
    pages_to_show = 2
    page = 0

    WHILE page < pages_to_show
        current_line = page * page_size

        temp_str = STR$(page + 1)
        PRINT "=== PAGER (page " + temp_str + "/" + STR$(total_pages) + ") ==="
        PRINT ""

        start_line = current_line
        end_line = start_line + page_size - 1
        IF end_line >= line_count THEN
            end_line = line_count - 1
        END IF

        i = start_line
        WHILE i <= end_line
            temp_str = STR$(i + 1)
            IF i < 9 THEN
                temp_str = " " + temp_str
            END IF
            IF i < 99 THEN
                temp_str = " " + temp_str
            END IF
            PRINT temp_str + ": Line " + STR$(i + 1) + " of sample data"
            i = i + 1
        WEND

        PRINT ""
        page = page + 1
    WEND

    PRINT "--- N=next P=prev G=top E=end Q=quit ---"
    PRINT ""

    PRINT ""
    PRINT "Search Demonstration:"
    PRINT "====================="
    search_term = "ERROR"

    found_count = 0
    i = 0
    WHILE i < line_count
        temp_str = "This is line " + STR$(i + 1) + " of sample data"
        IF i = 50 THEN temp_str = "ERROR: simulated error on line 51"
        IF i = 100 THEN temp_str = "ERROR: simulated error on line 101"
        IF i = 150 THEN temp_str = "ERROR: simulated error on line 151"
        search_result = INSTR(1, temp_str, search_term)
        IF search_result > 0 THEN
            IF found_count < 100 THEN
                found_lines(found_count) = i
                found_arr_idx(found_count) = i
                found_count = found_count + 1
            END IF
        END IF
        i = i + 1
    WEND

    temp_str = STR$(found_count)
    PRINT "Searching for: " + search_term
    PRINT "Found " + temp_str + " matches:"
    fi = 0
    WHILE fi < found_count
        temp_str = STR$(found_lines(fi) + 1)
        IF found_lines(fi) < 9 THEN
            temp_str = " " + temp_str
        END IF
        IF found_lines(fi) < 99 THEN
            temp_str = " " + temp_str
        END IF
        PRINT "  " + temp_str + ": Line " + STR$(found_lines(fi) + 1) + " (ERROR)"
        fi = fi + 1
    WEND

    PRINT ""
    temp_str = STR$(line_count)
    PRINT "Pager demo complete. Total lines: " + temp_str

    FUNCTION = 0
END FUNCTION
