add user name placeholder (#10109)

* Add user name placeholder

* non-ASCII character support

* fix: Explicitly include <boost/nowide/cstdlib.hpp>
This commit is contained in:
krmz-krmz
2025-07-18 16:43:46 +09:00
committed by GitHub
parent 46af00aa01
commit 11e2c054d0
4 changed files with 13 additions and 0 deletions

View File

@@ -2041,6 +2041,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
// Prepare the helper object for replacing placeholders in custom G-code and output filename.
m_placeholder_parser_integration.parser = print.placeholder_parser();
m_placeholder_parser_integration.parser.update_timestamp();
m_placeholder_parser_integration.parser.update_user_name();
m_placeholder_parser_integration.context.rng = std::mt19937(std::chrono::high_resolution_clock::now().time_since_epoch().count());
// Enable passing global variables between PlaceholderParser invocations.
m_placeholder_parser_integration.context.global_config = std::make_unique<DynamicConfig>();

View File

@@ -27,6 +27,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/nowide/convert.hpp>
#include <boost/nowide/cstdlib.hpp>
// Spirit v2.5 allows you to suppress automatic generation
// of predefined terminals to speed up complation. With
@@ -71,6 +72,7 @@ PlaceholderParser::PlaceholderParser(const DynamicConfig *external_config) : m_e
this->set("version", std::string(SoftFever_VERSION));
this->apply_env_variables();
this->update_timestamp();
this->update_user_name();
}
void PlaceholderParser::update_timestamp(DynamicConfig &config)
@@ -98,6 +100,12 @@ void PlaceholderParser::update_timestamp(DynamicConfig &config)
config.set_key_value("second", new ConfigOptionInt(timeinfo->tm_sec));
}
void PlaceholderParser::update_user_name(DynamicConfig &config)
{
const char* user = boost::nowide::getenv("USER") ? boost::nowide::getenv("USER") : boost::nowide::getenv("USERNAME") ? boost::nowide::getenv("USERNAME") : "unknown";
config.set_key_value("user", new ConfigOptionString(user));
}
static inline bool opts_equal(const DynamicConfig &config_old, const DynamicConfig &config_new, const std::string &opt_key)
{
const ConfigOption *opt_old = config_old.option(opt_key);

View File

@@ -71,6 +71,9 @@ public:
// Update timestamp, year, month, day, hour, minute, second variables at m_config.
void update_timestamp() { update_timestamp(m_config); }
static void update_user_name(DynamicConfig &config);
void update_user_name() { update_user_name(m_config); }
private:
// config has a higher priority than external_config when looking up a symbol.
DynamicConfig m_config;

View File

@@ -69,6 +69,7 @@ std::string PrintBase::output_filename(const std::string &format, const std::str
cfg = *config_override;
cfg.set_key_value("version", new ConfigOptionString(std::string(SoftFever_VERSION)));
PlaceholderParser::update_timestamp(cfg);
PlaceholderParser::update_user_name(cfg);
this->update_object_placeholders(cfg, default_ext);
if (! filename_base.empty()) {
cfg.set_key_value("input_filename", new ConfigOptionString(filename_base + default_ext));