Skip to content

Commit d2d7459

Browse files
authored
feat: store completions in $XDG_CONFIG_HOME instead of $HOME (#50)
1 parent bb277bb commit d2d7459

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It works on bash and zsh on Linux, macOS, and Windows.
1010

1111
There are several ways to achieve shell completions for CLI commands. Most of them depend on which shell you are using and, in some cases, the terminal.
1212

13-
The approach used by `cli_completion` is to create shell script files in a directory under the user's home ( `~/.dart-cli-completion` for Linux/macOS users) and then source these scripts in the shell initialization files (`~/.zshrc`, for example). For Windows users, completion information is stored in local app data (typically something like `C:\Users\You\AppData\Local`). Currently, completion will only work from a bash shell on Windows.
13+
The approach used by `cli_completion` is to create shell script files in a directory under the user's home ( `$XDG_CONFIG_HOME/.dart-cli-completion` or `~/.dart-cli-completion` for Linux/macOS users) and then source these scripts in the shell initialization files (`~/.zshrc`, for example). For Windows users, completion information is stored in local app data (typically something like `C:\Users\You\AppData\Local`). Currently, completion will only work from a bash shell on Windows.
1414

1515
We call this process [installation](#the-installation-process).
1616

lib/src/installer/completion_installation.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,21 @@ class CompletionInstallation {
7272
/// %LOCALAPPDATA%/DartCLICompletion
7373
///
7474
/// If [isWindows] is false, it will return the directory defined by
75-
/// $HOME/.dart_cli_completion
75+
/// $XDG_CONFIG_HOME/.dart_cli_completion or $HOME/.dart_cli_completion
7676
@visibleForTesting
7777
Directory get completionConfigDir {
7878
if (isWindows) {
7979
// Use localappdata on windows
8080
final localAppData = environment['LOCALAPPDATA']!;
8181
return Directory(path.join(localAppData, 'DartCLICompletion'));
8282
} else {
83-
// Use home on posix systems
84-
final home = environment['HOME']!;
85-
return Directory(path.join(home, '.dart-cli-completion'));
83+
// Try using XDG config folder
84+
var dirPath = environment['XDG_CONFIG_HOME'];
85+
// Fallback to $HOME if not following XDG specification
86+
if (dirPath == null || dirPath.isEmpty) {
87+
dirPath = environment['HOME'];
88+
}
89+
return Directory(path.join(dirPath!, '.dart-cli-completion'));
8690
}
8791
}
8892

test/src/installer/completion_installation_test.dart

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,39 @@ void main() {
7373
);
7474
});
7575

76-
test('gets config dir location on posix', () {
77-
final installation = CompletionInstallation(
78-
configuration: zshConfiguration,
79-
logger: logger,
80-
isWindows: false,
81-
environment: {
82-
'HOME': tempDir.path,
83-
},
84-
);
76+
group('gets config dir location on posix', () {
77+
test('respects XDG home', () {
78+
final installation = CompletionInstallation(
79+
configuration: zshConfiguration,
80+
logger: logger,
81+
isWindows: false,
82+
environment: {
83+
'XDG_CONFIG_HOME': tempDir.path,
84+
'HOME': 'ooohnoooo',
85+
},
86+
);
8587

86-
expect(
87-
installation.completionConfigDir.path,
88-
path.join(tempDir.path, '.dart-cli-completion'),
89-
);
88+
expect(
89+
installation.completionConfigDir.path,
90+
path.join(tempDir.path, '.dart-cli-completion'),
91+
);
92+
});
93+
94+
test('defaults to home', () {
95+
final installation = CompletionInstallation(
96+
configuration: zshConfiguration,
97+
logger: logger,
98+
isWindows: false,
99+
environment: {
100+
'HOME': tempDir.path,
101+
},
102+
);
103+
104+
expect(
105+
installation.completionConfigDir.path,
106+
path.join(tempDir.path, '.dart-cli-completion'),
107+
);
108+
});
90109
});
91110
});
92111

0 commit comments

Comments
 (0)