Print loaded files and errors, enable loading and searching

This commit is contained in:
Krzosa Karol
2024-07-06 09:51:39 +02:00
parent e3a176b2f9
commit 22828c9d23
7 changed files with 173 additions and 94 deletions

View File

@@ -3,8 +3,6 @@
#include "../basic/filesystem.h"
#include "../basic/thread_queue.h"
#include <thread>
#include <semaphore>
#include <mutex>
#include <stdio.h>
@@ -20,25 +18,98 @@ WorkQueue MainWorkQueue;
#include "loading_thread.cpp"
#include "searching_thread.cpp"
/*
void UISearchResults(Array<String> filenames) {
Scratch scratch;
Array<String> matches = LockSearchResults();
defer { UnlockSearchResults(); };
TODO:
- New threading model idea: I could just spin up a bunch of threads and then don't kill them. Just use them for searching! Each one would have it's own xarena and so on.
*/
float font_size = ImGui::GetFontSize();
ImFont *font = ImGui::GetFont();
const ImGuiViewport *main_viewport = ImGui::GetMainViewport();
//
// Searching thread
//
int64_t chars_per_line = (int64_t)(main_viewport->WorkSize.x / 2) / (int64_t)font->GetCharAdvance('_') - strlen(Prompt) * 2;
ImGuiListClipper clipper;
clipper.Begin((int)matches.len);
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
ImGui::PushID(i);
defer { ImGui::PopID(); };
auto &it = matches[i];
uintptr_t begin_region = (uintptr_t)XArena.data;
uintptr_t end_region = (uintptr_t)XArena.data + XArena.len;
uint64_t begin = (uintptr_t)(it.data - chars_per_line / 2);
uint64_t end = (uintptr_t)(it.data + it.len + chars_per_line / 2);
uint64_t a = Clamp(begin, begin_region, end_region);
uint64_t b = Clamp((uintptr_t)it.data, begin_region, end_region);
String left = {(char *)a, (int64_t)(b - a)};
uint64_t c = Clamp((uintptr_t)(it.data + it.len), begin_region, end_region);
uint64_t d = Clamp(end, begin_region, end_region);
String right = {(char *)c, (int64_t)(d - c)};
String middle = it;
String string = Format(scratch, "%.*s**%.*s**%.*s",
FmtString(left), FmtString(middle), FmtString(right));
XToTimeString *item = XFindItem(middle);
if (ImGui::Button(string.data)) {
String base = ChopLastPeriod(item->filepath); // .srt
base = ChopLastPeriod(base); // .en
For(filenames) {
if (StartsWith(it, base)) {
if (EndsWith(it, ".mkv") || EndsWith(it, ".webm") || EndsWith(it, ".mp4")) {
int seconds = item->hour * 60 * 60 + item->minute * 60 + item->second;
String copy = Copy(scratch, it);
for (int i = 0; i < copy.len; i += 1)
if (copy.data[i] == '/') copy.data[i] = '\\';
String args = Format(scratch, "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe --start-time %d \"%.*s\"", seconds, FmtString(copy));
RunEx(args);
}
}
}
}
ImGui::SameLine();
ImGui::Text(SkipToLastSlash(item->filepath).data);
}
}
}
void UILoadedFiles() {
Scratch scratch;
Array<FileLoadResult> file_load_results = XLockFileLoadResults();
defer { XUnlockFileResults(); };
ImGuiListClipper clipper;
clipper.Begin((int)file_load_results.len);
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
FileLoadResult file = file_load_results[file_load_results.len - 1 - i];
if (file.error.len) {
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)ImColor::HSV(7.0f, 0.6f, 0.6f));
String string = Format(scratch, "%.*s error: %.*s", FmtString(file.filename), FmtString(file.error));
ImGui::Text(string.data);
ImGui::PopStyleColor();
} else {
String string = Format(scratch, "%.*s loaded", FmtString(file.filename));
ImGui::Text(string.data);
}
}
}
}
int main(int, char **) {
InitOS();
InitScratch();
InitArena(&Perm);
InitArena(&XArena);
XArena.align = 0;
XInitLoading();
InitSearch();
ThreadStartupInfo infos[16] = {};
InitWorkQueue(&MainWorkQueue, 16, infos);
InitWorkQueue(&MainWorkQueue, Lengthof(infos), infos);
memcpy(Prompt, "read=D:/zizek", sizeof("read=D:/zizek"));
@@ -93,11 +164,9 @@ int main(int, char **) {
ImGui_ImplOpenGL3_Init(glsl_version);
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
// Our state
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
bool show_loaded_files = false;
bool set_focus_to_input = true;
int64_t frame = -1;
@@ -111,6 +180,7 @@ int main(int, char **) {
bool set_focus_to_list = false;
bool tab_press = false;
bool enter_press = false;
bool f1_press = false;
SDL_Event event;
#if 1
while (SDL_PollEvent(&event)) {
@@ -127,6 +197,8 @@ int main(int, char **) {
set_focus_to_input = true;
} else if (event.key.keysym.sym == SDLK_TAB) {
tab_press = true;
} else if (event.key.keysym.sym == SDLK_F1) {
f1_press = true;
}
} else if (event.type == SDL_KEYUP) {
@@ -151,11 +223,16 @@ int main(int, char **) {
ImGui::SetNextWindowSize(ImVec2(main_viewport->Size.x, 20), ImGuiCond_Always);
ImGui::Begin("input", NULL, window_flags);
if (ImGui::Button("Show loaded files") || f1_press) {
show_loaded_files = !show_loaded_files;
}
ImGui::SameLine();
if (set_focus_to_input) ImGui::SetKeyboardFocusHere(0);
if (tab_press && ImGui::IsWindowFocused()) set_focus_to_list = true;
if (ImGui::InputText("Input your query", Prompt, sizeof(Prompt))) {
StartSearchingForMatches();
}
ImGui::End();
if (enter_press) {
@@ -175,60 +252,10 @@ int main(int, char **) {
ImGui::Begin("result", NULL, window_flags);
if (!ImGui::IsWindowFocused() && set_focus_to_list) ImGui::SetKeyboardFocusHere(0);
Array<String> matches = LockSearchResults();
defer { UnlockSearchResults(); };
float font_size = ImGui::GetFontSize();
ImFont *font = ImGui::GetFont();
int64_t chars_per_line = (int64_t)(main_viewport->WorkSize.x / 2) / (int64_t)font->GetCharAdvance('_') - strlen(Prompt) * 2;
ImGuiListClipper clipper;
clipper.Begin((int)matches.len);
while (clipper.Step()) {
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
ImGui::PushID(i);
defer { ImGui::PopID(); };
auto &it = matches[i];
uintptr_t begin_region = (uintptr_t)XArena.data;
uintptr_t end_region = (uintptr_t)XArena.data + XArena.len;
uint64_t begin = (uintptr_t)(it.data - chars_per_line / 2);
uint64_t end = (uintptr_t)(it.data + it.len + chars_per_line / 2);
uint64_t a = Clamp(begin, begin_region, end_region);
uint64_t b = Clamp((uintptr_t)it.data, begin_region, end_region);
String left = {(char *)a, (int64_t)(b - a)};
uint64_t c = Clamp((uintptr_t)(it.data + it.len), begin_region, end_region);
uint64_t d = Clamp(end, begin_region, end_region);
String right = {(char *)c, (int64_t)(d - c)};
String middle = it;
String string = Format(frame_arena, "%.*s**%.*s**%.*s",
FmtString(left), FmtString(middle), FmtString(right));
XToTimeString *item = XFindItem(middle);
if (ImGui::Button(string.data)) {
String base = ChopLastPeriod(item->filepath); // .srt
base = ChopLastPeriod(base); // .en
For(filenames) {
if (StartsWith(it, base)) {
if (EndsWith(it, ".mkv") || EndsWith(it, ".webm") || EndsWith(it, ".mp4")) {
int seconds = item->hour * 60 * 60 + item->minute * 60 + item->second;
String copy = Copy(*frame_arena, it);
for (int i = 0; i < copy.len; i += 1)
if (copy.data[i] == '/') copy.data[i] = '\\';
String args = Format(*frame_arena, "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe --start-time %d \"%.*s\"", seconds, FmtString(copy));
RunEx(args);
}
}
}
}
ImGui::SameLine();
ImGui::Text(SkipToLastSlash(item->filepath).data);
}
if (show_loaded_files) {
UILoadedFiles();
} else {
UISearchResults(filenames);
}
ImGui::End();
}