diff dep/animia/src/strategist.cc @ 152:8700806c2cc2

dep/animia: awesome new breaking changes! I'm so tired
author Paper <mrpapersonic@gmail.com>
date Wed, 15 Nov 2023 02:34:59 -0500
parents 54744a48a7d7
children bd439dd6ffc5
line wrap: on
line diff
--- a/dep/animia/src/strategist.cc	Tue Nov 14 16:31:21 2023 -0500
+++ b/dep/animia/src/strategist.cc	Wed Nov 15 02:34:59 2023 -0500
@@ -3,6 +3,10 @@
 #include "animia/fd.h"
 #include "animia.h"
 
+#include <iostream>
+
+#include <regex>
+
 namespace animia::internal {
 
 class Strategist {
@@ -15,6 +19,7 @@
 		bool AddMedia(const MediaInfo media_information);
 
 		bool ApplyOpenFilesStrategy();
+		bool ApplyWindowTitleStrategy();
 
 		Result& result_;
 };
@@ -22,12 +27,14 @@
 bool Strategist::ApplyStrategies() {
 	bool success = false;
 
-	for (const auto strategy : result_.player.strategies) {
-		switch (strategy) {
-			case Strategy::OpenFiles:
-				success |= ApplyOpenFilesStrategy();
-				break;
-		}
+	switch (result_.type) {
+		case ResultType::Process:
+			success |= ApplyOpenFilesStrategy();
+			break;
+		case ResultType::Window:
+			std::cout << "Wat" << std::endl;
+			success |= ApplyWindowTitleStrategy();
+			break;
 	}
 
 	return success;
@@ -46,17 +53,56 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
+static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) {
+	if (format.empty())
+		return false;
+
+	const std::regex pattern(format);
+	std::smatch match;
+	std::regex_match(title, match, pattern);
+
+	// Use the first non-empty match result, because the regular expression may
+	// contain multiple sub-expressions.
+	for (size_t i = 1; i < match.size(); ++i) {
+		if (!match.str(i).empty()) {
+			title = match.str(i);
+			return true;
+		}
+	}
+
+	// Results are empty, but the match was successful
+	if (!match.empty()) {
+		title.clear();
+		return true;
+	}
+
+	return true;
+}
+
+static MediaInfoType InferMediaInformationType(const std::string& str) {
+	const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)");
+	return (std::regex_search(str, path_pattern))
+		? MediaInfoType::File : MediaInfoType::Unknown;
+}
+
+bool Strategist::ApplyWindowTitleStrategy() {
+	auto title = result_.window.text;
+	ApplyWindowTitleFormat(result_.player.window_title_format, title);
+
+	return AddMedia({InferMediaInformationType(title), title});
+}
+
 bool Strategist::ApplyOpenFilesStrategy() {
 	bool success = false;
 
 	const std::set<pid_t> pids{result_.process.pid};
-	std::vector<OpenFile> files;
-
-	fd.EnumerateOpenFiles(pids, files);
 
-	for (const auto& [pid, path] : files) {
-		success |= AddMedia({MediaInfoType::File, path});
-	}
+	auto open_file_proc = [&](const OpenFile& file) -> bool {
+		success |= AddMedia({MediaInfoType::File, file.path});
+		return true;
+	};
+
+	fd.EnumerateOpenFiles(pids, open_file_proc);
 
 	return success;
 }