Using app data path, deleting config every time, improve loop

This commit is contained in:
Krzosa Karol
2024-07-27 22:07:58 +02:00
parent 052894a628
commit 07a41e0266
6 changed files with 90 additions and 116 deletions

View File

@@ -314,6 +314,15 @@ String GetWorkingDir(Allocator arena) {
} }
bool IsAbsolute(String path) { bool IsAbsolute(String path) {
bool result = path.len > 3 && IsAlphabetic(path.data[0]) && path.data[1] == ':' && path.data[2] == '/'; bool result = path.len > 3 && IsAlphabetic(path.data[0]) && path.data[1] == ':' && (path.data[2] == '/' || path.data[2] == '\\');
return result;
}
bool DeleteFile(String path) {
wchar_t wpath[1024];
CreateWidecharFromChar(wpath, Lengthof(wpath), path.data, path.len);
BOOL success = DeleteFileW(wpath);
bool result = true;
if (success == 0) result = false;
return result; return result;
} }

View File

@@ -222,7 +222,38 @@ void MergeCarets(View *view, Range *mouse_selection_anchor) {
Swap(&view->carets[first_caret_index], &view->carets[0]); Swap(&view->carets[first_caret_index], &view->carets[0]);
} }
void ReplaceDebugbarData() { String DebugViewList() {
Scratch scratch;
Array<String> strings = {scratch};
For(Views) {
Buffer *buffer = GetBuffer(it.active_buffer);
String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name));
Add(&strings, string);
}
String result = Merge(scratch, strings, "\n");
return result;
}
String DebugWindowList() {
Scratch scratch;
Array<String> strings = {scratch};
For(Windows) {
View *view = GetActiveView(&it);
Buffer *buffer = GetBuffer(view->active_buffer);
String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name));
Add(&strings, string);
ForItem(child_view_id, it.views) {
View *child_view = GetView(child_view_id);
Buffer *child_buffer = GetBuffer(child_view->active_buffer);
String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name));
Add(&strings, child_string);
}
}
String result = Merge(scratch, strings, "\n");
return result;
}
void ReplaceDebugData() {
Buffer *buffer = GetBuffer("*debug*"); Buffer *buffer = GetBuffer("*debug*");
Window *window = GetActiveWindow(); Window *window = GetActiveWindow();
@@ -234,9 +265,15 @@ void ReplaceDebugbarData() {
Buffer *last_buffer = GetBuffer(last_view->active_buffer); Buffer *last_buffer = GetBuffer(last_view->active_buffer);
Scratch scratch; Scratch scratch;
String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld", (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID); String s = Format(scratch, "wid: %d\nvid: %d\nbid: %d\nframe: %lld\n", (int)last_window->id.id, (int)last_view->id.id, (int)last_buffer->id.id, (long long)FrameID);
String16 string = ToString16(scratch, s); String16 string = ToString16(scratch, s);
ReplaceText(buffer, GetRange(*buffer), string); ReplaceText(buffer, GetRange(*buffer), string);
// String view_list = DebugViewList();
// Append(buffer, ToString16(scratch, view_list));
// String window_list = DebugWindowList();
// Append(buffer, ToString16(scratch, window_list));
} }
void ReplaceInfobarData() { void ReplaceInfobarData() {

View File

@@ -52,39 +52,6 @@ int LuaListOpenBuffers(lua_State *L) {
return 1; return 1;
} }
int LuaListViews(lua_State *L) {
Scratch scratch;
Array<String> strings = {scratch};
For(Views) {
Buffer *buffer = GetBuffer(it.active_buffer);
String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name));
Add(&strings, string);
}
String result = Merge(scratch, strings, "\n");
lua_pushlstring(LuaState, result.data, result.len);
return 1;
}
int LuaListWindows(lua_State *L) {
Scratch scratch;
Array<String> strings = {scratch};
For(Windows) {
View *view = GetActiveView(&it);
Buffer *buffer = GetBuffer(view->active_buffer);
String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name));
Add(&strings, string);
ForItem(child_view_id, it.views) {
View *child_view = GetView(child_view_id);
Buffer *child_buffer = GetBuffer(child_view->active_buffer);
String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name));
Add(&strings, child_string);
}
}
String result = Merge(scratch, strings, "\n");
lua_pushlstring(LuaState, result.data, result.len);
return 1;
}
int LuaOpenBigBuffer(lua_State *L) { int LuaOpenBigBuffer(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow()); Window *window = GetWindow(GetLastActiveWindow());
@@ -110,12 +77,6 @@ void InitLua() {
lua_pushcfunction(LuaState, LuaListOpenBuffers); lua_pushcfunction(LuaState, LuaListOpenBuffers);
lua_setglobal(LuaState, "list_buffers"); lua_setglobal(LuaState, "list_buffers");
lua_pushcfunction(LuaState, LuaListViews);
lua_setglobal(LuaState, "list_views");
lua_pushcfunction(LuaState, LuaListWindows);
lua_setglobal(LuaState, "list_windows");
lua_pushcfunction(LuaState, LuaOpenBigBuffer); lua_pushcfunction(LuaState, LuaOpenBigBuffer);
lua_setglobal(LuaState, "open_big_buffer"); lua_setglobal(LuaState, "open_big_buffer");
} }

View File

@@ -103,63 +103,6 @@ void ProcessSDLEvent(SDL_Event *input_event) {
HandleEvent(event); HandleEvent(event);
} }
uint64_t MapCharToNumber(char c) {
switch (c) {
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
case 'a':
case 'A': return 10; break;
case 'b':
case 'B': return 11; break;
case 'c':
case 'C': return 12; break;
case 'd':
case 'D': return 13; break;
case 'e':
case 'E': return 14; break;
case 'f':
case 'F': return 15; break;
default: return 255;
}
}
uint64_t ParseInt(char *string, uint64_t len, uint64_t base) {
Assert(base >= 2 && base <= 16);
uint64_t acc = 0;
for (uint64_t i = 0; i < len; i++) {
uint64_t num = MapCharToNumber(string[i]);
if (num >= base) {
// @todo: error
return 0;
}
acc *= base;
acc += num;
}
return acc;
}
Color ParseColor(String string) {
Color result = {0xFF, 0xFF, 0, 0xFF};
String hex = CutPrefix(&string, 1);
String red = CutPrefix(&string, 2);
result.r = (uint8_t)ParseInt(red.data, red.len, 16);
String green = CutPrefix(&string, 2);
result.g = (uint8_t)ParseInt(green.data, green.len, 16);
String blue = CutPrefix(&string, 2);
result.b = (uint8_t)ParseInt(blue.data, blue.len, 16);
String alpha = CutPrefix(&string, 2);
result.a = (uint8_t)ParseInt(alpha.data, alpha.len, 16);
return result;
}
#if _WIN32 #if _WIN32
int WinMain(void *hInstance, void *hPrevInstance, const char *lpCmdLine, int nShowCmd) int WinMain(void *hInstance, void *hPrevInstance, const char *lpCmdLine, int nShowCmd)
#else #else
@@ -172,6 +115,14 @@ int main()
InitArena(&Perm); InitArena(&Perm);
WorkingDir = GetWorkingDir(Perm); WorkingDir = GetWorkingDir(Perm);
ExeDir = GetExeDir(Perm); ExeDir = GetExeDir(Perm);
{
String sdl_config_path = SDL_GetPrefPath("krzosa", "text_editor");
if (sdl_config_path.len && sdl_config_path.data[sdl_config_path.len - 1] == '/') {
sdl_config_path = Chop(sdl_config_path, 1); // chop '/'
}
ConfigDir = NormalizePath(Perm, sdl_config_path);
SDL_free(sdl_config_path.data);
}
if (SDL_Init(SDL_INIT_VIDEO) == -1) { if (SDL_Init(SDL_INIT_VIDEO) == -1) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
@@ -227,24 +178,32 @@ int main()
Buffer *lua_buffer = NULL; Buffer *lua_buffer = NULL;
Int lua_buffer_change_id = 0; Int lua_buffer_change_id = 0;
{ {
// Init base config, test that it works and initialize the lua stuff
// Init base config if (!luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) {
if (luaL_dostring(LuaState, BaseLuaConfig.data) == LUA_OK) {
} else {
Assert(0); Assert(0);
} }
// Init user config // Init user config
Scratch scratch; Scratch scratch;
String lua_config_path = Format(scratch, "%.*s/init.lua", FmtString(ExeDir)); String lua_config_path = Format(scratch, "%.*s/init.lua", FmtString(ConfigDir));
#if DEBUG_BUILD
// WARNING! Delete config to make sure we are running this code more frequently
SDL_RemovePath(lua_config_path.data);
#endif
lua_buffer = BufferOpenFile(lua_config_path); lua_buffer = BufferOpenFile(lua_config_path);
if (lua_buffer->len) {
String16 string16 = GetString(*lua_buffer); String16 string16 = GetString(*lua_buffer);
String string = ToString(scratch, string16); String string = ToString(scratch, string16);
if (luaL_dostring(LuaState, string.data) == LUA_OK) { if (luaL_dostring(LuaState, string.data) == LUA_OK) {
ReloadColors(); ReloadColors();
} else { } else {
__debugbreak();
} }
} else {
ReplaceText(lua_buffer, {}, ToString16(scratch, BaseLuaConfig));
}
lua_buffer_change_id = lua_buffer->change_id; lua_buffer_change_id = lua_buffer->change_id;
} }
@@ -252,6 +211,14 @@ int main()
while (AppIsRunning) { while (AppIsRunning) {
FrameID += 1; FrameID += 1;
// We are waiting here because we don't want to miss the
// resize event which we don't handle but which we will use
// in the loop to figure out window size and window layout
SDL_Event event = {};
if (WaitForEvents) SDL_WaitEvent(&event);
WaitForEvents = true;
int window_x, window_y; int window_x, window_y;
SDL_GetWindowSize(window, &window_x, &window_y); SDL_GetWindowSize(window, &window_x, &window_y);
WindowSize = {(float)window_x, (float)window_y}; WindowSize = {(float)window_x, (float)window_y};
@@ -269,12 +236,9 @@ int main()
window->mouse_in_scrollbar = false; window->mouse_in_scrollbar = false;
} }
SDL_Event event; if (event.type != SDL_EVENT_FIRST) {
if (WaitForEvents) {
SDL_WaitEvent(&event);
ProcessSDLEvent(&event); ProcessSDLEvent(&event);
} }
WaitForEvents = true;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
ProcessSDLEvent(&event); ProcessSDLEvent(&event);
} }
@@ -290,7 +254,7 @@ int main()
} }
ReplaceInfobarData(); ReplaceInfobarData();
ReplaceDebugbarData(); ReplaceDebugData();
if (lua_buffer->dirty == false && lua_buffer->change_id != lua_buffer_change_id) { if (lua_buffer->dirty == false && lua_buffer->change_id != lua_buffer_change_id) {
Scratch scratch; Scratch scratch;
@@ -352,7 +316,7 @@ int main()
{ {
ProfileScope(clip_scroll); ProfileScope(clip_scroll);
Int last_line = LastLine(*buffer); Int last_line = LastLine(*buffer);
view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line)*FontLineSpacing)); view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing));
// @note: // @note:
// GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for // GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for

View File

@@ -91,6 +91,7 @@ struct Scroller {
Int FrameID; Int FrameID;
String WorkingDir; String WorkingDir;
String ConfigDir;
String ExeDir; String ExeDir;
Arena Perm; Arena Perm;

View File

@@ -9,8 +9,10 @@
- open "asd/asd/asd/asd" - open "asd/asd/asd/asd"
- console buffer with output and errors - console buffer with output and errors
- some popup for errors - some popup for errors
- good error reporting for user
- word completion - word completion
- Colored strings - Colored strings
- open project files in folder and only show open views in Ctrl+P
- font cache and on demand unicode loads - font cache and on demand unicode loads