Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
151:54744a48a7d7 | 152:8700806c2cc2 |
---|---|
1 #include "animia/strategies.h" | 1 #include "animia/strategies.h" |
2 #include "animia/util.h" | 2 #include "animia/util.h" |
3 #include "animia/fd.h" | 3 #include "animia/fd.h" |
4 #include "animia.h" | 4 #include "animia.h" |
5 | |
6 #include <iostream> | |
7 | |
8 #include <regex> | |
5 | 9 |
6 namespace animia::internal { | 10 namespace animia::internal { |
7 | 11 |
8 class Strategist { | 12 class Strategist { |
9 public: | 13 public: |
13 | 17 |
14 private: | 18 private: |
15 bool AddMedia(const MediaInfo media_information); | 19 bool AddMedia(const MediaInfo media_information); |
16 | 20 |
17 bool ApplyOpenFilesStrategy(); | 21 bool ApplyOpenFilesStrategy(); |
22 bool ApplyWindowTitleStrategy(); | |
18 | 23 |
19 Result& result_; | 24 Result& result_; |
20 }; | 25 }; |
21 | 26 |
22 bool Strategist::ApplyStrategies() { | 27 bool Strategist::ApplyStrategies() { |
23 bool success = false; | 28 bool success = false; |
24 | 29 |
25 for (const auto strategy : result_.player.strategies) { | 30 switch (result_.type) { |
26 switch (strategy) { | 31 case ResultType::Process: |
27 case Strategy::OpenFiles: | 32 success |= ApplyOpenFilesStrategy(); |
28 success |= ApplyOpenFilesStrategy(); | 33 break; |
29 break; | 34 case ResultType::Window: |
30 } | 35 std::cout << "Wat" << std::endl; |
36 success |= ApplyWindowTitleStrategy(); | |
37 break; | |
31 } | 38 } |
32 | 39 |
33 return success; | 40 return success; |
34 } | 41 } |
35 | 42 |
44 return success; | 51 return success; |
45 } | 52 } |
46 | 53 |
47 //////////////////////////////////////////////////////////////////////////////// | 54 //////////////////////////////////////////////////////////////////////////////// |
48 | 55 |
56 static bool ApplyWindowTitleFormat(const std::string& format, std::string& title) { | |
57 if (format.empty()) | |
58 return false; | |
59 | |
60 const std::regex pattern(format); | |
61 std::smatch match; | |
62 std::regex_match(title, match, pattern); | |
63 | |
64 // Use the first non-empty match result, because the regular expression may | |
65 // contain multiple sub-expressions. | |
66 for (size_t i = 1; i < match.size(); ++i) { | |
67 if (!match.str(i).empty()) { | |
68 title = match.str(i); | |
69 return true; | |
70 } | |
71 } | |
72 | |
73 // Results are empty, but the match was successful | |
74 if (!match.empty()) { | |
75 title.clear(); | |
76 return true; | |
77 } | |
78 | |
79 return true; | |
80 } | |
81 | |
82 static MediaInfoType InferMediaInformationType(const std::string& str) { | |
83 const std::regex path_pattern(R"(^(?:[A-Za-z]:[/\\]|\\\\)[^<>:"/\\|?*]+)"); | |
84 return (std::regex_search(str, path_pattern)) | |
85 ? MediaInfoType::File : MediaInfoType::Unknown; | |
86 } | |
87 | |
88 bool Strategist::ApplyWindowTitleStrategy() { | |
89 auto title = result_.window.text; | |
90 ApplyWindowTitleFormat(result_.player.window_title_format, title); | |
91 | |
92 return AddMedia({InferMediaInformationType(title), title}); | |
93 } | |
94 | |
49 bool Strategist::ApplyOpenFilesStrategy() { | 95 bool Strategist::ApplyOpenFilesStrategy() { |
50 bool success = false; | 96 bool success = false; |
51 | 97 |
52 const std::set<pid_t> pids{result_.process.pid}; | 98 const std::set<pid_t> pids{result_.process.pid}; |
53 std::vector<OpenFile> files; | |
54 | 99 |
55 fd.EnumerateOpenFiles(pids, files); | 100 auto open_file_proc = [&](const OpenFile& file) -> bool { |
101 success |= AddMedia({MediaInfoType::File, file.path}); | |
102 return true; | |
103 }; | |
56 | 104 |
57 for (const auto& [pid, path] : files) { | 105 fd.EnumerateOpenFiles(pids, open_file_proc); |
58 success |= AddMedia({MediaInfoType::File, path}); | |
59 } | |
60 | 106 |
61 return success; | 107 return success; |
62 } | 108 } |
63 | 109 |
64 //////////////////////////////////////////////////////////////////////////////// | 110 //////////////////////////////////////////////////////////////////////////////// |