PicoMite V6.00.02 release candidates - all versions


Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11026
Posted: 07:23am 19 Apr 2025      

  Quote  What scroll mechanism is in help? When there is more text than 1 screen? Same as with LIST?


I originally didn't do this because the list mechanism is very basic and splits lines in the middle of words and fails when the last line to be displayed is too long. In which case, the "press any key" happens after the first line has already scrolled off, or, even worse, on a display with no scroll, after the whole screen has been wiped.

To fix this I needed a mechanism to split the line only on word boundaries. Not too hard but the sort of thing that would take several iterations to get right.

Lets see what AI can do (Cortana).

My instruction
  Quote  write a routine in C that takes a  c string containing a line of text and a screen width in characters. Output the string using printf or similar but if the string length exceeds the screen width split the output onto 2  or more lines. However, the break should only appear on a space character which if it is replaced by crlf should be discarded.


followed by
  Quote  ensure you cater for the limit condition where the string has no spaces. In this case truncate the output at the screen width.


The AI wrote
#include <stdio.h>
#include <string.h>

void printWrappedText(const char *text, int screenWidth) {
   int length = strlen(text);
   int start = 0; // Start index of the current line

   while (start < length) {
       int end = start + screenWidth; // Calculate the end index for the current line
       if (end >= length) {
           // If end is beyond the text length, just print the remaining text
           printf("%s\n", text + start);
           break;
       }

       // Find the last space within the current screen width
       int lastSpace = -1;
       for (int i = start; i < end; i++) {
           if (text[i] == ' ') {
               lastSpace = i;
           }
       }

       if (lastSpace != -1) {
           // If a space is found, break at the space
           printf("%.*s\n", lastSpace - start, text + start);
           start = lastSpace + 1; // Skip the space
       } else {
           // If no space is found, truncate at screen width
           printf("%.*s\n", screenWidth, text + start);
           start += screenWidth;
       }
   }
}

int main() {
   const char *text = "Thisisaverylongstringwithnospacesthatneedstobetruncatedproperly.";
   int screenWidth = 20; // Example screen width

   printWrappedText(text, screenWidth);

   return 0;
}


I then tested this with different strings on https://www.programiz.com/c-programming/online-compiler/ which is a great resource for fast testing of C code
The program worked perfectly so with a bit of modification to fit into MMBasic this will be in the next release.