Merge pull request #3331 from Eisfunke/eisfunke/regex-collection-replace
Enable using capture groups in window-rewrite
This commit is contained in:
		
						commit
						562e1e59b1
					
				|  | @ -36,7 +36,7 @@ class RegexCollection { | ||||||
|   std::map<std::string, std::string> regex_cache; |   std::map<std::string, std::string> regex_cache; | ||||||
|   std::string default_repr; |   std::string default_repr; | ||||||
| 
 | 
 | ||||||
|   std::string& find_match(std::string& value, bool& matched_any); |   std::string find_match(std::string& value, bool& matched_any); | ||||||
| 
 | 
 | ||||||
|  public: |  public: | ||||||
|   RegexCollection() = default; |   RegexCollection() = default; | ||||||
|  |  | ||||||
|  | @ -147,6 +147,7 @@ Additional to workspace name matching, the following *format-icons* can be set. | ||||||
| 		"class<firefox> title<.*github.*>": "", // Windows whose class is "firefox" and title contains "github". Note that "class" always comes first. | 		"class<firefox> title<.*github.*>": "", // Windows whose class is "firefox" and title contains "github". Note that "class" always comes first. | ||||||
| 		"foot": "", // Windows that contain "foot" in either class or title. For optimization reasons, it will only match against a title if at least one other window explicitly matches against a title. | 		"foot": "", // Windows that contain "foot" in either class or title. For optimization reasons, it will only match against a title if at least one other window explicitly matches against a title. | ||||||
| 		"code": "", | 		"code": "", | ||||||
|  | 		"title<.* - (.*) - VSCodium>": "codium $1"  // captures part of the window title and formats it into output | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  | @ -171,6 +171,7 @@ n.b.: the list of outputs can be obtained from command line using *swaymsg -t ge | ||||||
|   "window-rewrite": { |   "window-rewrite": { | ||||||
|     "class<firefox>": "", |     "class<firefox>": "", | ||||||
|     "class<kitty>": "k", |     "class<kitty>": "k", | ||||||
|  | 	"title<.* - (.*) - VSCodium>": "codium $1"  // captures part of the window title and formats it into output | ||||||
|   } |   } | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  | @ -31,11 +31,12 @@ RegexCollection::RegexCollection(const Json::Value& map, std::string default_rep | ||||||
|   std::sort(rules.begin(), rules.end(), [](Rule& a, Rule& b) { return a.priority > b.priority; }); |   std::sort(rules.begin(), rules.end(), [](Rule& a, Rule& b) { return a.priority > b.priority; }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| std::string& RegexCollection::find_match(std::string& value, bool& matched_any) { | std::string RegexCollection::find_match(std::string& value, bool& matched_any) { | ||||||
|   for (auto& rule : rules) { |   for (auto& rule : rules) { | ||||||
|     if (std::regex_search(value, rule.rule)) { |     std::smatch match; | ||||||
|  |     if (std::regex_search(value, match, rule.rule)) { | ||||||
|       matched_any = true; |       matched_any = true; | ||||||
|       return rule.repr; |       return match.format(rule.repr.data()); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue