Skip to content

Commit b871fcc

Browse files
committed
Use '$HOME/.config/i3workspaceconfig' as an alternate config file
'$HOME/.i3workspaceconfig' remains the default configuration file, but if '$HOME/.config/i3workspaceconfig' or '$HOME/.config/i3-renameworkspaces/config' exists, the first one found has priority. It is still possible to define another configuration file via the '-c' option
1 parent 9f864a6 commit b871fcc

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

i3-renameworkspaces.pl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
$opts{'h'} and say("Usage: i3-renameworkspaces.pl [-h] [-c configfile]"), exit(1);
1818

1919
# config file handling
20-
my $configname = $opts{'c'} || $ENV{'HOME'} . '/.i3workspaceconfig';
20+
my $configname = $opts{'c'} || locate_config_file();
2121
my $config = {};
2222
if (open(my $fh, '<', $configname)) {
2323
local $/; $config = decode_json(<$fh>); close($fh);
@@ -44,7 +44,7 @@ sub recurse {
4444
}
4545
else {
4646
$$wss{$$parent{'num'}} = { windows => ($windows = []), oldname=> $$parent{'name'}};
47-
}
47+
}
4848
}
4949
if ($$parent{'window_properties'}) {
5050
my $title = lc($$parent{'window_properties'}{'title'});
@@ -86,6 +86,39 @@ sub updatelabels {
8686
});
8787
}
8888

89+
sub locate_config_file {
90+
my $filename = "i3workspaceconfig";
91+
92+
my $config_home_dir;
93+
if ( defined( $ENV{'XDG_CONFIG_HOME'} ) ) {
94+
$config_home_dir = "$ENV{'XDG_CONFIG_HOME'}";
95+
}
96+
else {
97+
$config_home_dir = "$ENV{'HOME'}/.config";
98+
}
99+
100+
my @config_path_proposal = (
101+
"$config_home_dir/$filename",
102+
"$config_home_dir/i3-renameworkspaces/config",
103+
"$ENV{'HOME'}/.$filename"
104+
);
105+
my $config_path;
106+
foreach my $candidate (@config_path_proposal) {
107+
if ( -e $candidate && -f _ && -r _ ) {
108+
say "Config found: $candidate";
109+
$config_path = $candidate;
110+
last;
111+
}
112+
}
113+
114+
if ( not length $config_path ) {
115+
$config_path = "$ENV{'HOME'}/.$filename";
116+
say "Config default: $config_path";
117+
}
118+
119+
return $config_path;
120+
}
121+
89122
$i3->subscribe({
90123
window => sub { say('window'); updatelabels(); },
91124
workspace => sub { say('workspace'); updatelabels(); },

0 commit comments

Comments
 (0)