#!/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 answer = 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}" end rescue Interrupt exit end