./src/_compilation/gsubip in politics-1.0.82 vs ./src/_compilation/gsubip 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 != 3 +def main + size = ARGV.size + + if size != 3 Funcs.argument_data("gsubip", "[gsubip] [arguments]", "Global Substitute (Gsub) In Place\n\ngsubip is like gsub, which replaces all instances of a regular expression globally\n\n[regular expression 1] [regular expression 2] => result\n\nExample\n\ngsubip . FOO file\n\nWould make all characters in “file” become “FOO”\n\ngsubip cat dog file\n\nWould make all instances of “cat” “dog”\n\nIt is much cleaner than “sed.”\n\nYou don't need to have “sed” installed to run this program.\n\nIt doesn't read from the standard input\n\nIt takes exactly 3 arguments, no more, no fewer.\n\nSee also\n\ngsub") exit 1 end - gsubip(ARGV[0], ARGV[1], ARGV[2]) + begin + gsubip(ARGV[0]?, ARGV[1]?, ARGV[2]?) + rescue e : Exception + puts e + end end main