#!perl

use strict;
use warnings;

use Complete::Util qw(complete_array_elem hashify_answer);
use Cwd qw(getcwd);
use Getopt::Long::Complete qw(GetOptionsWithCompletion);

# FRAGMENT id=shcompgen-hint completer=1 for=cdsibling

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2025-09-23'; # DATE
our $DIST = 'App-CdUtils'; # DIST
our $VERSION = '0.010'; # VERSION

my $cwd = getcwd();
my ($curname) = $cwd =~ m!/([^/]*)$!;

GetOptionsWithCompletion(
    sub {
        my %args = @_;
        my $type = $args{type};
        my $word = $args{word};

        if ($word eq '') {
            my $res = hashify_answer(
                complete_array_elem(
                    word=>$word, array=>[$curname, "$curname "]),
            );
            $res->{final} = 1;
            return $res;
        } elsif ($type eq 'arg') {
            my @dirs;
            opendir my $dh, "..";
            for my $e (readdir $dh) {
                next if $e eq '.' || $e eq '..';
                next unless -d "../$e";
                push @dirs, $e;
            }
            return complete_array_elem(
                word=>$word, array=>\@dirs);
        }
        undef;
    },
    'help|h' => sub {
        print <<USAGE;
Usage:
  % cdsibling <dirname>
Options:
  --help, -h
  --version, -v
USAGE
        exit 0;
    },
    'version|v' => sub {
        no warnings 'once';
        print "cdsibling version ", ($main::VERSION || "dev"), "\n";
        exit 0;
    },
);
die "Usage: cdsibling <dirname>\n" unless @ARGV == 1;

$ARGV[0] = '' unless defined $ARGV[0];
print "../$ARGV[0]\n";

# ABSTRACT: Change to a sibling directory, with completion set to working directory
# PODNAME: cdsibling-backend

__END__

=pod

=encoding UTF-8

=head1 NAME

cdsibling-backend - Change to a sibling directory, with completion set to working directory

=head1 VERSION

This document describes version 0.010 of cdsibling-backend (from Perl distribution App-CdUtils), released on 2025-09-23.

=head1 SYNOPSIS

To use in shell:

 % cdsibling() { cd `cdsibling-backend "$1"`; }

A demonstration:

 % ls
 Foo-Bar
 Foo-Baz
 Foo-Qux

 % cd Foo-Bar

Case 1: tab completion when empty -> working directory so user can edit to cd to
a sibling directory similar to working directory.

 % cdsibling <tab>
 % cdsibling Foo-Bar_
 % cdsibling Foo-Baz          # user edits the command-line and type

Case 2: tab completion when not empty -> from list of sibling directories.

 % cdsibling Foo<tab>
 Foo-Bar  Foo-Baz   Foo-Qux

=head1 DESCRIPTION

B<cdsibling> is a convenience command to use to change to a sibling directory.
See SYNOPSIS for illustration on how it works.

=head1 COMPLETION

This script has shell tab completion capability with support for several
shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C cdsibling-backend cdsibling-backend

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is recommended, however, that you install modules using L<cpanm-shcompgen>
which can activate shell completion for scripts immediately.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete cdsibling-backend 'p/*/`cdsibling-backend`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is also recommended to install C<shcompgen> (see above).

=head2 other shells

For fish and zsh, install C<shcompgen> as described above.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-CdUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-CdUtils>.

=head1 SEE ALSO

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 CONTRIBUTING


To contribute, you can send patches by email/via RT, or send pull requests on
GitHub.

Most of the time, you don't need to build the distribution yourself. You can
simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your
system), you can install L<Dist::Zilla>,
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
that are considered a bug and can be reported to me.

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-CdUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=cut
