diff --git a/src/basic/basic.h b/src/basic/basic.h index 25e9bcc..57e7b62 100644 --- a/src/basic/basic.h +++ b/src/basic/basic.h @@ -208,14 +208,14 @@ For(arr.reverse_iter()) { } */ #define IterRemove(a) for (int i = 0; i < (a).len; i += 1) -#define IterRemovePrepare(a) \ - auto &it = (a)[i]; \ - bool remove_item = false; \ - defer { \ - if (remove_item) { \ - (a).ordered_remove(it); \ - i -= 1; \ - } \ +#define IterRemovePrepare(a) \ + auto &it = (a)[i]; \ + bool remove_item = false; \ + defer { \ + if (remove_item) { \ + Remove(&(a), it); \ + i -= 1; \ + } \ } #define ForItem(it, array) for (auto &it : (array)) #define For(array) ForItem(it, array) diff --git a/src/basic/filesystem.h b/src/basic/filesystem.h index 2dadea1..bfc5989 100644 --- a/src/basic/filesystem.h +++ b/src/basic/filesystem.h @@ -46,7 +46,7 @@ struct Process { char platform[6 * 8]; }; -Process RunCmd(String command_line, String working_dir); +Process CreateCommandLineProcess(String command_line, String working_dir); bool WaitForExit(Process *process); bool PollExitCode(Process *process); void KillProcess(Process *process); diff --git a/src/basic/win32.cpp b/src/basic/win32.cpp index bc42166..b81bdb3 100644 --- a/src/basic/win32.cpp +++ b/src/basic/win32.cpp @@ -318,7 +318,7 @@ static void Win32ProcessError(Process *process, String cmd) { Win32CloseProcess(process); } -Process RunCmd(String command_line, String working_dir) { +Process CreateCommandLineProcess(String command_line, String working_dir) { Process process = {}; Win32Process *p = (Win32Process *)process.platform; diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index 7e2bd50..833c08d 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -465,11 +465,13 @@ void AppendToConsole(String16 string) { View *view = FindView(buffer->id); Assert(view); - Array caret_copy = Copy(GetSystemAllocator(), view->carets); - defer { - Dealloc(&view->carets); - view->carets = caret_copy; - }; + // @todo: this prevents scrolling to end. what do we do with this? I want to adjust the + // cursor etc. + // Array caret_copy = Copy(GetSystemAllocator(), view->carets); + // defer { + // Dealloc(&view->carets); + // view->carets = caret_copy; + // }; bool scroll_to_end = false; if (view) { diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index ace8dac..72d2e80 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -887,6 +887,8 @@ void WindowCommand(Event event, Window *window, View *view) { if (GetSize(caret.range) == 0) range = EncloseExecWord(buffer, GetFront(caret)); String16 string = GetString(*buffer, range); Command_EvalLua(view, string); + + // Exec(string, GetCurrentBufferDir()); } if (Ctrl(SDLK_W)) { diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp new file mode 100644 index 0000000..b528177 --- /dev/null +++ b/src/text_editor/process.cpp @@ -0,0 +1,27 @@ +Array ActiveProcesses = {}; + +void Exec(String cmd, String working_dir) { + Process process = CreateCommandLineProcess(cmd, working_dir); + if (process.is_valid) Add(&ActiveProcesses, process); +} + +void Exec(String16 cmd16, String working_dir) { + Scratch scratch; + String cmd = ToString(scratch, cmd16); + Exec(cmd, working_dir); +} + +void Process_OnUpdate() { + Scratch scratch; + int64_t buffer_size = 4096; + char *buffer = AllocArray(scratch, char, buffer_size); + IterRemove(ActiveProcesses) { + IterRemovePrepare(ActiveProcesses); + StdoutPollInfo info = PollStdout(&it, buffer, buffer_size); + String string = {buffer, info.size_read}; + if (string.len) AppendToConsole(string); + + bool exited = PollExitCode(&it); + if (exited) remove_item = true; + } +} diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index ffdac50..bce0eb1 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -33,6 +33,7 @@ int FullScreenPositionX, FullScreenPositionY; #include "management.cpp" #include "window.cpp" +#include "process.cpp" #include "commands.cpp" #include "commands_clipboard.cpp" #include "commands_window.cpp" @@ -164,6 +165,7 @@ void Update(Event event) { HandleEvent(event); + Process_OnUpdate(); ReloadLuaConfig(); ReplaceDebugData(); @@ -237,7 +239,7 @@ int main() SDL_free(sdl_config_path.data); } - // Process process = RunCmd("git log", WorkingDir); + // Process process = CreateCommandLineProcess("git log", WorkingDir); // Scratch scratch; // char *buffer = AllocArray(scratch, char, 4096); // for (int i = 0; i < 4; i += 1) { @@ -313,7 +315,7 @@ int main() } WaitForEvents = true; - if (DocumentSelected || ScrollbarSelected) { + if (DocumentSelected || ScrollbarSelected || ActiveProcesses.len) { WaitForEvents = false; } diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 2b68bd2..427a501 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -125,5 +125,6 @@ void Command_SelectEntireBuffer(View *view); void Command_Replace(View *view, String16 string); void Command_SelectRangeOneCursor(View *view, Range range); +void AppendToConsole(String string); void ReportErrorf(const char *fmt, ...); void ReportWarningf(const char *fmt, ...); \ No newline at end of file