Init repo
This commit is contained in:
377
src/transcript_browser/main.cpp
Normal file
377
src/transcript_browser/main.cpp
Normal file
@@ -0,0 +1,377 @@
|
||||
#define BASIC_IMPL
|
||||
#include "../pdf_browser/basic.h"
|
||||
#include "filesystem.h"
|
||||
#include "tests.h"
|
||||
#include "raylib.h"
|
||||
|
||||
#include <thread>
|
||||
#include <semaphore>
|
||||
#include <mutex>
|
||||
|
||||
struct XToTimeString {
|
||||
String string; // String inside transcript arena
|
||||
uint16_t hour;
|
||||
uint16_t minute;
|
||||
uint16_t second;
|
||||
String filepath;
|
||||
};
|
||||
|
||||
struct TimeString {
|
||||
uint16_t hour;
|
||||
uint16_t minute;
|
||||
uint16_t second;
|
||||
String string;
|
||||
};
|
||||
|
||||
struct TimeFile {
|
||||
Array<TimeString> time_strings;
|
||||
String file;
|
||||
};
|
||||
|
||||
bool AppInitializedWithFolder;
|
||||
Arena XArena;
|
||||
Arena Perm;
|
||||
|
||||
Array<TimeString> ParseSrtFile(Arena *arena, String filename) {
|
||||
String content = ReadFile(*arena, filename);
|
||||
Array<String> lines = Split(*arena, content, "\n");
|
||||
|
||||
IterRemove(lines) {
|
||||
IterRemovePrepare(lines);
|
||||
it = Trim(it);
|
||||
if (it.len == 0) remove_item = true;
|
||||
}
|
||||
|
||||
long section_number = 1;
|
||||
Array<TimeString> time_strings = {*arena};
|
||||
for (int i = 0; i < lines.len;) {
|
||||
String it0 = lines[i++];
|
||||
long num = strtol(it0.data, NULL, 10);
|
||||
Assert(section_number == num);
|
||||
section_number += 1;
|
||||
|
||||
TimeString item = {};
|
||||
String it1 = lines[i++];
|
||||
item.hour = (uint16_t)strtol(it1.data, NULL, 10);
|
||||
item.minute = (uint16_t)strtol(it1.data + 3, NULL, 10);
|
||||
item.second = (uint16_t)strtol(it1.data + 6, NULL, 10);
|
||||
|
||||
String next_section_number = Format(*arena, "%d", section_number);
|
||||
while (i < lines.len && lines[i] != next_section_number) {
|
||||
String it = lines[i];
|
||||
item.string = lines[i];
|
||||
time_strings.add(item);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
IterRemove(time_strings) {
|
||||
IterRemovePrepare(time_strings);
|
||||
if (i > 0 && AreEqual(time_strings[i - 1].string, time_strings[i].string, true)) {
|
||||
remove_item = true;
|
||||
}
|
||||
}
|
||||
|
||||
return time_strings;
|
||||
}
|
||||
|
||||
Array<Arena *> ArenasToFree;
|
||||
Array<TimeFile> TimeFiles;
|
||||
std::mutex ParseThreadMutex;
|
||||
void ParseThreadEntry(Array<String> files) {
|
||||
Arena *arena = AllocArena();
|
||||
ParseThreadMutex.lock();
|
||||
ArenasToFree.add(arena);
|
||||
ParseThreadMutex.unlock();
|
||||
For(files) {
|
||||
Array<TimeString> time_strings = ParseSrtFile(arena, it);
|
||||
ParseThreadMutex.lock();
|
||||
TimeFiles.add({time_strings, it});
|
||||
ParseThreadMutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
Array<XToTimeString> XToTimeStringArray = {Perm};
|
||||
Array<String> InitForFolder(String folder) {
|
||||
Scratch scratch;
|
||||
ArenasToFree.allocator = scratch;
|
||||
TimeFiles.allocator = scratch;
|
||||
|
||||
int thread_count = 16;
|
||||
Array<String> filenames = {Perm};
|
||||
Array<String> srt_files = {scratch};
|
||||
for (FileIter iter = IterateFiles(scratch, folder); IsValid(iter); Advance(&iter)) {
|
||||
filenames.add(Copy(Perm, iter.absolute_path));
|
||||
if (EndsWith(iter.filename, ".srt")) {
|
||||
srt_files.add(Copy(scratch, iter.absolute_path));
|
||||
}
|
||||
}
|
||||
|
||||
Array<std::thread *> threads = {scratch};
|
||||
int files_per_thread = srt_files.len / thread_count;
|
||||
int remainder = srt_files.len % thread_count;
|
||||
int fi = 0;
|
||||
for (int ti = 0; ti < thread_count; ti += 1) {
|
||||
Array<String> files = {scratch};
|
||||
for (int i = 0; fi < srt_files.len && i < files_per_thread + remainder; fi += 1, i += 1) {
|
||||
files.add(srt_files[fi]);
|
||||
}
|
||||
if (remainder) remainder = 0;
|
||||
threads.add(new std::thread(ParseThreadEntry, files));
|
||||
}
|
||||
|
||||
For(threads) {
|
||||
it->join();
|
||||
delete it;
|
||||
}
|
||||
|
||||
ForItem(file, TimeFiles) {
|
||||
For(file.time_strings) {
|
||||
String s = Copy(XArena, it.string);
|
||||
s.data[s.len] = ' ';
|
||||
XToTimeStringArray.add({s, it.hour, it.minute, it.second, file.file});
|
||||
}
|
||||
}
|
||||
For(ArenasToFree) Release(it);
|
||||
|
||||
AppInitializedWithFolder = true;
|
||||
return filenames;
|
||||
}
|
||||
|
||||
//
|
||||
// Searching thread
|
||||
//
|
||||
|
||||
Arena MatchesArena;
|
||||
Array<String> Matches = {MatchesArena};
|
||||
XToTimeString *ItemFound;
|
||||
Array<char> Prompt; // system allocated
|
||||
|
||||
std::mutex SearchThreadArrayMutex;
|
||||
std::binary_semaphore SearchThreadSemaphore{0};
|
||||
bool SearchThreadStopSearching = false;
|
||||
bool SearchThreadRunning = true;
|
||||
void SearchThreadEntry() {
|
||||
InitArena(&MatchesArena);
|
||||
for (;;) {
|
||||
SearchThreadSemaphore.acquire();
|
||||
if (!SearchThreadRunning) break;
|
||||
SearchThreadStopSearching = false;
|
||||
|
||||
if (Prompt.len) {
|
||||
SearchThreadArrayMutex.lock();
|
||||
{
|
||||
Matches.clear();
|
||||
}
|
||||
SearchThreadArrayMutex.unlock();
|
||||
|
||||
String buffer = {(char *)XArena.data, (int64_t)XArena.len};
|
||||
String find = {Prompt.data, Prompt.len};
|
||||
int64_t index = 0;
|
||||
while (Seek(buffer, find, &index, SeekFlag_IgnoreCase)) {
|
||||
String found = {buffer.data + index, find.len};
|
||||
SearchThreadArrayMutex.lock();
|
||||
{
|
||||
Matches.add(found);
|
||||
}
|
||||
SearchThreadArrayMutex.unlock();
|
||||
|
||||
if (SearchThreadStopSearching) break;
|
||||
buffer = buffer.skip(index + find.len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SearchThreadClose(std::thread &thread) {
|
||||
SearchThreadRunning = false;
|
||||
SearchThreadSemaphore.release();
|
||||
thread.join();
|
||||
}
|
||||
|
||||
int main() {
|
||||
InitOS();
|
||||
InitScratch();
|
||||
InitArena(&Perm);
|
||||
InitArena(&XArena);
|
||||
Arena *frame_arena = AllocArena();
|
||||
XArena.align = 0;
|
||||
|
||||
String start_string = "C:/video";
|
||||
For(start_string) Prompt.add(it);
|
||||
|
||||
std::thread search_thread(SearchThreadEntry);
|
||||
int64_t chosen_text = 0;
|
||||
int64_t match_search_offset = 0;
|
||||
Array<String> filenames = {};
|
||||
|
||||
InitWindow(1920, 1080, "Transcript Browser");
|
||||
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
||||
SetTargetFPS(60);
|
||||
Font font = LoadFontEx("C:/Windows/Fonts/consola.ttf", 20, 0, 250);
|
||||
while (!WindowShouldClose()) {
|
||||
Clear(frame_arena);
|
||||
|
||||
for (int key = GetCharPressed(); key; key = GetCharPressed()) {
|
||||
UTF8Result utf8 = UTF32ToUTF8(key);
|
||||
if (utf8.error) {
|
||||
Prompt.add('?');
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0; i < utf8.len; i += 1) {
|
||||
Prompt.add(utf8.out_str[i]);
|
||||
match_search_offset = 0;
|
||||
SearchThreadStopSearching = true;
|
||||
SearchThreadSemaphore.release();
|
||||
}
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) {
|
||||
if (ItemFound) {
|
||||
ItemFound = NULL;
|
||||
} else if (Prompt.len > 0) {
|
||||
Prompt.pop();
|
||||
match_search_offset = 0;
|
||||
SearchThreadStopSearching = true;
|
||||
SearchThreadSemaphore.release();
|
||||
}
|
||||
}
|
||||
|
||||
int64_t offset_size = 1;
|
||||
if (IsKeyDown(KEY_LEFT_CONTROL)) {
|
||||
offset_size = 10;
|
||||
}
|
||||
if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) {
|
||||
chosen_text += offset_size;
|
||||
if (chosen_text > 10) match_search_offset += offset_size;
|
||||
}
|
||||
if (IsKeyPressed(KEY_UP) || IsKeyPressedRepeat(KEY_UP)) {
|
||||
chosen_text -= offset_size;
|
||||
match_search_offset -= offset_size;
|
||||
}
|
||||
chosen_text = Clamp(chosen_text, (int64_t)0, Max(Matches.len - 1, (int64_t)0));
|
||||
match_search_offset = Clamp(match_search_offset, (int64_t)0, Max(Matches.len - 1 - 10, (int64_t)0));
|
||||
|
||||
if (IsKeyPressed(KEY_ENTER)) {
|
||||
if (!AppInitializedWithFolder) {
|
||||
Prompt.add('\0');
|
||||
filenames = InitForFolder(Prompt.data);
|
||||
Prompt.clear();
|
||||
} else if (ItemFound) {
|
||||
String base = ChopLastPeriod(ItemFound->filepath); // .srt
|
||||
base = ChopLastPeriod(base); // .en
|
||||
|
||||
For(filenames) {
|
||||
if (StartsWith(it, base)) {
|
||||
if (EndsWith(it, ".mkv") || EndsWith(it, ".webm") || EndsWith(it, ".mp4")) {
|
||||
int seconds = ItemFound->hour * 60 * 60 + ItemFound->minute * 60 + ItemFound->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));
|
||||
printf("%.*s\n", FmtString(args));
|
||||
RunEx(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (Matches.len) {
|
||||
String string = Matches[chosen_text];
|
||||
For(XToTimeStringArray) {
|
||||
uintptr_t begin = (uintptr_t)(it.string.data);
|
||||
uintptr_t end = (uintptr_t)(it.string.data + it.string.len);
|
||||
uintptr_t needle = (uintptr_t)string.data;
|
||||
|
||||
if (needle >= begin && needle < end) {
|
||||
ItemFound = ⁢
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
float font_size = 20;
|
||||
float y = 0;
|
||||
int xwidth = MeasureTextEx(font, "_", font_size, 1).x;
|
||||
|
||||
if (!AppInitializedWithFolder) {
|
||||
Prompt.add('\0');
|
||||
DrawTextEx(font, "> ", {0, y}, font_size, 1, BLACK);
|
||||
DrawTextEx(font, Prompt.data, {(float)xwidth * 3, y}, font_size, 1, BLACK);
|
||||
Prompt.len -= 1;
|
||||
} else if (ItemFound) {
|
||||
uintptr_t begin_region = (uintptr_t)XArena.data;
|
||||
uintptr_t end_region = (uintptr_t)XArena.data + XArena.len;
|
||||
|
||||
uintptr_t begin = (uintptr_t)(ItemFound->string.data - 1000);
|
||||
uintptr_t end = (uintptr_t)(ItemFound->string.data + 1000);
|
||||
|
||||
begin = Clamp(begin, begin_region, end_region);
|
||||
end = Clamp(end, begin_region, end_region);
|
||||
String string = {(char *)begin, (int64_t)(end - begin)};
|
||||
|
||||
String filename = SkipToLastSlash(ItemFound->filepath);
|
||||
DrawTextEx(font, filename.data, {0, y}, font_size, 1, BLACK);
|
||||
y += font_size;
|
||||
|
||||
int per_line = (GetRenderWidth() / xwidth) - 20;
|
||||
|
||||
for (String it = string; it.len;) {
|
||||
String line = it.get_prefix(per_line);
|
||||
|
||||
if (ItemFound->string.data >= line.data && ItemFound->string.data < line.data + line.len) {
|
||||
DrawRectangleLines(0, y + font_size, GetRenderWidth(), 2, SKYBLUE);
|
||||
}
|
||||
|
||||
String line_terminated = Copy(*frame_arena, line);
|
||||
DrawTextEx(font, line_terminated.data, {0, y}, font_size, 1, DARKGRAY);
|
||||
|
||||
y += font_size;
|
||||
it = it.skip(per_line);
|
||||
}
|
||||
} else {
|
||||
Prompt.add('\0');
|
||||
DrawTextEx(font, "> ", {0, y}, font_size, 1, BLACK);
|
||||
DrawTextEx(font, Prompt.data, {(float)xwidth * 3, y}, font_size, 1, BLACK);
|
||||
Prompt.pop();
|
||||
y += font_size;
|
||||
|
||||
int64_t chars_per_line = GetRenderWidth() / xwidth - Prompt.len;
|
||||
|
||||
SearchThreadArrayMutex.lock();
|
||||
for (int64_t i = match_search_offset; i < Matches.len; i += 1) {
|
||||
String it = Matches[i];
|
||||
uintptr_t begin_region = (uintptr_t)XArena.data;
|
||||
uintptr_t end_region = (uintptr_t)XArena.data + XArena.len;
|
||||
|
||||
uintptr_t begin = (uintptr_t)(it.data - chars_per_line / 2);
|
||||
uintptr_t end = (uintptr_t)(it.data + chars_per_line / 2);
|
||||
|
||||
begin = Clamp(begin, begin_region, end_region);
|
||||
end = Clamp(end, begin_region, end_region);
|
||||
String string = Copy(*frame_arena, {(char *)begin, (int64_t)(end - begin)});
|
||||
|
||||
String string_first = Copy(*frame_arena, {(char *)begin, (int64_t)(it.data - begin)});
|
||||
String string_middle = Copy(*frame_arena, it);
|
||||
int width = MeasureTextEx(font, string_first.data, font_size, 1).x;
|
||||
if (chosen_text == i) DrawRectangleLines(0, y + font_size, GetRenderWidth(), 2, SKYBLUE);
|
||||
|
||||
String num = Format(*frame_arena, "%d", i);
|
||||
DrawTextEx(font, num.data, {0, y}, font_size, 1, DARKGRAY);
|
||||
DrawTextEx(font, string.data, {(float)xwidth * 4, y}, font_size, 1, DARKGRAY);
|
||||
DrawTextEx(font, string_middle.data, {(float)xwidth * 4 + (float)width, y}, font_size, 1, SKYBLUE);
|
||||
|
||||
y += font_size;
|
||||
if (y > GetRenderHeight()) break;
|
||||
}
|
||||
SearchThreadArrayMutex.unlock();
|
||||
}
|
||||
|
||||
if (IsKeyDown(KEY_F1)) DrawFPS(0, 0);
|
||||
EndDrawing();
|
||||
}
|
||||
CloseWindow();
|
||||
SearchThreadClose(search_thread);
|
||||
}
|
||||
Reference in New Issue
Block a user