46
46
// #endif
47
47
48
48
#include " query.hpp"
49
+ #if CF_MACOS
50
+ # include " rapidxml-1.13/rapidxml.hpp"
51
+ #endif
49
52
#include " switch_fnv1a.hpp"
50
53
#include " util.hpp"
51
54
#include " utils/dewm.hpp"
@@ -189,7 +192,7 @@ static std::string get_shell_name(const std::string_view shell_path)
189
192
return shell_path.substr (shell_path.rfind (' /' ) + 1 ).data ();
190
193
}
191
194
192
- static std::string get_term_name_env ()
195
+ static std::string get_term_name_env (bool get_default = false )
193
196
{
194
197
if (getenv (" SSH_TTY" ) != NULL )
195
198
return getenv (" SSH_TTY" );
@@ -216,11 +219,21 @@ static std::string get_term_name_env()
216
219
getenv (" GNOME_TERMINAL_SERVICE" ) != NULL )
217
220
return " gnome-terminal" ;
218
221
219
- if (getenv (" TERM_PROGRAM" ) != NULL )
220
- return getenv (" TERM_PROGRAM" );
222
+ if (get_default)
223
+ {
224
+ char *env = getenv (" TERM_PROGRAM" );
225
+ if (env != NULL )
226
+ {
227
+ if (hasStart (env, " Apple" ))
228
+ return " Apple Terminal" ;
229
+
230
+ return env;
231
+ }
221
232
222
- if (getenv (" TERM" ) != NULL )
223
- return getenv (" TERM" );
233
+ env = getenv (" TERM" );
234
+ if (env != NULL )
235
+ return env;
236
+ }
224
237
225
238
return UNKNOWN;
226
239
}
@@ -361,6 +374,33 @@ static std::string get_term_version(const std::string_view term_name)
361
374
bool remove_term_name = true ;
362
375
std::string ret;
363
376
377
+ #if CF_MACOS
378
+ if (term_name == " Apple Terminal" )
379
+ {
380
+ std::ifstream f (" /System/Applications/Utilities/Terminal.app/Content/version.plist" , std::ios::in);
381
+ if (!f.is_open ())
382
+ goto skip;
383
+
384
+ std::string buffer ((std::istreambuf_iterator<char >(f)), std::istreambuf_iterator<char >());
385
+ buffer.push_back (' \0 ' );
386
+
387
+ rapidxml::xml_document<> doc;
388
+ doc.parse <0 >(&buffer[0 ]);
389
+ rapidxml::xml_node<>* root_node = doc.first_node (" plist" )->first_node (" dict" )->first_node (" key" );
390
+
391
+ for (; root_node; root_node = root_node->next_sibling ())
392
+ {
393
+ const std::string_view key = root_node->value (); // <key>ProductName</key>
394
+ root_node = root_node->next_sibling ();
395
+ const std::string_view value = root_node->value (); // <string>macOS</string>
396
+ if (key == " CFBundleVersion" )
397
+ return value.data ();
398
+ }
399
+ }
400
+
401
+ skip:
402
+ #endif
403
+
364
404
switch (fnv1a16::hash (str_tolower (term_name.data ())))
365
405
{
366
406
case " st" _fnv1a16:
0 commit comments