Skip to content

Commit f0de674

Browse files
committed
test user.terminal_version #1
1 parent dace0e0 commit f0de674

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/query/linux/user.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
// #endif
4747

4848
#include "query.hpp"
49+
#if CF_MACOS
50+
# include "rapidxml-1.13/rapidxml.hpp"
51+
#endif
4952
#include "switch_fnv1a.hpp"
5053
#include "util.hpp"
5154
#include "utils/dewm.hpp"
@@ -189,7 +192,7 @@ static std::string get_shell_name(const std::string_view shell_path)
189192
return shell_path.substr(shell_path.rfind('/') + 1).data();
190193
}
191194

192-
static std::string get_term_name_env()
195+
static std::string get_term_name_env(bool get_default = false)
193196
{
194197
if (getenv("SSH_TTY") != NULL)
195198
return getenv("SSH_TTY");
@@ -216,11 +219,21 @@ static std::string get_term_name_env()
216219
getenv("GNOME_TERMINAL_SERVICE") != NULL)
217220
return "gnome-terminal";
218221

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+
}
221232

222-
if (getenv("TERM") != NULL)
223-
return getenv("TERM");
233+
env = getenv("TERM");
234+
if (env != NULL)
235+
return env;
236+
}
224237

225238
return UNKNOWN;
226239
}
@@ -361,6 +374,33 @@ static std::string get_term_version(const std::string_view term_name)
361374
bool remove_term_name = true;
362375
std::string ret;
363376

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+
364404
switch (fnv1a16::hash(str_tolower(term_name.data())))
365405
{
366406
case "st"_fnv1a16:

0 commit comments

Comments
 (0)