Permalink
Browse files

move non-git utils to https://github.com/mjdominus/util

  • Loading branch information...
1 parent 399825f commit eef115df2c72cecf7c3938374cf4b1a4a7d2bd00 @mjdominus committed Apr 11, 2013
Showing with 0 additions and 173 deletions.
  1. +0 −86 copy-if-changed
  2. +0 −87 menupick
View
@@ -1,86 +0,0 @@
-#!/usr/bin/perl
-
-use strict;
-
-use Digest::MD5 qw(md5);
-use File::Spec::Functions;
-use File::Basename qw(basename dirname);
-
-use Getopt::Std;
-my %opt = (v => 0, n => 0);
-getopts('vn', \%opt) or usage();
-my $dst = pop @ARGV or usage();
-$opt{v} = 1 if $opt{n};
-
-sub usage {
- print STDERR "usage: copy-if-changed [-vn] src dst
- copy-if-changed [-vn] srcs... dstdir\n";
- exit 2;
-}
-
-if (-d $dst) {
- for my $src (@ARGV) {
- copy_if_changed($src, catfile($dst, basename($src)));
- }
-} elsif (-f $dst || ! -e _) {
- @ARGV == 1 or usage();
- copy_if_changed($ARGV[0], $dst);
-} else {
- die "Destination '$dst' is neither a file nor a directory\n";
-}
-
-use Errno qw(ENOENT !);
-
-sub copy_if_changed {
- my ($src, $dst) = @_;
- my $dh = md5_file($dst,
- sub {
- if ($!{ENOENT}) {
- return "";
- } else {
- warn "Couldn't open file '$dst': $!; skipping\n";
- return;
- }
- });
-
- my $sh = md5_file($src,
- sub { warn "Couldn't open file '$src': $!; skipping\n";
- return;
- })
- or return;
-
- if ($sh eq $dh) {
- warn "$dst is up to date; no change\n" if $opt{v};
- } else {
- warn "$src -> $dst\n" if $opt{v};
- copy_file($src, $dst) unless $opt{n};
- }
-}
-
-use File::Temp qw(tempfile);
-sub copy_file {
- my ($sf, $df) = @_;
- my $dir = dirname($df);
- open my($s), "<". $sf
- or die "Couldn't open file '$sf': $!";
- my ($d, $tf) = tempfile(".tmpfileXXXXX", $dir)
- or die "Couldn't start tempfile in directory '$dir': $!";
- { local $/ = 8192;
- while (my $rec = <$s>) {
- print $d $rec or die "Couldn't write tempfile: $!";
- }
- close $d or die "Couldn't finish tempfile: $!";
- }
- rename $tf => $df
- or die "Couldn't rename tempfile '$tf' in directory '$dir'\n";
- return 1;
-}
-
-sub md5_file {
- my ($file, $fail) = @_;
- my $fh;
- open($fh, "<", $file)
- and return Digest::MD5->new->addfile($fh)->digest;
- return $fail ? $fail->($file) : undef;
-}
-
View
@@ -1,87 +0,0 @@
-#!/usr/bin/perl
-
-use Getopt::Std;
-my $opt = { 1 => 0, n => 30 };
-getopts('1n:', $opt) or usage();
-
-chomp(my @item = <>);
-
-unless (@item) {
- warn "No items supplied to menupick\n";
- exit;
-}
-
-open my($ttyin), "<", "/dev/tty"
- or do { warn "No tty: $!\n"; exit 1 };
-open my($ttyout), ">", "/dev/tty"
- or do { warn "No tty: $!\n"; exit 1 };
-
-if ($opt->{1} && @item == 1) {
- print @item, "\n";
- exit;
-}
-
-if ($opt->{n} > 0 && @item > $opt->{n}) {
- warn sprintf "%d items in menu is too many\n", 0+@item;
- exit 2;
-}
-
-sub show_menu {
- my ($set) = @_;
- my %sh = map { $_ => 1 } @$set;
- for my $i (0 .. $#item) {
- printf $ttyout "%2d. %s %s\n", $i, $sh{$i} ? "*" : " ", $item[$i];
- }
-}
-show_menu();
-
-my @set = ();
-while (1) {
- my $in = prompt($ttyin, $ttyout);
- chomp $in;
- last unless $in =~ /\S/;
- my $quit = $in =~ s/!\s*$//;
- my $show = $in =~ s/\?{2,}\s*$//;
- if ($in =~ /^\s*\?\s*$/) {
- print_set($ttyout, @set);
- next;
- } elsif ($in =~ /^\s*\*\s*$/) {
- @set = (0 .. $#item);
- }
- modify($in, \@set);
- show_menu(\@set) if $show;
- last if $quit;
-}
-
-print join "\n", @item[@set], "";
-exit 0;
-
-sub prompt {
- my ($in, $out) = @_;
- print $out "> ";
- my $txt = <$in>;
- return $txt;
-}
-
-sub modify {
- my ($mod, $set) = @_;
- my %set;
- @set{@$set} = (1) x @$set; # URGH
- #mod =~ tr/,/ /;
- my @items = split /\s+/, $mod;
- for my $item (@items) {
- if ($item =~ /^\d+$/) {
- $set{$item} = 1;
- } elsif ($item =~ /^ (\d+) - (\d+) $/x) {
- for ($1 .. $2) { $set{$_} = 1 }
- } elsif ($item =~ /^ [-!] (\d+) $/x) {
- delete $set{$1};
- }
- }
- @$set = sort { $a <=> $b } keys %set;
-}
-
-sub print_set {
- my ($out) = shift;
- print $out "@_\n";
-}

0 comments on commit eef115d

Please sign in to comment.