Mercurial
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 62:ea9ef388ab97 | 63:fff1b048dda6 |
|---|---|
| 298 | 298 |
| 299 fclose(f); | 299 fclose(f); |
| 300 return 0; | 300 return 0; |
| 301 } | 301 } |
| 302 | 302 |
| 303 void updateUrlWithParams(char *url, size_t urlSize, const char *baseUrl, const char *params) | 303 void Postdog_UpdateUrlWithParams(char *url, size_t urlSize, const char *baseUrl, const char *params) |
| 304 { | 304 { |
| 305 // Find if there's already a ? in the URL | 305 // Find if there's already a ? in the URL |
| 306 char *questionMark = strchr(baseUrl, '?'); | 306 char *questionMark = strchr(baseUrl, '?'); |
| 307 | 307 |
| 308 if (questionMark != NULL) { | 308 if (questionMark != NULL) { |
| 336 free(paramsCopy); | 336 free(paramsCopy); |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 | 339 |
| 340 // Save request to history file | 340 // Save request to history file |
| 341 void saveRequestToHistory(const char *url, const char *method, const char *headers, const char *body) | 341 void Postdog_SaveRequestToHistory(const char *url, const char *method, const char *headers, const char *body) |
| 342 { | 342 { |
| 343 PostDog_HistoryDirectory_Exists(); | 343 PostDog_HistoryDirectory_Exists(); |
| 344 | 344 |
| 345 // Generate filename with timestamp | 345 // Generate filename with timestamp |
| 346 time_t now = time(NULL); | 346 time_t now = time(NULL); |
| 362 // Write method | 362 // Write method |
| 363 fprintf(f, "Method: %s\n", method); | 363 fprintf(f, "Method: %s\n", method); |
| 364 | 364 |
| 365 // Write headers | 365 // Write headers |
| 366 fprintf(f, "Headers:\n"); | 366 fprintf(f, "Headers:\n"); |
| 367 if (headers && strlen(headers) > 0) { | 367 if (headers && strlen(headers) > 0) |
| 368 { | |
| 368 fprintf(f, "%s\n", headers); | 369 fprintf(f, "%s\n", headers); |
| 369 } else { | 370 } |
| 371 else | |
| 372 { | |
| 370 fprintf(f, "None\n"); | 373 fprintf(f, "None\n"); |
| 371 } | 374 } |
| 372 | 375 |
| 373 fprintf(f, "---\n"); | 376 fprintf(f, "---\n"); |
| 374 | 377 |
| 375 // Write body | 378 // Write body |
| 376 fprintf(f, "Body:\n"); | 379 fprintf(f, "Body:\n"); |
| 377 if (body && strlen(body) > 0) { | 380 if (body && strlen(body) > 0) |
| 381 { | |
| 378 fprintf(f, "%s\n", body); | 382 fprintf(f, "%s\n", body); |
| 379 } else { | 383 } |
| 384 else | |
| 385 { | |
| 380 fprintf(f, "None\n"); | 386 fprintf(f, "None\n"); |
| 381 } | 387 } |
| 382 | 388 |
| 383 fclose(f); | 389 fclose(f); |
| 384 printf("Request saved to %s\n", filename); | 390 printf("Request saved to %s\n", filename); |
| 385 } | 391 } |
| 386 | 392 |
| 387 void PostDog_Render_TextWithScroll(Rectangle textArea, Vector2 scroll, char * input) { | 393 void PostDog_Render_TextWithScroll(Rectangle textArea, Vector2 scroll, char *input) |
| 394 { | |
| 388 BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height); | 395 BeginScissorMode(textArea.x, textArea.y, textArea.width, textArea.height); |
| 389 | 396 |
| 390 int yPos = textArea.y + 5 - (int)scroll.y; | 397 int yPos = textArea.y + 5 - (int)scroll.y; |
| 391 char *textCopy = strdup(input); | 398 int charactersPerLine = (int)(textArea.width / (TEXT_SIZE/1.5)); // Account for padding |
| 392 char *line = strtok(textCopy, "\n"); | 399 int totalPos = 0; |
| 393 | 400 int inputLen = strlen(input); |
| 394 while (line != NULL && yPos < textArea.y + textArea.height) | 401 |
| 395 { | 402 while (totalPos < inputLen) |
| 396 if (yPos + LINE_HEIGHT > textArea.y) { | 403 { |
| 397 DrawText(line, textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | 404 int lineEnd = totalPos; |
| 398 } | 405 int lineLength = 0; |
| 406 | |
| 407 while (lineEnd < inputLen && lineLength < charactersPerLine && input[lineEnd] != '\n') | |
| 408 { | |
| 409 lineEnd++; | |
| 410 lineLength++; | |
| 411 } | |
| 412 | |
| 413 if (yPos + LINE_HEIGHT > textArea.y && yPos < textArea.y + textArea.height) | |
| 414 { | |
| 415 char savedChar = input[lineEnd]; | |
| 416 input[lineEnd] = '\0'; | |
| 417 DrawText(&input[totalPos], textArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 418 input[lineEnd] = savedChar; | |
| 419 } | |
| 420 | |
| 399 yPos += LINE_HEIGHT; | 421 yPos += LINE_HEIGHT; |
| 400 line = strtok(NULL, "\n"); | 422 |
| 401 } | 423 totalPos = lineEnd; |
| 402 free(textCopy); | 424 if (totalPos < inputLen && input[totalPos] == '\n') |
| 403 | 425 { |
| 426 totalPos++; | |
| 427 } | |
| 428 } | |
| 429 | |
| 404 EndScissorMode(); | 430 EndScissorMode(); |
| 405 } | 431 } |
| 406 | 432 |
| 407 int main() | 433 int main() |
| 408 { | 434 { |
| 644 // Use JSON body for POST/PUT, otherwise use empty body | 670 // Use JSON body for POST/PUT, otherwise use empty body |
| 645 const char *requestBody = (methodActive == 1 || methodActive == 2) ? jsonInput : NULL; | 671 const char *requestBody = (methodActive == 1 || methodActive == 2) ? jsonInput : NULL; |
| 646 const char *requestHeaders = headersInput; | 672 const char *requestHeaders = headersInput; |
| 647 | 673 |
| 648 // Save request to history | 674 // Save request to history |
| 649 saveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody); | 675 Postdog_SaveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody); |
| 650 | 676 |
| 651 // Reload history to show the new request | 677 // Reload history to show the new request |
| 652 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); | 678 historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS); |
| 653 | 679 |
| 654 // Making the requests. | 680 // Making the requests. |
| 778 | 804 |
| 779 // Remove existing params if any | 805 // Remove existing params if any |
| 780 char *questionMark = strchr(tempUrl, '?'); | 806 char *questionMark = strchr(tempUrl, '?'); |
| 781 if (questionMark) *questionMark = '\0'; | 807 if (questionMark) *questionMark = '\0'; |
| 782 | 808 |
| 783 updateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput); | 809 Postdog_UpdateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput); |
| 784 } | 810 } |
| 785 break; | 811 break; |
| 786 } | 812 } |
| 787 } | 813 } |
| 788 | 814 |
| 817 | 843 |
| 818 // Draw border | 844 // Draw border |
| 819 DrawRectangleLinesEx(responseArea, 1, GRAY); | 845 DrawRectangleLinesEx(responseArea, 1, GRAY); |
| 820 | 846 |
| 821 // Draw response text with scroll | 847 // Draw response text with scroll |
| 822 BeginScissorMode(responseArea.x, responseArea.y, responseArea.width, responseArea.height); | 848 PostDog_Render_TextWithScroll(responseArea, responseScroll, responseText); |
| 823 | |
| 824 int yPos = responseArea.y + 5 - (int)responseScroll.y; | |
| 825 char *textCopy = strdup(responseText); | |
| 826 char *line = strtok(textCopy, "\n"); | |
| 827 | |
| 828 while (line != NULL && yPos < responseArea.y + responseArea.height) | |
| 829 { | |
| 830 if (yPos + LINE_HEIGHT > responseArea.y) | |
| 831 { | |
| 832 DrawText(line, responseArea.x + 5, yPos, TEXT_SIZE, DARKGRAY); | |
| 833 } | |
| 834 yPos += LINE_HEIGHT; | |
| 835 line = strtok(NULL, "\n"); | |
| 836 } | |
| 837 free(textCopy); | |
| 838 | |
| 839 EndScissorMode(); | |
| 840 | 849 |
| 841 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) | 850 if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C)) |
| 842 { | 851 { |
| 843 if (CheckCollisionPointRec(GetMousePosition(), responseArea)) { | 852 if (CheckCollisionPointRec(GetMousePosition(), responseArea)) { |
| 844 SetClipboardText(responseText); | 853 SetClipboardText(responseText); |