From c660118f8be465860bc749c82121a99e372d721b Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 12 Jul 2024 12:47:29 +0200 Subject: [PATCH] Read on start and omit file type --- src/transcript_browser/config.cpp | 79 +++++++++++++++++++++-- src/transcript_browser/loading_thread.cpp | 52 ++++++++++++--- src/transcript_browser/main.cpp | 72 ++++----------------- 3 files changed, 127 insertions(+), 76 deletions(-) diff --git a/src/transcript_browser/config.cpp b/src/transcript_browser/config.cpp index 6a94be0..38a7166 100644 --- a/src/transcript_browser/config.cpp +++ b/src/transcript_browser/config.cpp @@ -8,9 +8,17 @@ struct ConfigParseResult { Array errors; }; -char SRTCommand[512] = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --start-time {time_in_seconds} {video}"; -char PDFCommand[512] = "C:/Users/Karol/AppData/Local/SumatraPDF/SumatraPDF.exe -page {page} {file}"; -char TXTCommand[512] = "notepad.exe {file}"; +struct ReplaceVar { + String replace; + String with; +}; +// @todo: move the config errors to config menu!!! + +char SRTCommand[512] = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --start-time {time_in_seconds} {video}"; +char PDFCommand[512] = "C:/Users/Karol/AppData/Local/SumatraPDF/SumatraPDF.exe -page {page} {file}"; +char TXTCommand[512] = "notepad.exe {file}"; +char ReadOnStart[512] = "-"; +bool ReadFoldersRecursively = false; void SkipWhitespace(String *s) { while (s->len && IsWhitespace(s->data[0])) { @@ -31,7 +39,7 @@ String ParseWord(String *s) { return result; } -bool ExpectString(String *s, String expect) { +bool MatchString(String *s, String expect) { SkipWhitespace(s); if (StartsWith(*s, expect)) { *s = s->skip(expect.len); @@ -57,7 +65,7 @@ ConfigParseResult ParseConfig(Allocator allocator, String string) { result.errors.add(Format(allocator, "failed to parse config at line: %d, the key is invalid", (int)lines.get_index(it))); continue; } - bool e = ExpectString(&s, "="); + bool e = MatchString(&s, "="); if (!e) { result.errors.add(Format(allocator, "failed to parse config at line: %d, expected '=' assignment sign", (int)lines.get_index(it))); continue; @@ -81,7 +89,7 @@ String GetValue(Array vars, String key) { } String SerializeConfig(Allocator allocator) { - String content = Format(allocator, "SRTCommand = %s\nPDFCommand = %s\nTXTCommand = %s\n", SRTCommand, PDFCommand, TXTCommand); + String content = Format(allocator, "SRTCommand = %s\nPDFCommand = %s\nTXTCommand = %s\nReadOnStart = %s", SRTCommand, PDFCommand, TXTCommand, ReadOnStart); return content; } @@ -123,6 +131,12 @@ void LoadConfig() { memcpy(SRTCommand, srt_command.data, len); SRTCommand[len] = 0; } + String read_on_start = GetValue(result.vars, "ReadOnStart"); + if (read_on_start.len) { + int len = ClampTop((int)read_on_start.len, (int)sizeof(ReadOnStart) - 1); + memcpy(ReadOnStart, read_on_start.data, len); + ReadOnStart[len] = 0; + } } void SaveConfig() { @@ -133,6 +147,59 @@ void SaveConfig() { WriteFile(config_path, content); } +String ReplaceVars(Allocator allocator, Array vars_to_replace, String string) { + Array sb = {allocator}; + for (int64_t i = 0; i < string.len; i += 1) { + if (string[i] == '{') { + + // extract the variable + i += 1; + String var = {string.data + i, 0}; + for (; i < string.len && string[i] != '}'; i += 1) + var.len += 1; + + // find the replacement + String found = ""; + For(vars_to_replace) { + if (it.replace == var) { + found = it.with; + break; + } + } + + For(found) sb.add(it); + + } else sb.add(string[i]); + } + + sb.add('\0'); + String result = {sb.data, sb.len - 1}; + return result; +} + +void TestReplaceVars() { + Scratch scratch; + Array vars_to_replace = {scratch}; + String exe_folder = GetExePath(scratch); + vars_to_replace.add({"exe_folder", exe_folder}); + String data_folder = Format(scratch, "%.*s/data", FmtString(exe_folder)); + vars_to_replace.add({"data_folder", data_folder}); + + { + String r = ReplaceVars(scratch, vars_to_replace, "{exe_folder}"); + Assert(r == exe_folder); + } + + { + String r = ReplaceVars(scratch, vars_to_replace, "{exe_folder}{data_folder}"); + Assert(r == Format(scratch, "%.*s%.*s", FmtString(exe_folder), FmtString(data_folder))); + } + { + String r = ReplaceVars(scratch, vars_to_replace, "..{exe_folder}..{data_folder}..{exe_folder}asd"); + Assert(r == Format(scratch, "..%.*s..%.*s..%.*sasd", FmtString(exe_folder), FmtString(data_folder), FmtString(exe_folder))); + } +} + void TestConfig() { Scratch scratch; { diff --git a/src/transcript_browser/loading_thread.cpp b/src/transcript_browser/loading_thread.cpp index 671358a..31ac0ae 100644 --- a/src/transcript_browser/loading_thread.cpp +++ b/src/transcript_browser/loading_thread.cpp @@ -111,21 +111,53 @@ Array XLockFileLoadResults() { void XUnlockFileResults() { } +void GetFilesRecursive(Array *arr, String folder) { + for (FileIter it = IterateFiles(Perm, folder); IsValid(it); Advance(&it)) { + if (it.is_directory) { + GetFilesRecursive(arr, it.absolute_path); + continue; + } + arr->add(Copy(arr->allocator, it.absolute_path)); + } +} + +Array GetFilesRecursive(Allocator allocator, String path) { + Array files = {allocator}; + GetFilesRecursive(&files, path); + return files; +} + void XAddFolder(String folder, Array *filenames) { Scratch scratch; - Array files_to_parse = {scratch}; - for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) { - String file = Copy(Perm, iter.absolute_path); - filenames->add(file); - if (EndsWith(iter.filename, ".srt", true)) { - files_to_parse.add(file); - } else if (EndsWith(iter.filename, ".txt", true) || EndsWith(iter.filename, ".html", true)) { - files_to_parse.add(file); - } else if (EndsWith(iter.filename, ".pdf", true)) { - files_to_parse.add(file); + Array files_here = {Perm}; + if (ReadFoldersRecursively) { + files_here = GetFilesRecursive(Perm, folder); + } else { + for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) { + if (iter.is_directory) continue; + String file = Copy(Perm, iter.absolute_path); + files_here.add(file); } } + Array files_to_parse = {scratch}; + For(files_here) { + if (EndsWith(it, ".srt", true)) { + String srt_command = SRTCommand; + if (srt_command == "-") continue; + files_to_parse.add(it); + } else if (EndsWith(it, ".txt", true) || EndsWith(it, ".html", true)) { + String txt_command = TXTCommand; + if (txt_command == "-") continue; + files_to_parse.add(it); + } else if (EndsWith(it, ".pdf", true)) { + String pdf_command = PDFCommand; + if (pdf_command == "-") continue; + files_to_parse.add(it); + } + filenames->add(it); + } + if (files_to_parse.len == 0) { XFileLoadResults.add({Copy(Perm, folder), "no files found"}); return; diff --git a/src/transcript_browser/main.cpp b/src/transcript_browser/main.cpp index 53412fc..8e7ef33 100644 --- a/src/transcript_browser/main.cpp +++ b/src/transcript_browser/main.cpp @@ -43,64 +43,6 @@ WorkQueue MainWorkQueue; #include "loading_thread.cpp" #include "searching_thread.cpp" -struct ReplaceVar { - String replace; - String with; -}; - -String ReplaceVars(Allocator allocator, Array vars_to_replace, String string) { - Array sb = {allocator}; - for (int64_t i = 0; i < string.len; i += 1) { - if (string[i] == '{') { - - // extract the variable - i += 1; - String var = {string.data + i, 0}; - for (; i < string.len && string[i] != '}'; i += 1) - var.len += 1; - - // find the replacement - String found = ""; - For(vars_to_replace) { - if (it.replace == var) { - found = it.with; - break; - } - } - - For(found) sb.add(it); - - } else sb.add(string[i]); - } - - sb.add('\0'); - String result = {sb.data, sb.len - 1}; - return result; -} - -void TestReplaceVars() { - Scratch scratch; - Array vars_to_replace = {scratch}; - String exe_folder = GetExePath(scratch); - vars_to_replace.add({"exe_folder", exe_folder}); - String data_folder = Format(scratch, "%.*s/data", FmtString(exe_folder)); - vars_to_replace.add({"data_folder", data_folder}); - - { - String r = ReplaceVars(scratch, vars_to_replace, "{exe_folder}"); - Assert(r == exe_folder); - } - - { - String r = ReplaceVars(scratch, vars_to_replace, "{exe_folder}{data_folder}"); - Assert(r == Format(scratch, "%.*s%.*s", FmtString(exe_folder), FmtString(data_folder))); - } - { - String r = ReplaceVars(scratch, vars_to_replace, "..{exe_folder}..{data_folder}..{exe_folder}asd"); - Assert(r == Format(scratch, "..%.*s..%.*s..%.*sasd", FmtString(exe_folder), FmtString(data_folder), FmtString(exe_folder))); - } -} - void UISearchResults(Array filenames) { Scratch scratch; Array matches = LockSearchResults(); @@ -226,11 +168,10 @@ int main(int, char **) { XInitLoading(); InitSearch(); + LoadConfig(); ThreadStartupInfo infos[16] = {}; InitWorkQueue(&MainWorkQueue, Lengthof(infos), infos); - memcpy(Prompt, "read=D:/zizek", sizeof("read=D:/zizek")); - Array filenames = {}; if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { @@ -281,6 +222,15 @@ int main(int, char **) { ImGui_ImplOpenGL3_Init(glsl_version); io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f); + { + String read_on_start = ReadOnStart; + if (read_on_start != "-") { + Scratch scratch; + Array strings = Split(scratch, read_on_start, ";"); + For(strings) XAddFolder(it, &filenames); + } + } + bool command_menu_open = false; bool show_loaded_files = false; bool set_focus_to_input = true; @@ -407,8 +357,10 @@ int main(int, char **) { ImGui::InputText(".srt", SRTCommand, sizeof(SRTCommand)); ImGui::InputText(".pdf", PDFCommand, sizeof(PDFCommand)); ImGui::InputText(".txt", TXTCommand, sizeof(TXTCommand)); + ImGui::InputText("Folders to read during startup", ReadOnStart, sizeof(ReadOnStart)); if (ImGui::Button("Save config")) { SaveConfig(); + LoadConfig(); } } else { if (show_loaded_files) {