App-CdUtils

 view release on metacpan or  search on metacpan

script/cdprevsibling-backend  view on Meta::CPAN

#!perl

use 5.010001;
use strict;
use warnings;

use Cwd qw(cwd);
use File::chdir;
use Getopt::Long::Complete qw(GetOptionsWithCompletion);

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2026-04-07'; # DATE
our $DIST = 'App-CdUtils'; # DIST
our $VERSION = '0.015'; # VERSION

# FRAGMENT id=shcompgen-hint completer=1 for=cdprevsibling
# FRAGMENT id=shcompgen-hint completer=1 for=cdnextsibling

# FRAGMENT id=shcompgen-hint completer=1 for=gdprevsibling
# FRAGMENT id=shcompgen-hint completer=1 for=gdnextsibling

# FRAGMENT id=shcompgen-hint completer=1 for=lsprevsibling
# FRAGMENT id=shcompgen-hint completer=1 for=lsnextsibling

my $cwd = $ENV{PWD} || cwd();
my @cwd_elems = split m!/+!, $cwd;
my $cmd = $0 =~ /prev/ ? 'cdprevsibling' : 'cdnextsibling';
my $sort_files_opts = $ENV{CDPREVNEXTSIBLING_SORT_FILES_OPTS} // '--by-field=name';
my %opts = (
    num => 1,
    level => 1,
    action => 'cd',
);

GetOptionsWithCompletion(
    sub {
        [];
    },
    'help|h' => sub {
        print <<USAGE;
Usage:
  % $cmd [options]
Options:
  --help, -h
  --version, -v
  --num=i, -n
  --level=i, -l
  --sort=s, -S, -t
  --ls
USAGE
        exit 0;
    },
    'version|v' => sub {
        no warnings 'once';
        print "$cmd version ", ($main::VERSION || "dev"), "\n";
        exit 0;
    },
    'num|n=i' => \$opts{num},
    'level|l=i' => \$opts{level},
    'ls' => sub { $opts{action} = 'ls' },
    'sort=s' => sub {
        my $val = $1;
        if ($val eq 'name') {
            $sort_files_opts = "--by-field name";
        } elsif ($val eq 'size') {
            $sort_files_opts = "--by-field size --reverse";
        } elsif ($val eq 'time') {
            $sort_files_opts = "--by-field time --reverse";
        } else {
            die "$cmd: Invalid value for --sort, please only specify name, size, or time\n";
        }
    },
    'S' => sub { $opts{sort} = 'size' },
    't' => sub { $opts{sort} = 'time' },
);

die "$cmd: Level must be at least 1\n" if $opts{level} < 1;

# we are at the root, no siblings
unless (@cwd_elems - $opts{level} + 1 > 0) {
    print ".\n";
    exit 0;
}

my @dirs;
{



( run in 0.720 second using v1.01-cache-2.11-cpan-71847e10f99 )