Skip to content

Commit

Permalink
Append script parent dir to lua package.path
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Švagelj <tin.svagelj@live.com>
  • Loading branch information
Caellian committed Apr 30, 2024
1 parent 1efc24d commit 5051e0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
37 changes: 36 additions & 1 deletion src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <cmath>
#include <cstdarg>
#include <ctime>
#include <filesystem>
#include <iostream>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -275,7 +276,7 @@ int text_width = 1,
struct information info;

/* path to config file */
std::string current_config;
std::filesystem::path current_config;

/* set to 1 if you want all text to be in uppercase */
static conky::simple_config_setting<bool> stuff_in_uppercase("uppercase", false,
Expand Down Expand Up @@ -1961,6 +1962,40 @@ void load_config_file() {
lua::stack_sentry s(l);
l.checkstack(2);

// Extend lua package.path so scripts can use relative paths
{
struct stat file_stat {};

std::string path_ext;

// add XDG directory to lua path
auto xdg_path =
std::filesystem::path(to_real_path(XDG_CONFIG_FILE)).parent_path();
if (stat(xdg_path.c_str(), &file_stat) == 0) {
path_ext.push_back(';');
path_ext.append(xdg_path);
path_ext.append("/?.lua");
}

auto parent_path = current_config.parent_path();
if (xdg_path != parent_path && stat(path_ext.c_str(), &file_stat) == 0) {
path_ext.push_back(';');
path_ext.append(parent_path);
path_ext.append("/?.lua");
}

l.getglobal("package");
l.getfield(-1, "path");

auto path = l.tostring(-1);
path.append(path_ext);
l.pop();
l.pushstring(path.c_str());

l.setfield(-2, "path");
l.pop();
}

try {
#ifdef BUILD_BUILTIN_CONFIG
if (current_config == builtin_config_magic) {
Expand Down
3 changes: 2 additions & 1 deletion src/conky.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <config.h> /* defines */
#include <sys/utsname.h> /* struct uname_s */
#include <csignal>
#include <filesystem>
#include <memory>

#include "colours.h"
Expand Down Expand Up @@ -346,7 +347,7 @@ extern conky::simple_config_setting<bool> utf8_mode;
extern conky::range_config_setting<unsigned int> max_user_text;

/* path to config file */
extern std::string current_config;
extern std::filesystem::path current_config;

#define DEFAULT_TEXT_BUFFER_SIZE_S "##DEFAULT_TEXT_BUFFER_SIZE"

Expand Down

0 comments on commit 5051e0a

Please sign in to comment.