Better code readability
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 29 Jun 2025 16:18:21 +0000 (19:18 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 29 Jun 2025 16:18:21 +0000 (19:18 +0300)
Miscellaneous/wsystem.bas
Miscellaneous/wsystem.webm [new file with mode: 0644]

index c5c9332..625b09e 100755 (executable)
 \r
 ' Changelog:\r
 ' 2003, Initial version\r
-' 2024, Improved program readability using AI\r
+' 2024-2025, Improved program readability\r
 \r
 DECLARE SUB demo ()\r
-DECLARE FUNCTION getline$ (w%, l%)\r
-DECLARE SUB loadfile (file$, d%)\r
-DECLARE SUB sendline (w%, l%, newstring$)\r
-DECLARE FUNCTION getflin% ()\r
-DECLARE SUB refresh ()\r
-DECLARE FUNCTION addpage% (x%, y%, xs%, ys%, title$)\r
-DECLARE SUB box (x%, y%, xl%, yl%, e$)\r
+DECLARE FUNCTION GetLineFromWindow$ (windowNum%, lineNum%)\r
+DECLARE SUB LoadFileIntoWindow (fileName$, windowNum%)\r
+DECLARE SUB SendLineToWindow (windowNum%, lineNum%, newString$)\r
+DECLARE FUNCTION GetFreeLineIndex% ()\r
+DECLARE SUB RefreshAllWindows ()\r
+DECLARE FUNCTION AddWindow% (x%, y%, widt%, haigh%, title$)\r
+DECLARE SUB DrawBox (x%, y%, widt%, haigh%, edgeStyle$)\r
 DEFINT A-Z\r
-DECLARE SUB shpage (a)\r
-DECLARE SUB start ()\r
+DECLARE SUB ShowWindow (windowNum)\r
+DECLARE SUB InitializeSystem ()\r
 \r
-DIM SHARED stamo\r
-stamo = 5000\r
-DIM SHARED st$(1 TO stamo)\r
-DIM SHARED stpn\r
+' Global variables for text storage\r
+DIM SHARED maxStorage%\r
+maxStorage% = 5000\r
+DIM SHARED textStorage$(1 TO maxStorage%)\r
+DIM SHARED storagePointer%\r
 \r
-DIM SHARED pag(1 TO 10, 1 TO 1000)\r
-DIM SHARED pagx(1 TO 10), pagy(1 TO 10), pagxs(1 TO 10), pagys(1 TO 10)\r
-DIM SHARED pagon(1 TO 10)\r
-DIM SHARED pagtitle$(1 TO 10)\r
+' Window-related global arrays\r
+DIM SHARED windowData(1 TO 10, 1 TO 1000)      ' Stores line indices for each window\r
+DIM SHARED windowX(1 TO 10), windowY(1 TO 10)  ' Position of each window\r
+DIM SHARED windowWidth(1 TO 10), windowHeight(1 TO 10)  ' Size of each window\r
+DIM SHARED windowActiveStatus(1 TO 10)         ' Whether window is active\r
+DIM SHARED windowTitle$(1 TO 10)               ' Title of each window\r
 \r
-DIM SHARED pagshx(1 TO 10)      ' x & y shift\r
-DIM SHARED pagshy(1 TO 10)\r
+' Window scrolling/shifting\r
+DIM SHARED windowShiftX(1 TO 10)      ' Horizontal shift\r
+DIM SHARED windowShiftY(1 TO 10)      ' Vertical shift\r
 \r
-DIM SHARED pageactive            ' active page\r
+DIM SHARED currentActiveWindow%       ' Currently active window for display\r
 \r
-start\r
+InitializeSystem\r
 \r
 demo\r
 \r
-FUNCTION addpage% (x, y, xs, ys, title$)\r
-    FOR a = 1 TO 10\r
-        IF pagon(a) = 0 THEN\r
-            b = a\r
-            GOTO 1\r
-        END IF\r
-    NEXT a\r
-1\r
-\r
-    pagon(b) = 1\r
-    pagx(b) = x\r
-    pagy(b) = y\r
-    pagxs(b) = xs\r
-    pagys(b) = ys\r
-    pagtitle$(b) = title$\r
+FUNCTION AddWindow% (x%, y%, widt%, heigh%, title$)\r
+    ' Adds a new window to the system\r
 \r
-    addpage% = b\r
+    ' Find an empty window slot\r
+    FOR windowNum% = 1 TO 10\r
+        IF windowActiveStatus(windowNum%) = 0 THEN\r
+            foundWindow% = windowNum%\r
+            GOTO FoundEmptySlot\r
+        END IF\r
+    NEXT windowNum%\r
+\r
+FoundEmptySlot:\r
+    ' Initialize the window properties\r
+    windowActiveStatus(foundWindow%) = 1\r
+    windowX(foundWindow%) = x%\r
+    windowY(foundWindow%) = y%\r
+    windowWidth(foundWindow%) = widt%\r
+    windowHeight(foundWindow%) = heigh%\r
+    windowTitle$(foundWindow%) = title$\r
+\r
+    ' Return the window number\r
+    AddWindow% = foundWindow%\r
 END FUNCTION\r
 \r
-SUB clrwnd (w)\r
-    FOR a = 1 TO 1000\r
-        IF pag(w, a) > 0 THEN\r
-            st$(pag(w, a)) = ""\r
-            pag(w, a) = 0\r
+SUB ClearWindowContent (windowNum%)\r
+    ' Clears all content from a window\r
+\r
+    FOR lineNum% = 1 TO 1000\r
+        IF windowData(windowNum%, lineNum%) > 0 THEN\r
+            textStorage$(windowData(windowNum%, lineNum%)) = ""\r
+            windowData(windowNum%, lineNum%) = 0\r
         END IF\r
-    NEXT a\r
+    NEXT lineNum%\r
 END SUB\r
 \r
 SUB demo\r
     ' Create three windows with different sizes and titles\r
-    w1 = addpage%(1, 1, 30, 10, "window 1.")\r
-    w2 = addpage%(1, 12, 80, 30, "second window")\r
-    w3 = addpage%(31, 2, 30, 10, "last window")\r
+    window1% = AddWindow%(1, 1, 30, 10, "window 1.")\r
+    window2% = AddWindow%(1, 12, 80, 30, "second window")\r
+    window3% = AddWindow%(31, 2, 30, 10, "last window")\r
 \r
     ' Load the same file into all windows\r
-    loadfile "wsystem.bas", w2\r
-    loadfile "wsystem.bas", w1\r
-    loadfile "wsystem.bas", w3\r
+    LoadFileIntoWindow "wsystem.bas", window2%\r
+    LoadFileIntoWindow "wsystem.bas", window1%\r
+    LoadFileIntoWindow "wsystem.bas", window3%\r
 \r
-4\r
-    pageactive = INT(RND * 3) + 1\r
-    refresh\r
+AnimateWindows:\r
+    ' Randomly select an active window to animate\r
+    currentActiveWindow% = INT(RND * 3) + 1\r
+    RefreshAllWindows\r
 \r
     ' Animate the windows by shifting their content\r
-    FOR a = 1 TO 100\r
-        pagshx(pageactive) = SIN(a / 10) * 10 + 10\r
-        pagshy(pageactive) = a\r
-        shpage (pageactive)\r
+    FOR animationFrame% = 1 TO 100\r
+        windowShiftX(currentActiveWindow%) = SIN(animationFrame% / 10) * 10 + 10\r
+        windowShiftY(currentActiveWindow%) = animationFrame%\r
+        ShowWindow currentActiveWindow%\r
+        ' split-second delay\r
         SOUND 0, 1\r
         IF INKEY$ <> "" THEN SYSTEM\r
-    NEXT a\r
+    NEXT animationFrame%\r
 \r
-    GOTO 4\r
+    GOTO AnimateWindows\r
 END SUB\r
 \r
-FUNCTION getflin% ' Get free line\r
-2\r
-    IF stpn > 1000 THEN\r
-        stpn = 1\r
+FUNCTION GetFreeLineIndex%\r
+    ' Finds a free line in text storage\r
+\r
+FindNextLine:\r
+    IF storagePointer% > 1000 THEN\r
+        storagePointer% = 1\r
     END IF\r
-    IF st$(stpn) = "" THEN\r
-        getflin% = stpn\r
-        stpn = stpn + 1\r
+\r
+    IF textStorage$(storagePointer%) = "" THEN\r
+        GetFreeLineIndex% = storagePointer%\r
+        storagePointer% = storagePointer% + 1\r
     ELSE\r
-        stpn = stpn + 1\r
-        GOTO 2\r
+        storagePointer% = storagePointer% + 1\r
+        GOTO FindNextLine\r
     END IF\r
 END FUNCTION\r
 \r
-FUNCTION getline$ (w, l)\r
-    ' Retrieve the line from the window memory\r
-    IF pag(w, l) = 0 THEN\r
-        getline$ = ""\r
+FUNCTION GetLineFromWindow$ (windowNum%, lineNum%)\r
+    ' Retrieve a line from a window's memory\r
+\r
+    IF windowData(windowNum%, lineNum%) = 0 THEN\r
+        GetLineFromWindow$ = ""\r
     ELSE\r
-        getline$ = st$(pag(w, l))\r
+        GetLineFromWindow$ = textStorage$(windowData(windowNum%, lineNum%))\r
     END IF\r
 END FUNCTION\r
 \r
-SUB loadfile (file$, d)\r
-    ' Load a file into the window memory\r
+SUB LoadFileIntoWindow (fileName$, windowNum%)\r
+    ' Load a file into a window's memory\r
 \r
-    OPEN file$ FOR INPUT AS #1\r
-    FOR a = 1 TO 1000\r
+    OPEN fileName$ FOR INPUT AS #1\r
+    FOR lineNum% = 1 TO 1000\r
         IF EOF(1) <> 0 THEN\r
-            GOTO 3\r
+            GOTO FileLoaded\r
         END IF\r
-        LINE INPUT #1, a$\r
-        sendline d, a, a$\r
-    NEXT a\r
-3\r
+        LINE INPUT #1, lineContent$\r
+        SendLineToWindow windowNum%, lineNum%, lineContent$\r
+    NEXT lineNum%\r
 \r
+FileLoaded:\r
     CLOSE #1\r
 \r
     ' Fill the remaining lines with empty strings\r
-    FOR b = a TO 1000\r
-        sendline d, b, ""\r
-    NEXT b\r
+    FOR blankLineNum% = lineNum% TO 1000\r
+        SendLineToWindow windowNum%, blankLineNum%, ""\r
+    NEXT blankLineNum%\r
 END SUB\r
 \r
-SUB refresh\r
+SUB RefreshAllWindows\r
     ' Redraw all active windows\r
+\r
     CLS\r
-    FOR a = 1 TO 10\r
-        IF pagon(a) > 0 THEN\r
-            shpage (a)\r
+    FOR windowNum% = 1 TO 10\r
+        IF windowActiveStatus(windowNum%) > 0 THEN\r
+            ShowWindow windowNum%\r
         END IF\r
-    NEXT a\r
+    NEXT windowNum%\r
 END SUB\r
 \r
-SUB sendline (w, l, newstring$) ' window, lineNum, lineItself\r
-                                ' send string into window memory\r
-    a$ = newstring$\r
+SUB SendLineToWindow (windowNum%, lineNum%, newString$)\r
+    ' Stores a string in a window's memory\r
+\r
+    lineContent$ = newString$\r
 \r
     ' Remove trailing spaces from the string\r
-    IF a$ = SPACE$(LEN(a$)) THEN\r
-        a$ = ""\r
+    IF lineContent$ = SPACE$(LEN(lineContent$)) THEN\r
+        lineContent$ = ""\r
     END IF\r
 \r
-    IF LEN(a$) > 0 THEN\r
-5\r
-        IF RIGHT$(a$, 1) = " " THEN\r
-            a$ = LEFT$(a$, LEN(a$) - 1)\r
-            GOTO 5\r
+    IF LEN(lineContent$) > 0 THEN\r
+TrimRight:\r
+        IF RIGHT$(lineContent$, 1) = " " THEN\r
+            lineContent$ = LEFT$(lineContent$, LEN(lineContent$) - 1)\r
+            GOTO TrimRight\r
         END IF\r
     END IF\r
 \r
     ' Update the window memory with the new string\r
-    IF a$ = "" THEN\r
-        IF pag(w, l) > 0 THEN\r
-            st$(pag(w, l)) = "": pag(w, l) = 0\r
+    IF lineContent$ = "" THEN\r
+        IF windowData(windowNum%, lineNum%) > 0 THEN\r
+            textStorage$(windowData(windowNum%, lineNum%)) = "": windowData(windowNum%, lineNum%) = 0\r
         END IF\r
     ELSE\r
-        IF pag(w, l) = 0 THEN\r
-            pag(w, l) = getflin%\r
+        IF windowData(windowNum%, lineNum%) = 0 THEN\r
+            windowData(windowNum%, lineNum%) = GetFreeLineIndex%\r
         END IF\r
-        st$(pag(w, l)) = a$\r
+        textStorage$(windowData(windowNum%, lineNum%)) = lineContent$\r
     END IF\r
 END SUB\r
 \r
-SUB shpage (page)\r
-    ' Draw the specified window on the screen\r
+SUB ShowWindow (windowNum%)\r
+    ' Draws a window on the screen\r
 \r
-    ' Determine background color based on active page\r
-    IF page = pageactive THEN\r
-        bg = 1\r
+    ' Determine background color based on active window\r
+    IF windowNum% = currentActiveWindow% THEN\r
+        bgColor% = 1\r
     ELSE\r
-        bg = 0\r
+        bgColor% = 0\r
     END IF\r
 \r
-    x = pagx(page)\r
-    y = pagy(page)\r
-    xl = pagxs(page)\r
-    yl = pagys(page)\r
-    e$ = pagtitle$(page)\r
-\r
-    COLOR 11, bg\r
-\r
-    ' Draw the window border\r
-    a$ = ""\r
-    d$ = ""\r
-    FOR a = 1 TO xl - 2\r
-        a$ = a$ + CHR$(205)\r
-    NEXT a\r
-    b$ = CHR$(201) + a$ + CHR$(187)\r
-    c$ = CHR$(200) + a$ + CHR$(188)\r
-\r
-    LOCATE y, x\r
-    PRINT b$\r
-    LOCATE y + yl - 1, x\r
-    PRINT c$\r
-\r
-    ' Draw the window content\r
-    FOR a = 1 TO yl - 2\r
-        LOCATE y + a, x\r
-        d$ = getline$(page, a + pagshy(page))\r
-        d$ = d$ + SPACE$(300)\r
-        d$ = RIGHT$(d$, LEN(d$) - pagshx(page))\r
-        d$ = LEFT$(d$, xl - 2)\r
-        PRINT CHR$(186) + d$ + CHR$(186)\r
-    NEXT a\r
-\r
-    ' Draw the window title\r
-    xt = INT(x + (xl / 2) - (LEN(e$) / 2) - 2)\r
-    LOCATE y, xt\r
+    x% = windowX(windowNum%)\r
+    y% = windowY(windowNum%)\r
+    widt% = windowWidth(windowNum%)\r
+    heigh% = windowHeight(windowNum%)\r
+    title$ = windowTitle$(windowNum%)\r
+\r
+    COLOR 11, bgColor%\r
+\r
+    ' Create border components\r
+    FOR borderSegment% = 1 TO widt% - 2\r
+        topBottom$ = topBottom$ + CHR$(205)\r
+    NEXT borderSegment%\r
+    borderTop$ = CHR$(201) + topBottom$ + CHR$(187)\r
+    borderBottom$ = CHR$(200) + topBottom$ + CHR$(188)\r
+\r
+    ' Draw top border\r
+    LOCATE y%, x%\r
+    PRINT borderTop$\r
+\r
+    ' Draw bottom border\r
+    LOCATE y% + heigh% - 1, x%\r
+    PRINT borderBottom$\r
+\r
+    ' Draw window content\r
+    FOR lineNum% = 1 TO heigh% - 2\r
+        LOCATE y% + lineNum%, x%\r
+        lineContent$ = GetLineFromWindow$(windowNum%, lineNum% + windowShiftY(windowNum%))\r
+        lineContent$ = lineContent$ + SPACE$(300)\r
+        lineContent$ = RIGHT$(lineContent$, LEN(lineContent$) - windowShiftX(windowNum%))\r
+        lineContent$ = LEFT$(lineContent$, widt% - 2)\r
+        PRINT CHR$(186) + lineContent$ + CHR$(186)\r
+    NEXT lineNum%\r
+\r
+    ' Draw window title\r
+    titleX% = INT(x% + (widt% / 2) - (LEN(title$) / 2) - 2)\r
+    LOCATE y%, titleX%\r
     PRINT "[ "\r
-    xt = xt + 2\r
+    titleX% = titleX% + 2\r
 \r
     COLOR 10\r
-    LOCATE y, xt\r
-    PRINT e$\r
+    LOCATE y%, titleX%\r
+    PRINT title$\r
 \r
-    xt = xt + LEN(e$)\r
+    titleX% = titleX% + LEN(title$)\r
     COLOR 11\r
-    LOCATE y, xt\r
+    LOCATE y%, titleX%\r
     PRINT " ]"\r
     COLOR 7, 0\r
 END SUB\r
 \r
-SUB start\r
+SUB InitializeSystem\r
     ' Initialize the screen and shared memory\r
+\r
     WIDTH 80, 50\r
     VIEW PRINT 1 TO 50\r
 \r
-    FOR a = 1 TO stamo\r
-        st$(a) = ""\r
-    NEXT a\r
+    FOR storageIndex% = 1 TO maxStorage%\r
+        textStorage$(storageIndex%) = ""\r
+    NEXT storageIndex%\r
 \r
-    stpn = 1\r
+    storagePointer% = 1\r
 END SUB
\ No newline at end of file
diff --git a/Miscellaneous/wsystem.webm b/Miscellaneous/wsystem.webm
new file mode 100644 (file)
index 0000000..1a456bb
Binary files /dev/null and b/Miscellaneous/wsystem.webm differ