Big update
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
/*
|
||||
@todo: txt files
|
||||
@todo: read recursive
|
||||
@todo: add option for user to control what files are read
|
||||
@todo: pdf files
|
||||
@todo: plumbing for custom commands - pdf: {tools_path}/vlc {filename} --start-at {hour}:{second}:{minute}
|
||||
@todo: config with preload_files, initial_query, style
|
||||
@todo: help menu and config menu
|
||||
|
||||
@@ -21,70 +25,76 @@
|
||||
#include <SDL.h>
|
||||
#include "glad.h"
|
||||
|
||||
#include <pdfio.h>
|
||||
#include "read_pdf.cpp"
|
||||
#include "read_srt.cpp"
|
||||
|
||||
Arena Perm;
|
||||
WorkQueue MainWorkQueue;
|
||||
|
||||
#include "loading_thread.cpp"
|
||||
#include "searching_thread.cpp"
|
||||
|
||||
#include <pdfio.h>
|
||||
char SRTCommand[512] = "\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --start-time {time_in_seconds} {video}";
|
||||
char PDFCommand[512];
|
||||
char TXTCommand[512] = "subl.exe {file}";
|
||||
|
||||
#if 0
|
||||
extern "C" void OutputDebugStringA(const char *);
|
||||
void Printf(const char *string, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, string, result);
|
||||
OutputDebugStringA(result.data);
|
||||
}
|
||||
#else
|
||||
#define Printf(...) (0)
|
||||
#endif
|
||||
struct ReplaceVar {
|
||||
String replace;
|
||||
String with;
|
||||
};
|
||||
|
||||
void TestPdfio() {
|
||||
const char *filename = "C:/Users/Karol/Desktop/Hegels-Logic.pdf";
|
||||
pdfio_file_t *file = pdfioFileOpen(filename, NULL, NULL, NULL, NULL);
|
||||
Assert(file);
|
||||
defer { pdfioFileClose(file); };
|
||||
String ReplaceVars(Allocator allocator, Array<ReplaceVar> vars_to_replace, String string) {
|
||||
Array<char> sb = {allocator};
|
||||
for (int64_t i = 0; i < string.len; i += 1) {
|
||||
if (string[i] == '{') {
|
||||
|
||||
char buffer[1024];
|
||||
size_t page_count = pdfioFileGetNumPages(file);
|
||||
Printf("%s: %u pages\n", filename, (unsigned)page_count);
|
||||
for (size_t page_i = 0; page_i < page_count; page_i += 1) {
|
||||
pdfio_obj_t *obj = pdfioFileGetPage(file, page_i);
|
||||
if (obj == NULL) continue;
|
||||
// extract the variable
|
||||
i += 1;
|
||||
String var = {string.data + i, 0};
|
||||
for (; i < string.len && string[i] != '}'; i += 1)
|
||||
var.len += 1;
|
||||
|
||||
if (page_i > 110) __debugbreak();
|
||||
|
||||
size_t num_streams = pdfioPageGetNumStreams(obj);
|
||||
Printf("%s: page%u=%p, num_streams=%u\n", filename, (unsigned)page_i, obj, (unsigned)num_streams);
|
||||
for (size_t stream_i = 0; stream_i < num_streams; stream_i += 1) {
|
||||
pdfio_stream_t *st = pdfioPageOpenStream(obj, stream_i, true);
|
||||
if (st == NULL) continue;
|
||||
Printf("%s: page%u st%u=%p\n", filename, (unsigned)page_i, (unsigned)stream_i, st);
|
||||
defer { pdfioStreamClose(st); };
|
||||
|
||||
bool first = true;
|
||||
while (pdfioStreamGetToken(st, buffer, sizeof(buffer))) {
|
||||
if (buffer[0] == '(') {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
Printf(" ");
|
||||
}
|
||||
|
||||
Printf("%s", buffer + 1);
|
||||
} else if (!strcmp(buffer, "Td") || !strcmp(buffer, "TD") || !strcmp(buffer, "T*") || !strcmp(buffer, "\'") || !strcmp(buffer, "\"")) {
|
||||
Printf("\n");
|
||||
first = true;
|
||||
// find the replacement
|
||||
String found = "<not found>";
|
||||
For(vars_to_replace) {
|
||||
if (it.replace == var) {
|
||||
found = it.with;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
Printf("\n");
|
||||
}
|
||||
}
|
||||
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<ReplaceVar> 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)));
|
||||
}
|
||||
__debugbreak();
|
||||
}
|
||||
|
||||
void UISearchResults(Array<String> filenames) {
|
||||
@@ -123,22 +133,33 @@ void UISearchResults(Array<String> filenames) {
|
||||
String string = Format(scratch, "%.*s**%.*s**%.*s",
|
||||
FmtString(left), FmtString(middle), FmtString(right));
|
||||
|
||||
XToTimeString *item = XFindItem(middle);
|
||||
XToSource *item = XFindSource(middle);
|
||||
if (ImGui::Button(string.data)) {
|
||||
String base = ChopLastPeriod(item->filepath); // .srt
|
||||
base = ChopLastPeriod(base); // .en
|
||||
String exe_folder = GetExeDir(scratch);
|
||||
exe_folder = Format(scratch, "\"%.*s\"", FmtString(exe_folder));
|
||||
Array<ReplaceVar> replace_vars = {scratch};
|
||||
replace_vars.add({"exe_folder", GetExeDir(scratch)});
|
||||
|
||||
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);
|
||||
}
|
||||
if (item->kind == SourceKind_SRT) {
|
||||
String it = FindVideoForSRT(filenames, item->filepath);
|
||||
if (it.len) {
|
||||
int seconds = item->srt.hour * 60 * 60 + item->srt.minute * 60 + item->srt.second;
|
||||
String copy = Copy(scratch, it);
|
||||
for (int i = 0; i < copy.len; i += 1)
|
||||
if (copy.data[i] == '/') copy.data[i] = '\\';
|
||||
|
||||
replace_vars.add({"video", Format(scratch, "\"%.*s\"", FmtString(copy))});
|
||||
replace_vars.add({"time_in_seconds", Format(scratch, "%d", seconds)});
|
||||
String args = ReplaceVars(scratch, replace_vars, SRTCommand);
|
||||
Process process = RunEx(args);
|
||||
if (!process.is_valid) XFileLoadResults.bounded_add({"", process.error_message});
|
||||
}
|
||||
} else if (item->kind == SourceKind_TXT) {
|
||||
replace_vars.add({"file", Format(scratch, "\"%.*s\"", FmtString(item->filepath))});
|
||||
replace_vars.add({"line", Format(scratch, "%d", item->txt.row + 1)});
|
||||
String args = ReplaceVars(scratch, replace_vars, TXTCommand);
|
||||
Process process = RunEx(args);
|
||||
if (!process.is_valid) XFileLoadResults.bounded_add({"", process.error_message});
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
@@ -175,8 +196,21 @@ int main(int, char **) {
|
||||
InitScratch();
|
||||
InitArena(&Perm);
|
||||
|
||||
// TestPdfio();
|
||||
// return 0;
|
||||
// {
|
||||
// Scratch scratch;
|
||||
// for (FileIter it = IterateFiles(scratch, "D:/pdfs"); IsValid(it); Advance(&it)) {
|
||||
// if (it.is_directory || !EndsWith(it.absolute_path, ".pdf", true)) continue;
|
||||
// PDF pdf = pdfioReadPDF(scratch, it.absolute_path);
|
||||
// Array<String> strings = {scratch};
|
||||
// For(pdf.pages) {
|
||||
// strings.add(it.string);
|
||||
// }
|
||||
// String s = Merge(scratch, strings, "\n");
|
||||
// WriteFile(Format(scratch, "%.*s.txt", FmtString(it.absolute_path)), s);
|
||||
|
||||
// String pdf_content = ReadFile(scratch, it.absolute_path);
|
||||
// }
|
||||
// }
|
||||
|
||||
XInitLoading();
|
||||
InitSearch();
|
||||
@@ -223,8 +257,8 @@ int main(int, char **) {
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
(void)io;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
|
||||
// Setup Dear ImGui style
|
||||
// ImGui::StyleColorsDark();
|
||||
@@ -235,6 +269,7 @@ int main(int, char **) {
|
||||
ImGui_ImplOpenGL3_Init(glsl_version);
|
||||
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
|
||||
|
||||
bool command_menu_open = false;
|
||||
bool show_loaded_files = false;
|
||||
bool set_focus_to_input = true;
|
||||
int64_t frame = -1;
|
||||
@@ -250,6 +285,7 @@ int main(int, char **) {
|
||||
bool tab_press = false;
|
||||
bool enter_press = false;
|
||||
bool f1_press = false;
|
||||
bool f2_press = false;
|
||||
SDL_Event event;
|
||||
#if 1
|
||||
while (SDL_PollEvent(&event)) {
|
||||
@@ -268,6 +304,7 @@ int main(int, char **) {
|
||||
case SDLK_BACKSPACE:
|
||||
case SDLK_DELETE:
|
||||
set_focus_to_input = true;
|
||||
break;
|
||||
case SDLK_RETURN:
|
||||
enter_press = true;
|
||||
break;
|
||||
@@ -277,6 +314,9 @@ int main(int, char **) {
|
||||
case SDLK_F1:
|
||||
f1_press = true;
|
||||
break;
|
||||
case SDLK_F2:
|
||||
f2_press = true;
|
||||
break;
|
||||
}
|
||||
} else if (event.type == SDL_TEXTINPUT) {
|
||||
set_focus_to_input = true;
|
||||
@@ -304,17 +344,28 @@ int main(int, char **) {
|
||||
ImGui::Begin("input", NULL, window_flags);
|
||||
|
||||
if (show_loaded_files) {
|
||||
if (ImGui::Button("Show search results (F1)") || f1_press) {
|
||||
if (ImGui::Button("Show files (F1)") || f1_press) {
|
||||
show_loaded_files = !show_loaded_files;
|
||||
}
|
||||
} else {
|
||||
if (ImGui::Button("Show loaded files (F1)") || f1_press) {
|
||||
if (ImGui::Button("Hide files (F1)") || f1_press) {
|
||||
show_loaded_files = !show_loaded_files;
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
|
||||
if (command_menu_open) {
|
||||
if (ImGui::Button("Hide config (F2)") || f2_press) {
|
||||
command_menu_open = !command_menu_open;
|
||||
}
|
||||
} else {
|
||||
if (ImGui::Button("Show config (F2)") || f2_press) {
|
||||
command_menu_open = !command_menu_open;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (set_focus_to_input) ImGui::SetKeyboardFocusHere(0);
|
||||
if (!command_menu_open && 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();
|
||||
@@ -339,11 +390,19 @@ int main(int, char **) {
|
||||
|
||||
ImGui::Begin("result", NULL, window_flags);
|
||||
if (!ImGui::IsWindowFocused() && set_focus_to_list) ImGui::SetKeyboardFocusHere(0);
|
||||
if (show_loaded_files) {
|
||||
UILoadedFiles();
|
||||
|
||||
if (command_menu_open) {
|
||||
ImGui::InputText(".srt", SRTCommand, sizeof(SRTCommand));
|
||||
ImGui::InputText(".pdf", PDFCommand, sizeof(PDFCommand));
|
||||
ImGui::InputText(".txt", TXTCommand, sizeof(TXTCommand));
|
||||
} else {
|
||||
UISearchResults(filenames);
|
||||
if (show_loaded_files) {
|
||||
UILoadedFiles();
|
||||
} else {
|
||||
UISearchResults(filenames);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user