Opening new buffer which is git show commit command

This commit is contained in:
Krzosa Karol
2024-08-08 07:24:13 +02:00
parent 161a9e3965
commit 080669b5e9
8 changed files with 55 additions and 16 deletions

View File

@@ -312,11 +312,18 @@ function MatchAgainstIncludes(s)
return nil
end
function MatchGitCommit(s)
local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()}
end
Rules = {
GenericTextFileRule,
MatchAgainstIncludes,
MatchGitCommit,
}
function ApplyRules(s)
for i = #Rules,1,-1 do
rule = Rules[i]

View File

@@ -421,11 +421,24 @@ StdoutPollInfo PollStdout(Process *process, char *buffer, int64_t buffer_size) {
Assert(process->is_valid);
Win32Process *p = (Win32Process *)process->platform;
DWORD bytes_read = 0;
DWORD bytes_avail = 0;
bool peek_error = PeekNamedPipe(p->child_stdout_read, buffer, (DWORD)buffer_size, &bytes_read, &bytes_avail, NULL) == 0;
StdoutPollInfo result = {};
bool peek_error = PeekNamedPipe(p->child_stdout_read, NULL, 0, NULL, &bytes_avail, NULL) == 0;
if (peek_error) {
return result;
}
StdoutPollInfo result = {bytes_read, bytes_avail};
if (bytes_avail == 0) {
return result;
}
DWORD bytes_read = 0;
bool read_error = ReadFile(p->child_stdout_read, buffer, (DWORD)buffer_size, &bytes_read, 0) == 0;
if (read_error) {
return result;
}
result = {bytes_read, bytes_avail - bytes_read};
return result;
}

View File

@@ -456,12 +456,12 @@ View *FindView(BufferID buffer_id) {
return NULL;
}
void Command_Append(ViewID view_id, String16 string) {
void Command_Append(ViewID view_id, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
View *view = GetView(view_id);
Buffer *buffer = GetBuffer(view->active_buffer);
bool scroll_to_end = false;
if (view) {
if (scroll_to_end_if_cursor_on_last_line) {
Int line = PosToLine(*buffer, GetFront(view->carets[0]));
if (line == buffer->line_starts.len - 1) scroll_to_end = true;
}
@@ -484,30 +484,29 @@ void Command_Append(ViewID view_id, String16 string) {
}
}
void Command_Append(ViewID view_id, String string) {
void Command_Append(ViewID view_id, String string, bool scroll_to_end_if_cursor_on_last_line) {
Scratch scratch;
String16 string16 = ToString16(scratch, string);
Command_Append(view_id, string16);
Command_Append(view_id, string16, scroll_to_end_if_cursor_on_last_line);
}
void ReportErrorf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL);
Command_Append(ConsoleViewID, string);
Command_Append(ConsoleViewID, string, true);
}
void ReportConsolef(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Command_Append(ConsoleViewID, string);
Command_Append(ConsoleViewID, string, true);
}
void ReportWarningf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
String16 string16 = ToString16(scratch, string);
Command_Append(ConsoleViewID, string16);
Command_Append(ConsoleViewID, string, true);
SetVisibility(ConsoleWindowID, true);
SetActiveWindow(ConsoleWindowID);
}

View File

@@ -204,11 +204,18 @@ function MatchAgainstIncludes(s)
return nil
end
function MatchGitCommit(s)
local command = "git --no-pager show "..s
return {kind = "exec", cmd = command, working_dir = GetCurrentBufferDir()}
end
Rules = {
GenericTextFileRule,
MatchAgainstIncludes,
MatchGitCommit,
}
function ApplyRules(s)
for i = #Rules,1,-1 do
rule = Rules[i]

View File

@@ -57,6 +57,19 @@ void Open(String path) {
}
UpdateScroll(window, true);
SetActiveWindow(window->id);
} 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));
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);
SetActiveWindow(window->id);
} else {
ReportWarningf("Failed to match any of ApplyRules results!");
}
@@ -80,7 +93,7 @@ int LuaPrint(lua_State *L) {
Scratch scratch;
String string = luaL_checkstring(L, 1);
lua_pop(L, 1);
Command_Append(ConsoleViewID, string);
Command_Append(ConsoleViewID, string, true);
return 0;
}

View File

@@ -21,7 +21,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);
if (string.len) Command_Append(view_id, string, false);
bool exited = PollExitCode(&it);
if (exited) remove_item = true;

View File

@@ -124,8 +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 Command_Append(ViewID view_id, String16 string, bool scroll_to_end_if_cursor_on_last_line);
void Command_Append(ViewID view_id, String string, bool scroll_to_end_if_cursor_on_last_line);
void ReportErrorf(const char *fmt, ...);
void ReportWarningf(const char *fmt, ...);

View File

@@ -1,5 +1,5 @@
- I guess it's pretty dangerous passing pointers everywhere?
- Attach BufferID to process, append to that buffer
- 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 <hash>