Skip to content

Instantly share code, notes, and snippets.

@michaelfward
Last active August 29, 2015 14:18
Show Gist options
  • Save michaelfward/00ed4a01b56b3f6b8889 to your computer and use it in GitHub Desktop.
Save michaelfward/00ed4a01b56b3f6b8889 to your computer and use it in GitHub Desktop.
ruby script to adjust lines a through b (i made it for when i copy and paste large chunks of code but don't want to fix my indentations. keep a uniform pattern throughout, and voila)
#programmed by michael ward
#h3xc0ntr0l@gmail.com
def adjustline(line, off)
str = ""
1.upto(off) {|x| str << " "}
str << line
str
end
def getfile_withlines(fd,a,b,off)
num = 0
nl = []
fd.each_line do |line|
num += 1
if num >= a && num <= b
line = adjustline(line, off)
nl.push(line)
else
nl.push(line)
end
end
nl
end
def write(content, path)
fdd = File.open(path, "w")
content.each{|str| fdd.write(str)}
end
def usage
puts "movefile.rb file startline endline offset"
exit
end
usage unless ARGV.length == 4
path, startline, endline, offset = ARGV[0..3]
fd = File.open(path, "r")
nw = getfile_withlines(fd, startline.to_i, endline.to_i, offset.to_i)
write(nw, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment