diff postdog/main.c @ 63:fff1b048dda6

[Postdog] Fixed a problem where string did not wrap.
author June Park <parkjune1995@gmail.com>
date Tue, 23 Dec 2025 14:00:37 -0800
parents ccb42d5bf8fd
children 48f260576059
line wrap: on
line diff
--- a/postdog/main.c	Tue Dec 23 11:48:11 2025 -0800
+++ b/postdog/main.c	Tue Dec 23 14:00:37 2025 -0800
@@ -300,7 +300,7 @@
   return 0;
 }
 
-void updateUrlWithParams(char *url, size_t urlSize, const char *baseUrl, const char *params)
+void Postdog_UpdateUrlWithParams(char *url, size_t urlSize, const char *baseUrl, const char *params)
 {
   // Find if there's already a ? in the URL
   char *questionMark = strchr(baseUrl, '?');
@@ -338,7 +338,7 @@
 }
 
 // Save request to history file
-void saveRequestToHistory(const char *url, const char *method, const char *headers, const char *body)
+void Postdog_SaveRequestToHistory(const char *url, const char *method, const char *headers, const char *body)
 {
   PostDog_HistoryDirectory_Exists();
 
@@ -364,9 +364,12 @@
 
   // Write headers
   fprintf(f, "Headers:\n");
-  if (headers && strlen(headers) > 0) {
+  if (headers && strlen(headers) > 0)
+  {
     fprintf(f, "%s\n", headers);
-  } else {
+  }
+  else
+  {
     fprintf(f, "None\n");
   }
 
@@ -374,9 +377,12 @@
 
   // Write body
   fprintf(f, "Body:\n");
-  if (body && strlen(body) > 0) {
+  if (body && strlen(body) > 0)
+  {
     fprintf(f, "%s\n", body);
-  } else {
+  }
+  else
+  {
     fprintf(f, "None\n");
   }
 
@@ -384,23 +390,43 @@
   printf("Request saved to %s\n", filename);
 }
 
-void PostDog_Render_TextWithScroll(Rectangle textArea, Vector2 scroll, char * input) {
+void PostDog_Render_TextWithScroll(Rectangle textArea, Vector2 scroll, char *input)
+{
   BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height);
-
+  
   int yPos = textArea.y + 5 - (int)scroll.y;
-  char *textCopy = strdup(input);
-  char *line = strtok(textCopy, "\n");
-
-  while (line != NULL && yPos < textArea.y + textArea.height)
+  int charactersPerLine = (int)(textArea.width / (TEXT_SIZE/1.5)); // Account for padding
+  int totalPos = 0;
+  int inputLen = strlen(input);
+  
+  while (totalPos < inputLen)
   {
-    if (yPos + LINE_HEIGHT > textArea.y) {
-      DrawText(line, textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY);
+    int lineEnd = totalPos;
+    int lineLength = 0;
+    
+    while (lineEnd < inputLen && lineLength < charactersPerLine && input[lineEnd] != '\n')
+    {
+      lineEnd++;
+      lineLength++;
     }
+    
+    if (yPos + LINE_HEIGHT > textArea.y && yPos < textArea.y + textArea.height)
+    {
+      char savedChar = input[lineEnd];
+      input[lineEnd] = '\0';
+      DrawText(&input[totalPos], textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY);
+      input[lineEnd] = savedChar;
+    }
+    
     yPos += LINE_HEIGHT;
-    line = strtok(NULL, "\n");
+    
+    totalPos = lineEnd;
+    if (totalPos < inputLen && input[totalPos] == '\n')
+    {
+      totalPos++;
+    }
   }
-  free(textCopy);
-
+  
   EndScissorMode();
 }
 
@@ -646,7 +672,7 @@
         const char *requestHeaders = headersInput;
 
         // Save request to history
-        saveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody);
+        Postdog_SaveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody);
 
         // Reload history to show the new request
         historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS);
@@ -780,7 +806,7 @@
             char *questionMark = strchr(tempUrl, '?');
             if (questionMark) *questionMark = '\0';
 
-            updateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput);
+            Postdog_UpdateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput);
           }
           break;
         }
@@ -819,24 +845,7 @@
       DrawRectangleLinesEx(responseArea, 1, GRAY);
 
       // Draw response text with scroll
-      BeginScissorMode(responseArea.x, responseArea.y, responseArea.width, responseArea.height);
-
-      int yPos = responseArea.y + 5 - (int)responseScroll.y;
-      char *textCopy = strdup(responseText);
-      char *line = strtok(textCopy, "\n");
-
-      while (line != NULL && yPos < responseArea.y + responseArea.height)
-      {
-        if (yPos + LINE_HEIGHT > responseArea.y)
-        {
-          DrawText(line, responseArea.x + 5, yPos, TEXT_SIZE, DARKGRAY);
-        }
-        yPos += LINE_HEIGHT;
-        line = strtok(NULL, "\n");
-      }
-      free(textCopy);
-
-      EndScissorMode();
+      PostDog_Render_TextWithScroll(responseArea, responseScroll, responseText);
 
       if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C))
       {