./src/_compilation/swap in politics-1.0.82 vs ./src/_compilation/swap in politics-1.0.87

- old
+ new

@@ -1,14 +1,27 @@ #!/usr/bin/crystal -def delete(arg) - `find -name #{arg}`.each_line do |i| - File.delete(i) - end +CORE_UTIL_STRING = "core_utils" + +def prepend_each(arg) + STDIN.each_line do |line| + puts arg + line + end end +def append_each(arg) + STDIN.each_line do |line| + puts line + arg + end +end +def delete(arg) + `find -name #{arg}`.each_line do |i| + File.delete(i) + end +end + def nth_word(arg) i = arg.to_u64 - 1 STDIN.each_line do |line| puts line.split[i] rescue "" end @@ -18,15 +31,15 @@ STDIN.each_line do |line| puts line.strip end end -def lines() +def lines(folder = ".") if !STDIN.tty? - puts STDIN.gets_to_end.count("\n") + puts STDIN.gets_to_end.count('\n') else - res = `find .`.count("\n") - 1 + res = `find #{folder}`.count('\n') - 1 puts res end end def args(arg1) @@ -153,29 +166,29 @@ end def gsubip(arg1, arg2, arg3) regex = Regex.new(arg1) text = File.read(arg3).gsub(/#{arg1}/m, arg2) - File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(arg3).permissions) do |h| + File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(arg3).permissions) do |h| h.print text end begin File.rename t, arg3 rescue - rm t + File.delete t end end def rip(arg1, arg2, arg3) text = File.read(arg3).gsub(arg1, arg2) - File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(arg3).permissions) do |h| + File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(arg3).permissions) do |h| h.print text end begin File.rename t, arg3 rescue - rm t + File.delete t end end def gsub(arg1, arg2) print STDIN.gets_to_end.gsub(/#{arg1}/m, arg2) @@ -195,41 +208,41 @@ def swap(file1, file2) [file1, file2].each do |f| File.exists?(f) || abort("No file named #{f.dump}") end - t = File.tempname("coreutils", "_tmp") + t = File.tempname(CORE_UTIL_STRING, "_tmp") File.rename file1, t File.rename file2, file1 File.rename t, file2 end def prepend(file) file_data = File.read(file) new_data = STDIN.gets_to_end - File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(file).permissions) do |h| + File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(file).permissions) do |h| h << new_data + file_data end begin File.rename t, file rescue - rm t + File.delete t end end def append(file) file_data = File.read(file) new_data = STDIN.gets_to_end - File.open(t = File.tempname("coreutils", "_tmp"), "w", File.info(file).permissions) do |h| + File.open(t = File.tempname(CORE_UTIL_STRING, "_tmp"), "w", File.info(file).permissions) do |h| h << file_data + new_data end begin File.rename t, file rescue - rm t + File.delete t end end class Funcs def self.argument_data(program, usage, long_desc) @@ -256,16 +269,22 @@ puts text end end -def main() - if ARGV.size != 2 +def main + size = ARGV.size + + if size != 2 Funcs.argument_data("swap", "[swap] [arguments]", "Swaps two files\n\nExample\n\nswap text1 text2") exit 1 end - swap(ARGV[0], ARGV[1]) + begin + swap(ARGV[0]?, ARGV[1]?) + rescue e : Exception + puts e + end end main