Merge pull request #327 from MihailRis/open-folder

Add platform::open_folder & core.open_folder
This commit is contained in:
MihailRis
2024-10-25 14:59:04 +03:00
committed by GitHub
3 changed files with 29 additions and 5 deletions
+9
View File
@@ -167,6 +167,14 @@ static int l_get_setting_info(lua::State* L) {
throw std::runtime_error("unsupported setting type");
}
#include "util/platform.hpp"
static int l_open_folder(lua::State* L) {
auto path = engine->getPaths()->resolve(lua::require_string(L, 1));
platform::open_folder(path);
return 0;
}
/// @brief Quit the game
static int l_quit(lua::State*) {
Window::setShouldClose(true);
@@ -184,5 +192,6 @@ const luaL_Reg corelib[] = {
{"set_setting", lua::wrap<l_set_setting>},
{"str_setting", lua::wrap<l_str_setting>},
{"get_setting_info", lua::wrap<l_get_setting_info>},
{"open_folder", lua::wrap<l_open_folder>},
{"quit", lua::wrap<l_quit>},
{NULL, NULL}};
+15 -4
View File
@@ -7,12 +7,11 @@
#include <sstream>
#include "typedefs.hpp"
#include "stringutil.hpp"
#ifdef _WIN32
#include <Windows.h>
#include "stringutil.hpp"
void platform::configure_encoding() {
// set utf-8 encoding to console output
SetConsoleOutputCP(CP_UTF8);
@@ -35,7 +34,6 @@ std::string platform::detect_locale() {
.replace(2, 1, "_")
.substr(0, 5);
}
#else
void platform::configure_encoding() {
@@ -49,5 +47,18 @@ std::string platform::detect_locale() {
return preferredLocaleName.substr(0, 5);
}
#endif
void platform::open_folder(const std::filesystem::path& folder) {
if (!std::filesystem::is_directory(folder)) {
return;
}
#ifdef __APPLE__
auto cmd = "open "+util::quote(folder.u8string());
#elif defined(_WIN32)
auto cmd = "start explorer "+util::quote(folder.u8string());
#else
auto cmd = "xdg-open "+util::quote(folder.u8string());
#endif
system(cmd.c_str());
}
+5 -1
View File
@@ -1,9 +1,13 @@
#pragma once
#include <string>
#include <filesystem>
namespace platform {
void configure_encoding();
// @return environment locale in ISO format ll_CC
/// @return environment locale in ISO format ll_CC
std::string detect_locale();
/// @brief Open folder using system file manager asynchronously
/// @param folder target folder
void open_folder(const std::filesystem::path& folder);
}