diff --git a/demo.sh b/demo.sh new file mode 100755 index 0000000..3b8398c --- /dev/null +++ b/demo.sh @@ -0,0 +1,11 @@ +#!/bin/bash +shopt -s expand_aliases + +alias rt="./rtracker" + +rt Testing sleep... \ + % sleep 3 +rt Hello world? \ + %o echo Hello world +rt Producing some error \ + % rm not_exist diff --git a/rtracker b/rtracker new file mode 100755 index 0000000..002bcbd Binary files /dev/null and b/rtracker differ diff --git a/rtracker.c b/rtracker.c new file mode 100644 index 0000000..09cf928 --- /dev/null +++ b/rtracker.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include + +#include "c_cross_pack/libs/threads.h" + +char comment[1000] = ""; +char command[10000] = ""; + +// Out of command execution +char out[1000][100]; +int out_i = 0; + +bool runCommand(char* command) { + FILE *fp; + char path[1024]; + int status; + + // Combine stdout and stderr using 2>&1 + char fullCommand[1024]; + snprintf(fullCommand, sizeof(fullCommand), "%s 2>&1", command); + + /* Open the command for reading. */ + fp = popen(fullCommand, "r"); + if (fp == NULL) { + printf("Failed to run command\n"); + return false; + } + + /* Read the output a line at a time and store it in the out array */ + while (fgets(path, sizeof(path), fp) != NULL) { + strncpy(out[out_i], path, 100); + out[out_i][99] = '\0'; // Ensure null termination + out_i++; + if (out_i >= 1000) { + break; // Prevent overflow + } + } + + /* Close */ + status = pclose(fp); + + /* Check the return status of the command */ + if (WIFEXITED(status)) { + int exit_status = WEXITSTATUS(status); + if (exit_status != 0) { + return false; // Command failed + } + } else { + return false; // Command did not exit normally + } + + return true; +} + +void* loadAnim() { + char anim[] = {'-', '\\', '|', '/'}; + while (true) { + for (size_t i = 0; i < sizeof(anim)/sizeof(anim[0]); i++) { + printf("\r[\033[1;33m%c\033[1;0m] %s", anim[i], comment); + fflush(stdout); + usleep(100 * 1000); // 100 ms + } + } +} + +void getOut() { + for (int i = 0; i <= out_i; i++) { + printf("%s", out[i]); + } +} + +int main(int argc, char **argv) { + bool isCommand = false; + bool forceOut = false; + for(int i = 1; i < argc; i++) { + if (isCommand == false) { + if (strcmp(argv[i], "%o") == 0) { + isCommand = true; + forceOut = true; + } else if (strcmp(argv[i], "%") == 0) { + isCommand = true; + } else { + strcat(comment, argv[i]); + strcat(comment, " "); + } + } else { + strcat(command, argv[i]); + strcat(command, " "); + } + } + + void* thread_handle; + start_thread(&thread_handle, loadAnim); + if (runCommand(command)) { + kill_thread(thread_handle); + printf("\r[\033[1;32m🗸\033[1;0m] %s\n", comment); + if (forceOut == true) { + getOut(); + } + } else { + kill_thread(thread_handle); + printf("\r[\033[1;31m𐄂\033[1;0m] %s\n", comment); + // Get out of failed command + getOut(); + return 1; + } + + return 0; +} diff --git a/test b/test new file mode 100644 index 0000000..e69de29