PicoMite V6.00.02 release candidates - all versions
| Author | Message | ||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11026 |
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 followed by 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. |
||||