Create process, poll and send output to console buffer

This commit is contained in:
Krzosa Karol
2024-08-07 07:51:50 +02:00
parent 7b8b8c751d
commit 327f352872
8 changed files with 51 additions and 17 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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;

View File

@@ -465,11 +465,13 @@ void AppendToConsole(String16 string) {
View *view = FindView(buffer->id);
Assert(view);
Array<Caret> 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> caret_copy = Copy(GetSystemAllocator(), view->carets);
// defer {
// Dealloc(&view->carets);
// view->carets = caret_copy;
// };
bool scroll_to_end = false;
if (view) {

View File

@@ -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)) {

View File

@@ -0,0 +1,27 @@
Array<Process> 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;
}
}

View File

@@ -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;
}

View File

@@ -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, ...);