Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138#!/usr/bin/env perl
use strict;use warnings;
my @project_dirs = ( "$ENV{HOME}/projects", "$ENV{HOME}/ag", "$ENV{HOME}/tmpdev",);
my @projects;
sub is_git_repo { my ($dir) = @_; return -d "$dir/.git" || -f "$dir/.git";}
sub is_pijul_repo { my ($dir) = @_; return -d "$dir/.pijul";}
sub is_svn_repo { my ($dir) = @_; return -d "$dir/.svn";}
sub is_flake { my ($dir) = @_; return -f "$dir/flake.nix";}
sub rel_to_home { my ($abs) = @_; $abs =~ s{^$ENV{HOME}/?}{}; return $abs;}
sub with_prefix { my ($label, $text) = @_; my $prefix = "[$label]"; my $indent = 6 - length($prefix); $indent = 0 if $indent < 0; return $prefix . ' ' x $indent . $text;}
sub as_git_repo { my ($p) = @_; return with_prefix("R", $p);}
sub as_pijul_repo { my ($p) = @_; return with_prefix("P", $p);}
sub as_svn_repo { my ($p) = @_; return with_prefix("S", $p);}
sub as_flake { my ($p) = @_; return with_prefix("F", $p);}
sub as_worktree { my ($p) = @_; return with_prefix("T", $p);}
push @projects, { label => as_git_repo("dotfiles"), path => "$ENV{HOME}/dotfiles",};
for my $root (@project_dirs) { opendir(my $dh, $root) or next; while (my $entry = readdir($dh)) { next if $entry =~ /^\./; my $sub = "$root/$entry"; next unless -d $sub;
if (is_git_repo($sub)) { push @projects, { label => as_git_repo(rel_to_home($sub)), path => $sub, }; } elsif (is_svn_repo($sub)) { push @projects, { label => as_svn_repo(rel_to_home($sub)), path => $sub, }; } elsif (is_pijul_repo($sub)) { push @projects, { label => as_pijul_repo(rel_to_home($sub)), path => $sub, }; } elsif (is_flake($sub)) { push @projects, { label => as_flake(rel_to_home($sub)), path => $sub, }; } else { opendir(my $sdh, $sub) or next; while (my $wt = readdir($sdh)) { next if $wt =~ /^\./; my $p = "$sub/$wt"; if (-d $p && is_git_repo($p)) { push @projects, { label => as_worktree(rel_to_home($p)), path => $p, }; } } closedir($sdh) } } closedir($dh);}
my $choices = join("\n", map { $_->{label} } @projects);my $selection = `printf "$choices" | fzf --prompt="Navigate to project: "`;chomp($selection);
if ($selection ne '') { for my $proj (@projects) { if ($proj->{label} eq $selection) { my $target = $proj->{path}; print "$target\n"; exit 0; } }}
exit 1;