Skip to content

Commit

Permalink
add xdg_config_home
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-0 authored Jan 7, 2025
1 parent 522a2d3 commit 2135a6f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ The appearance can be adapted at any point in time.
#### Using a configuration file (Optional)
If the primary `borders` process is started without any arguments (or launched
as a service by brew), it will search for a file at
`~/.config/borders/bordersrc` and execute it on launch if found.
`$XDG_CONFIG_HOME/borders/bordersrc` and execute it on launch if found.

An example configuration file could look like this:
`~/.config/borders/bordersrc`
`$XDG_CONFIG_HOME/borders/bordersrc`
```bash
#!/bin/bash

Expand Down
2 changes: 1 addition & 1 deletion docs/borders.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ If an instance of *borders* is already running, subsequent invocations will
update the existing process with the new arguments.

If no instance of *borders* is running and no arguments are supplied, we try
to execute a file at ~/.config/borders/bordersrc where a configuration command
to execute a file at ~/$XDG_CONFIG_HOME/borders/bordersrc where a configuration command
could be issued.

# NOMENCLATURE
Expand Down
38 changes: 27 additions & 11 deletions src/misc/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,34 @@ static inline bool file_setx(const char* filename) {
}

static inline void execute_config_file(const char* name, const char* filename) {
char *home = getenv("HOME");
if (!home) return;

uint32_t size = strlen(home) + strlen(name) + strlen(filename) + 256;
char path[size];
snprintf(path, size, "%s/.config/%s/%s", home, name, filename);
if (!file_exists(path)) {
snprintf(path, size, "%s/.%s", home, filename);
if (!file_exists(path)) {
debug("No config file found...\n");
char *xcfg = getenv("XDG_CONFIG_HOME")
if (!xcfg){
char *home = getenv("HOME");
if (!home){
return;
};
} else{
uint32_t size = strlen(home) + strlen(name) + strlen(filename) + 256;
char path[size];
snprintf(path, size, "%s/.config/%s/%s", home, name, filename);
if (!file_exists(path)) {
snprintf(path, size, "%s/.%s", home, filename);
if (!file_exists(path)) {
debug("No config file found...\n");
return;
};
}
}
} else {
uint32_t size = strlen(xcfg) + strlen(name) + strlen(filename) + 256;
char path[size];
snprintf(path, size, "%s/%s/%s", xcfg, name, filename);
if (!file_exists(path)) {
snprintf(path, size, "%s/.%s", xcfg, filename);
if (!file_exists(path)) {
debug("No config file found...\n");
return;
};
}
}

if (!file_setx(path)) {
Expand Down

0 comments on commit 2135a6f

Please sign in to comment.