Skip to content

Instantly share code, notes, and snippets.

@sinisterchipmunk
Created April 18, 2011 09:57
Show Gist options
  • Save sinisterchipmunk/925094 to your computer and use it in GitHub Desktop.
Save sinisterchipmunk/925094 to your computer and use it in GitHub Desktop.
Very simple script to display a specified line in a file, plus the line immediately preceding and following it. I whipped this up as a quick solution to manually following a particularly vague stack trace from Jax (github.com/sinisterchipmunk/jax).
#!/usr/bin/env ruby
def fail(msg)
puts msg
puts
exit
end
fail "Usage: line [filename] [lineno]" if !ARGV[0] || !ARGV[1] || !File.file?(ARGV[0])
lines = File.read(ARGV[0]).lines.to_a
lineno = ARGV[1].to_i - 1
fail "There are only #{lines.length} lines in the file" if lineno >= lines.length
print " "
puts(lineno <= 0 ? "<bof>" : lines[lineno-1])
print "> "
puts lines[lineno]
print " "
puts(lineno >= lines.length-1 ? "<eof>" : lines[lineno+1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment