Command_Append

This commit is contained in:
Krzosa Karol
2024-08-08 07:02:05 +02:00
parent bb44eab406
commit 161a9e3965
8 changed files with 37 additions and 35 deletions

View File

@@ -40,10 +40,11 @@ struct StdoutPollInfo {
}; };
struct Process { struct Process {
bool is_valid; bool is_valid;
String error_message; String error_message;
int exit_code; int exit_code;
char platform[6 * 8]; int64_t view_id; // text editor view
char platform[6 * 8];
}; };
Process CreateCommandLineProcess(String command_line, String working_dir); Process CreateCommandLineProcess(String command_line, String working_dir);

View File

@@ -456,20 +456,9 @@ View *FindView(BufferID buffer_id) {
return NULL; return NULL;
} }
void AppendToConsole(String16 string) { void Command_Append(ViewID view_id, String16 string) {
Buffer *buffer = GetBuffer(ConsoleBufferID); View *view = GetView(view_id);
Buffer *buffer = GetBuffer(view->active_buffer);
// @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> caret_copy = Copy(GetSystemAllocator(), view->carets);
// defer {
// Dealloc(&view->carets);
// view->carets = caret_copy;
// };
bool scroll_to_end = false; bool scroll_to_end = false;
if (view) { if (view) {
@@ -477,6 +466,15 @@ void AppendToConsole(String16 string) {
if (line == buffer->line_starts.len - 1) scroll_to_end = true; if (line == buffer->line_starts.len - 1) scroll_to_end = true;
} }
Array<Caret> 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_SelectRangeOneCursor(view, GetEndAsRange(*buffer));
Command_Replace(view, string); Command_Replace(view, string);
Command_Replace(view, L"\n"); 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; Scratch scratch;
String16 string16 = ToString16(scratch, string); String16 string16 = ToString16(scratch, string);
AppendToConsole(string16); Command_Append(view_id, string16);
} }
void ReportErrorf(const char *fmt, ...) { void ReportErrorf(const char *fmt, ...) {
Scratch scratch; Scratch scratch;
STRING_FORMAT(scratch, fmt, string); STRING_FORMAT(scratch, fmt, string);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL);
AppendToConsole(string); Command_Append(ConsoleViewID, string);
} }
void ReportConsolef(const char *fmt, ...) { void ReportConsolef(const char *fmt, ...) {
Scratch scratch; Scratch scratch;
STRING_FORMAT(scratch, fmt, string); STRING_FORMAT(scratch, fmt, string);
AppendToConsole(string); Command_Append(ConsoleViewID, string);
} }
void ReportWarningf(const char *fmt, ...) { void ReportWarningf(const char *fmt, ...) {
Scratch scratch; Scratch scratch;
STRING_FORMAT(scratch, fmt, string); STRING_FORMAT(scratch, fmt, string);
String16 string16 = ToString16(scratch, string); String16 string16 = ToString16(scratch, string);
AppendToConsole(string16); Command_Append(ConsoleViewID, string16);
SetVisibility(ConsoleWindowID, true); SetVisibility(ConsoleWindowID, true);
SetActiveWindow(ConsoleWindowID); SetActiveWindow(ConsoleWindowID);
} }

View File

@@ -80,7 +80,7 @@ int LuaPrint(lua_State *L) {
Scratch scratch; Scratch scratch;
String string = luaL_checkstring(L, 1); String string = luaL_checkstring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
AppendToConsole(string); Command_Append(ConsoleViewID, string);
return 0; return 0;
} }

View File

@@ -16,7 +16,7 @@ WindowID PopupWindowID;
WindowID DebugWindowID; WindowID DebugWindowID;
WindowID ConsoleWindowID; WindowID ConsoleWindowID;
BufferID ConsoleBufferID; ViewID ConsoleViewID;
BufferID DebugBufferID; BufferID DebugBufferID;
BufferID SearchBufferID; BufferID SearchBufferID;

View File

@@ -1,14 +1,15 @@
Array<Process> ActiveProcesses = {}; Array<Process> ActiveProcesses = {};
void Exec(String cmd, String working_dir) { void Exec(ViewID view, String cmd, String working_dir) {
Process process = CreateCommandLineProcess(cmd, working_dir); Process process = CreateCommandLineProcess(cmd, working_dir);
process.view_id = view.id;
if (process.is_valid) Add(&ActiveProcesses, process); 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; Scratch scratch;
String cmd = ToString(scratch, cmd16); String cmd = ToString(scratch, cmd16);
Exec(cmd, working_dir); Exec(view, cmd, working_dir);
} }
void UpdateProcesses() { void UpdateProcesses() {
@@ -17,9 +18,10 @@ void UpdateProcesses() {
char *buffer = AllocArray(scratch, char, buffer_size); char *buffer = AllocArray(scratch, char, buffer_size);
IterRemove(ActiveProcesses) { IterRemove(ActiveProcesses) {
IterRemovePrepare(ActiveProcesses); IterRemovePrepare(ActiveProcesses);
StdoutPollInfo info = PollStdout(&it, buffer, buffer_size); StdoutPollInfo info = PollStdout(&it, buffer, buffer_size);
String string = {buffer, info.size_read}; String string = {buffer, info.size_read};
if (string.len) AppendToConsole(string); ViewID view_id = {it.view_id};
if (string.len) Command_Append(view_id, string);
bool exited = PollExitCode(&it); bool exited = PollExitCode(&it);
if (exited) remove_item = true; if (exited) remove_item = true;

View File

@@ -124,7 +124,8 @@ void UpdateScroll(Window *window, bool update_caret_scrolling);
void Command_SelectEntireBuffer(View *view); void Command_SelectEntireBuffer(View *view);
void Command_Replace(View *view, String16 string); void Command_Replace(View *view, String16 string);
void Command_SelectRangeOneCursor(View *view, Range range); 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 ReportErrorf(const char *fmt, ...);
void ReportWarningf(const char *fmt, ...); void ReportWarningf(const char *fmt, ...);

View File

@@ -1,6 +1,6 @@
- I guess it's pretty dangerous passing pointers everywhere?
- Attach BufferID to process, append to that buffer - Attach BufferID to process, append to that buffer
- kill all processes on exit https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433 - 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 - 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 <hash> - Open git commit as part of "Open" in new buffer using git --no-pager show <hash>
- try using git grep for search for now, combine with fuzzy search buffer - try using git grep for search for now, combine with fuzzy search buffer

View File

@@ -153,11 +153,11 @@ void InitWindows() {
window->absolute_position = true; window->absolute_position = true;
window->dont_save_in_active_window_history = true; window->dont_save_in_active_window_history = true;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*")); Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*"));
ConsoleBufferID = buffer->id;
// buffer->no_history = true; // buffer->no_history = true;
View *view = CreateView(buffer->id); View *view = CreateView(buffer->id);
ConsoleViewID = view->id;
window->active_view = view->id; window->active_view = view->id;
CreateTitlebar(window_id); CreateTitlebar(window_id);