Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions i3-renameworkspaces.pl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$opts{'h'} and say("Usage: i3-renameworkspaces.pl [-h] [-c configfile]"), exit(1);

# config file handling
my $configname = $opts{'c'} || $ENV{'HOME'} . '/.i3workspaceconfig';
my $configname = $opts{'c'} || locate_config_file();
my $config = {};
if (open(my $fh, '<', $configname)) {
local $/; $config = decode_json(<$fh>); close($fh);
Expand All @@ -44,7 +44,7 @@ sub recurse {
}
else {
$$wss{$$parent{'num'}} = { windows => ($windows = []), oldname=> $$parent{'name'}};
}
}
}
if ($$parent{'window_properties'}) {
my $title = lc($$parent{'window_properties'}{'title'});
Expand Down Expand Up @@ -86,6 +86,39 @@ sub updatelabels {
});
}

sub locate_config_file {
my $filename = "i3workspaceconfig";

my $config_home_dir;
if ( defined( $ENV{'XDG_CONFIG_HOME'} ) ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create a list of possible candidate locations, and then loop through them, taking the first one that exists and eventually fall back to i3workspaceconfig?

$config_home_dir = "$ENV{'XDG_CONFIG_HOME'}";
}
else {
$config_home_dir = "$ENV{'HOME'}/.config";
}

my @config_path_proposal = (
"$config_home_dir/$filename",
"$config_home_dir/i3-renameworkspaces/config",
"$ENV{'HOME'}/.$filename"
);
my $config_path;
foreach my $candidate (@config_path_proposal) {
if ( -e $candidate && -f _ && -r _ ) {
say "Config found: $candidate";
$config_path = $candidate;
last;
}
}

if ( not length $config_path ) {
$config_path = "$ENV{'HOME'}/.$filename";
say "Config default: $config_path";
}

return $config_path;
}

$i3->subscribe({
window => sub { say('window'); updatelabels(); },
workspace => sub { say('workspace'); updatelabels(); },
Expand Down