Sha256: 4257e04b687ef85bf547083f0de0c08750ec44b163d485068b01d68100625ec0

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

#!/usr/bin/env ruby

require 'pathname'
require 'tins/xt'
require 'fileutils'
include FileUtils::Verbose
require 'utils'
require 'term/ansicolor'
include Term::ANSIColor

$config = Utils::ConfigFile.new
$config.configure_from_paths

def local_path(path)
  Pathname.new(path).expand_path.relative_path_from(
    Pathname.new(?.).expand_path
  ).to_s
end

def diff_dir
  `diff -rq #{$src.inspect} #{$dst.inspect}`
end

def ask?(prompt:)
  print prompt
  gets.chomp
end

$src = ARGV.shift or fail "need source directory as first argument"
$src = local_path $src
$dst = ARGV.shift || ?.
$dst = local_path $dst

puts diff_dir

diff_dir.each_line do |line|
  case line
  when /^Only in (.*?): (.*?)$/
    s, d = $~.captures
    sd = local_path(s)
    s  = local_path(File.join(sd, d))
    $config.sync_dir.skip?(s) and next
    dd = [ $src, $dst ].find { _1 != sd }
    d  = local_path(File.join(dd, d))
    if File.file?(s) and File.ascii?(s)
      puts color(214, File.read(s))
    end
    puts " Only found in #{s.inspect} ".center(Tins::Terminal.cols, ?*)
    puts <<~end
      (C)opy #{s.inspect} to #{d.inspect}
      (E)dit #{s.inspect}
      (D)elete #{s.inspect}
      (S)kip
    end
    case ask?(prompt: 'action? ')
    when /\Ac/i
      cp s, d
    when /\Ae/i
      system "vim #{s.inspect}"
      redo
    when /\Ad/i
      rm s
    end
  when /^Files (.*?) and (.*?) differ$/
    s, d = $~.captures
    s = local_path(s)
    $config.sync_dir.skip?(s) and next
    d = local_path(d)
    puts `diff --color=always -u #{s.inspect} #{d.inspect}`
    puts " Difference found #{s.inspect} #{d.inspect} ".center(Tins::Terminal.cols, ?*)
    puts <<~end
      (V)imdiff
      (S)kip
    end
    ask?(prompt: 'action? ') =~ /\Av/i or next
    system "vimdiff #{s.inspect} #{d.inspect}"
    redo
  end
rescue Interrupt
  exit
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
utils-0.43.0 bin/sync_dir
utils-0.42.0 bin/sync_dir
utils-0.41.0 bin/sync_dir
utils-0.40.0 bin/sync_dir