add bash completion script

This commit is contained in:
2024-11-24 15:43:10 +01:00
parent 0df5a7a5ce
commit e9168203f5

View File

@@ -73,6 +73,9 @@ fn printHelp() void {
\\ show NAME Show the code for the authenticator with name NAME
\\
\\Options:
\\ --bash Print script to set up Bash shell integration. Usage: add
\\ eval "$(zig-totp --bash)"
\\ to your ~/.bashrc
\\ --config [path] The path to a config file (default is $XDG_CONFIG_HOME/zig-totp or $HOME/.zig-totp).
\\
;
@@ -182,7 +185,26 @@ fn parseArgs(allocator: Allocator, args: []const []const u8) !Args {
while (i < args.len) : (i += 1) {
const arg = args[i];
if (eql(u8, "--config", arg)) {
if (eql(u8, "--bash", arg)) {
const msg =
\\_zig_totp_completions()
\\{
\\ if [ "${COMP_WORDS[$COMP_CWORD-1]}" = 'show' ]
\\ then
\\ COMPREPLY=($(compgen -W "$(zig-totp list 2> /dev/null )" -- "${COMP_WORDS[$COMP_CWORD]}" ))
\\ elif [ "${COMP_WORDS[$COMP_CWORD-1]}" = '--config' ]
\\ then
\\ COMPREPLY=($(compgen -A file -- "${COMP_WORDS[$COMP_CWORD]}" ))
\\ else
\\ COMPREPLY=($(compgen -W "list show --bash --config" -- "${COMP_WORDS[$COMP_CWORD]}" ))
\\ fi
\\}
\\complete -F _zig_totp_completions zig-totp
\\
;
try std.io.getStdOut().writer().print("{s}\n", .{msg});
std.process.exit(0);
} else if (eql(u8, "--config", arg)) {
i += 1;
if (i >= args.len) {
std.debug.print("expected a path after parameter '--config'\n see 'zig-totp' for help\n", .{});