Changeset e6cec01
- Timestamp:
- Dec 28, 2009, 12:03:29 AM (15 years ago)
- Branches:
- master, release-1.10, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- dc8f6e0
- Parents:
- 880311d
- git-author:
- Nelson Elhage <nelhage@mit.edu> (12/27/09 23:48:30)
- git-committer:
- Nelson Elhage <nelhage@mit.edu> (12/28/09 00:03:29)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/lib/BarnOwl/Completion/Util.pm
r69c27e6 re6cec01 5 5 6 6 use base qw(Exporter); 7 our @EXPORT_OK = qw(complete_flags );7 our @EXPORT_OK = qw(complete_flags complete_file); 8 8 9 9 use Getopt::Long; 10 use Cwd qw(abs_path); 11 use File::Basename qw(dirname basename); 12 10 13 11 14 sub complete_flags { … … 67 70 } 68 71 } 72 73 sub expand_tilde { 74 # Taken from The Perl Cookbook, recipe 7.3 75 my $path = shift; 76 $path =~ s{ ^ ~ ( [^/]* ) } 77 { $1 78 ? (getpwnam($1))[7] 79 : ( $ENV{HOME} || $ENV{LOGDIR} 80 || (getpwuid($>))[7] 81 ) 82 }ex; 83 return $path; 84 } 85 86 sub splitfile { 87 my $path = shift; 88 if ($path =~ m{^(.*/)([^/]*)$}) { 89 return ($1, $2); 90 } else { 91 return ('', $path); 92 } 93 } 94 95 sub complete_file { 96 my $string = shift; 97 98 return ['~/', '~/', 0] if $string eq '~'; 99 100 my $path = abs_path(expand_tilde($string)); 101 my $dir; 102 if ($string =~ m{/$} || $string eq '' || basename($string) eq '.') { 103 $dir = $path; 104 } else { 105 $dir = dirname($path); 106 } 107 return unless -d $dir; 108 109 my ($pfx, $base) = splitfile($string); 110 111 opendir(my $dh, $dir) or return; 112 my @dirs = readdir($dh); 113 close($dh); 114 115 my @out; 116 for my $d (@dirs) { 117 # Skip dotfiles unless explicitly requested 118 if($d =~ m{^[.]} && $base !~ m{^[.]}) { 119 next; 120 } 121 122 my ($text, $value, $done) = ($d, "${pfx}${d}", 1); 123 124 if (-d "$dir/$d") { 125 $text .= "/"; 126 $value .= "/"; 127 $done = 0; 128 } 129 push @out, [$text, $value, $done]; 130 } 131 return @out; 132 } -
t/completion.t
r1167bf1 re6cec01 289 289 \&complete_filter_expr); 290 290 291 # Test complete_files 292 use BarnOwl::Completion::Util qw(complete_file); 293 use File::Temp; 294 use File::Path qw(mkpath); 295 296 my $tmpdir = File::Temp::tempdir(CLEANUP => 1); 297 298 # Make sure $tmpdir does not have a trailing / 299 $tmpdir =~ s{/$}{}; 300 $ENV{HOME} = $tmpdir; 301 302 sub touch { 303 my $path = shift; 304 system("touch", "$path"); 305 } 306 307 mkpath("$tmpdir/.owl/", 308 "$tmpdir/.owl/modules/", 309 "$tmpdir/Public/", 310 "$tmpdir/Private/", 311 "$tmpdir/.ours", 312 "$tmpdir/www"); 313 touch("$tmpdir/.zephyr.subs"); 314 touch("$tmpdir/wheee"); 315 touch("$tmpdir/.owl/startup"); 316 317 sub completion_value { 318 my $c = shift; 319 return $c unless ref($c) eq 'ARRAY'; 320 return $c->[1]; 321 } 322 323 sub test_file { 324 my $spec = shift; 325 my $pfx = shift; 326 my $dirs = shift; 327 my $files = shift; 328 329 my $expect = [ sort {$a->[1] cmp $b->[1]} 330 ((map {["$_/", defined($pfx)?"$pfx/$_/":"$_/", 0]} @$dirs), 331 (map {["$_", defined($pfx)?"$pfx/$_" :$_ , 1]} @$files)) 332 ]; 333 334 local $Test::Builder::Level = $Test::Builder::Level + 1; 335 336 my @got = complete_file($spec); 337 338 @got = grep {completion_value($_) =~ m{^\Q$spec\E}} @got; 339 @got = sort {completion_value($a) cmp completion_value($b)} @got; 340 341 use Data::Dumper; 342 is_deeply(\@got, $expect); 343 } 344 345 is_deeply([complete_file("~")], [["~/", "~/", 0]]); 346 347 chdir($tmpdir); 348 test_file("$tmpdir/", $tmpdir, 349 [qw(Public Private www)], 350 [qw(wheee)]); 351 352 test_file("./", ".", 353 [qw(Public Private www)], 354 [qw(wheee)]); 355 356 test_file("", undef, [qw(Public Private www)], [qw(wheee)]); 357 358 test_file("./.owl/", "./.owl", 359 [qw(modules)], 360 [qw(startup)]); 361 362 test_file("~/", "~", 363 [qw(Public Private www)], 364 [qw(wheee)]); 365 366 test_file("P", undef, [qw(Public Private)], []); 367 368 test_file("$tmpdir/.", $tmpdir, 369 [qw(. .. .owl .ours)], 370 [qw(.zephyr.subs)]); 291 371 1; 292 372
Note: See TracChangeset
for help on using the changeset viewer.