Skip to content

Commit

Permalink
feat: store completions in $XDG_CONFIG_HOME instead of $HOME (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-recki authored Feb 10, 2023
1 parent bb277bb commit d2d7459
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It works on bash and zsh on Linux, macOS, and Windows.

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.

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.
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.

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

Expand Down
12 changes: 8 additions & 4 deletions lib/src/installer/completion_installation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,21 @@ class CompletionInstallation {
/// %LOCALAPPDATA%/DartCLICompletion
///
/// If [isWindows] is false, it will return the directory defined by
/// $HOME/.dart_cli_completion
/// $XDG_CONFIG_HOME/.dart_cli_completion or $HOME/.dart_cli_completion
@visibleForTesting
Directory get completionConfigDir {
if (isWindows) {
// Use localappdata on windows
final localAppData = environment['LOCALAPPDATA']!;
return Directory(path.join(localAppData, 'DartCLICompletion'));
} else {
// Use home on posix systems
final home = environment['HOME']!;
return Directory(path.join(home, '.dart-cli-completion'));
// Try using XDG config folder
var dirPath = environment['XDG_CONFIG_HOME'];
// Fallback to $HOME if not following XDG specification
if (dirPath == null || dirPath.isEmpty) {
dirPath = environment['HOME'];
}
return Directory(path.join(dirPath!, '.dart-cli-completion'));
}
}

Expand Down
45 changes: 32 additions & 13 deletions test/src/installer/completion_installation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,39 @@ void main() {
);
});

test('gets config dir location on posix', () {
final installation = CompletionInstallation(
configuration: zshConfiguration,
logger: logger,
isWindows: false,
environment: {
'HOME': tempDir.path,
},
);
group('gets config dir location on posix', () {
test('respects XDG home', () {
final installation = CompletionInstallation(
configuration: zshConfiguration,
logger: logger,
isWindows: false,
environment: {
'XDG_CONFIG_HOME': tempDir.path,
'HOME': 'ooohnoooo',
},
);

expect(
installation.completionConfigDir.path,
path.join(tempDir.path, '.dart-cli-completion'),
);
expect(
installation.completionConfigDir.path,
path.join(tempDir.path, '.dart-cli-completion'),
);
});

test('defaults to home', () {
final installation = CompletionInstallation(
configuration: zshConfiguration,
logger: logger,
isWindows: false,
environment: {
'HOME': tempDir.path,
},
);

expect(
installation.completionConfigDir.path,
path.join(tempDir.path, '.dart-cli-completion'),
);
});
});
});

Expand Down

0 comments on commit d2d7459

Please sign in to comment.