diff --git a/src/basic/basic.h b/src/basic/basic.h index bfed189..9d67701 100644 --- a/src/basic/basic.h +++ b/src/basic/basic.h @@ -1106,6 +1106,18 @@ struct Scratch { Scratch(Scratch &arena, Scratch &a2); }; +struct RandomSeed { + uint64_t a; +}; + +inline uint64_t GetRandomU64(RandomSeed *state) { + uint64_t x = state->a; + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + return state->a = x; +} + // // Implementation // diff --git a/src/basic/filesystem.h b/src/basic/filesystem.h index b8c8f82..5aa6249 100644 --- a/src/basic/filesystem.h +++ b/src/basic/filesystem.h @@ -40,11 +40,13 @@ struct StdoutPollInfo { }; struct Process { - bool is_valid; - String error_message; - int exit_code; + bool is_valid; + String error_message; + int exit_code; + char platform[6 * 8]; + int64_t view_id; // text editor view - char platform[6 * 8]; + bool scroll_to_end; }; Process CreateCommandLineProcess(String command_line, String working_dir); diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index a121cb1..caadc69 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -28,6 +28,13 @@ String FieldString(lua_State *L, String name) { return result; } +RandomSeed UniqueBufferNameSeed = {13}; +String GetUniqueBufferName(Allocator allocator, String working_dir, String prepend_name) { + uint64_t number = GetRandomU64(&UniqueBufferNameSeed); + String buffer_name = Format(allocator, "%.*s/%.*s%llu", FmtString(working_dir), FmtString(prepend_name), number); + return buffer_name; +} + void Open(String path) { Scratch scratch; @@ -60,15 +67,14 @@ void Open(String path) { } else if (FieldString(LuaState, "kind") == "exec") { String cmd = FieldString(LuaState, "cmd"); String working_dir = FieldString(LuaState, "working_dir"); - String asd = "asd"; - String buffer_name = Format(scratch, "%.*s/%.*s", FmtString(working_dir), FmtString(asd)); + String buffer_name = GetUniqueBufferName(scratch, working_dir, "+CMD"); CheckpointBeforeGoto(); Window *window = GetWindow(GetLastActiveWindow()); View *view = WindowOpenBufferView(window, buffer_name); Buffer *buffer = GetBuffer(view->active_buffer); view->carets[0] = MakeCaret({}); - Exec(view->id, cmd, working_dir); + Exec(view->id, false, cmd, working_dir); SetActiveWindow(window->id); } else { ReportWarningf("Failed to match any of ApplyRules results!"); diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index edb420f..0518ece 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -173,21 +173,23 @@ View *FindViewWithBufferName(String name) { return NULL; } -String GetCurrentBufferName() { +Window *GetCurrentWindow() { Window *window = GetWindow(ActiveWindow); if (window->is_title_bar) { window = GetWindow(window->title_bar_window); } + return window; +} + +String GetCurrentBufferName() { + Window *window = GetCurrentWindow(); View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); return buffer->name; } String GetCurrentBufferDir() { - Window *window = GetWindow(ActiveWindow); - if (window->is_title_bar) { - window = GetWindow(window->title_bar_window); - } + Window *window = GetCurrentWindow(); View *view = GetView(window->active_view); Buffer *buffer = GetBuffer(view->active_buffer); String name = buffer->is_directory ? buffer->name : ChopLastSlash(buffer->name); diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index 723218a..51614da 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -1,15 +1,16 @@ Array ActiveProcesses = {}; -void Exec(ViewID view, String cmd, String working_dir) { - Process process = CreateCommandLineProcess(cmd, working_dir); - process.view_id = view.id; +void Exec(ViewID view, bool scroll_to_end, String cmd, String working_dir) { + Process process = CreateCommandLineProcess(cmd, working_dir); + process.view_id = view.id; + process.scroll_to_end = scroll_to_end; if (process.is_valid) Add(&ActiveProcesses, process); } -void Exec(ViewID view, String16 cmd16, String working_dir) { +void Exec(ViewID view, bool scroll_to_end, String16 cmd16, String working_dir) { Scratch scratch; String cmd = ToString(scratch, cmd16); - Exec(view, cmd, working_dir); + Exec(view, scroll_to_end, cmd, working_dir); } void UpdateProcesses() { @@ -21,7 +22,7 @@ void UpdateProcesses() { 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, false); + if (string.len) Command_Append(view_id, string, it.scroll_to_end); bool exited = PollExitCode(&it); if (exited) remove_item = true; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 5291297..69e8866 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -2,7 +2,6 @@ - kill process using command in window - kill all processes on exit https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433 - 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 - Test stdin writing code - Implement shell interaction (last line should have a '$'' symbols, if you press enter it should send that line to stdin of a running shell)