From 161a9e396535ba396c7177013e99a3e448cdf610 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Thu, 8 Aug 2024 07:02:05 +0200 Subject: [PATCH] Command_Append --- src/basic/filesystem.h | 9 +++++---- src/text_editor/commands.cpp | 36 ++++++++++++++++------------------ src/text_editor/lua_api.cpp | 2 +- src/text_editor/management.cpp | 2 +- src/text_editor/process.cpp | 14 +++++++------ src/text_editor/text_editor.h | 3 ++- src/text_editor/todo.txt | 2 +- src/text_editor/window.cpp | 4 ++-- 8 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/basic/filesystem.h b/src/basic/filesystem.h index bfc5989..b8c8f82 100644 --- a/src/basic/filesystem.h +++ b/src/basic/filesystem.h @@ -40,10 +40,11 @@ struct StdoutPollInfo { }; struct Process { - bool is_valid; - String error_message; - int exit_code; - char platform[6 * 8]; + bool is_valid; + String error_message; + int exit_code; + int64_t view_id; // text editor view + char platform[6 * 8]; }; Process CreateCommandLineProcess(String command_line, String working_dir); diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index fb9a60b..a594894 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -456,20 +456,9 @@ View *FindView(BufferID buffer_id) { return NULL; } -void AppendToConsole(String16 string) { - Buffer *buffer = GetBuffer(ConsoleBufferID); - - // @todo: ? - View *view = FindView(buffer->id); - Assert(view); - - // @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; - // }; +void Command_Append(ViewID view_id, String16 string) { + View *view = GetView(view_id); + Buffer *buffer = GetBuffer(view->active_buffer); bool scroll_to_end = false; if (view) { @@ -477,6 +466,15 @@ void AppendToConsole(String16 string) { if (line == buffer->line_starts.len - 1) scroll_to_end = true; } + Array caret_copy = {}; + if (!scroll_to_end) caret_copy = Copy(GetSystemAllocator(), view->carets); + defer { + if (!scroll_to_end) { + Dealloc(&view->carets); + view->carets = caret_copy; + } + }; + Command_SelectRangeOneCursor(view, GetEndAsRange(*buffer)); Command_Replace(view, string); Command_Replace(view, L"\n"); @@ -486,30 +484,30 @@ void AppendToConsole(String16 string) { } } -void AppendToConsole(String string) { +void Command_Append(ViewID view_id, String string) { Scratch scratch; String16 string16 = ToString16(scratch, string); - AppendToConsole(string16); + Command_Append(view_id, string16); } void ReportErrorf(const char *fmt, ...) { Scratch scratch; STRING_FORMAT(scratch, fmt, string); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL); - AppendToConsole(string); + Command_Append(ConsoleViewID, string); } void ReportConsolef(const char *fmt, ...) { Scratch scratch; STRING_FORMAT(scratch, fmt, string); - AppendToConsole(string); + Command_Append(ConsoleViewID, string); } void ReportWarningf(const char *fmt, ...) { Scratch scratch; STRING_FORMAT(scratch, fmt, string); String16 string16 = ToString16(scratch, string); - AppendToConsole(string16); + Command_Append(ConsoleViewID, string16); SetVisibility(ConsoleWindowID, true); SetActiveWindow(ConsoleWindowID); } \ No newline at end of file diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 625f3a0..dca94a4 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -80,7 +80,7 @@ int LuaPrint(lua_State *L) { Scratch scratch; String string = luaL_checkstring(L, 1); lua_pop(L, 1); - AppendToConsole(string); + Command_Append(ConsoleViewID, string); return 0; } diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index a423715..edb420f 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -16,7 +16,7 @@ WindowID PopupWindowID; WindowID DebugWindowID; WindowID ConsoleWindowID; -BufferID ConsoleBufferID; +ViewID ConsoleViewID; BufferID DebugBufferID; BufferID SearchBufferID; diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index 87d2ab7..bab11cc 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -1,14 +1,15 @@ Array ActiveProcesses = {}; -void Exec(String cmd, String working_dir) { +void Exec(ViewID view, String cmd, String working_dir) { Process process = CreateCommandLineProcess(cmd, working_dir); + process.view_id = view.id; if (process.is_valid) Add(&ActiveProcesses, process); } -void Exec(String16 cmd16, String working_dir) { +void Exec(ViewID view, String16 cmd16, String working_dir) { Scratch scratch; String cmd = ToString(scratch, cmd16); - Exec(cmd, working_dir); + Exec(view, cmd, working_dir); } void UpdateProcesses() { @@ -17,9 +18,10 @@ void UpdateProcesses() { 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); + StdoutPollInfo info = PollStdout(&it, buffer, buffer_size); + String string = {buffer, info.size_read}; + ViewID view_id = {it.view_id}; + if (string.len) Command_Append(view_id, string); bool exited = PollExitCode(&it); if (exited) remove_item = true; diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index 427a501..d2d12ba 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -124,7 +124,8 @@ void UpdateScroll(Window *window, bool update_caret_scrolling); void Command_SelectEntireBuffer(View *view); void Command_Replace(View *view, String16 string); void Command_SelectRangeOneCursor(View *view, Range range); +void Command_Append(ViewID view_id, String16 string); +void Command_Append(ViewID view_id, String string); -void AppendToConsole(String string); void ReportErrorf(const char *fmt, ...); void ReportWarningf(const char *fmt, ...); \ No newline at end of file diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index e484b0a..8434625 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,6 +1,6 @@ +- I guess it's pretty dangerous passing pointers everywhere? - Attach BufferID to process, append to that buffer - kill all processes on exit https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433 -- AppendToConsole should scroll only if caret is at end and one caret otherwise the carets should not change - if exec is prepended with '!' symbol then run a shell command - Open git commit as part of "Open" in new buffer using git --no-pager show - try using git grep for search for now, combine with fuzzy search buffer diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index 40dd5a9..8c9f950 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -153,11 +153,11 @@ void InitWindows() { window->absolute_position = true; window->dont_save_in_active_window_history = true; - Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*")); - ConsoleBufferID = buffer->id; + Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*")); // buffer->no_history = true; View *view = CreateView(buffer->id); + ConsoleViewID = view->id; window->active_view = view->id; CreateTitlebar(window_id);