added cat --ex option, so cat can display the source of an exception

This commit is contained in:
John Mair 2011-07-27 03:15:36 +12:00
parent 081d66693e
commit b3763f9382
1 changed files with 29 additions and 1 deletions

View File

@ -37,6 +37,7 @@ class Pry
command "cat", "Show output of file FILE. Type `cat --help` for more information." do |*args|
start_line = 0
end_line = -1
file_name = nil
opts = Slop.parse!(args) do |opt|
opt.on :s, :start, "Start line (defaults to start of file)Line 1 is the first line.", true, :as => Integer do |line|
@ -47,6 +48,16 @@ class Pry
end_line = line - 1
end
opt.on :ex, "Show a window of N lines around last exception (defaults to 5).", :optional => true, :as => Integer do |window_size|
window_size = window_size ? window_size : 5
ex = Pry.last_exception
next if !ex
start_line = (ex.line - 1) - window_size
start_line = start_line < 0 ? 0 : start_line
end_line = (ex.line - 1) + window_size
file_name = ex.file
end
opt.on :l, "line-numbers", "Show line numbers."
opt.on :t, :type, "The specific file type for syntax higlighting (e.g ruby, python)", true, :as => Symbol
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
@ -57,7 +68,13 @@ class Pry
next if opts.help?
file_name = args.shift
if opts.ex?
next output.puts "No Exception or Exception has no associated file." if file_name.nil?
next output.puts "Cannot cat exceptions raised in REPL." if Pry.eval_path == file_name
else
file_name = args.shift
end
if !file_name
output.puts "Must provide a file name."
next
@ -65,6 +82,17 @@ class Pry
contents, normalized_start_line, _ = read_between_the_lines(file_name, start_line, end_line)
if opts.ex?
contents = contents.lines.map.with_index do |line, idx|
l = idx + start_line
if l == (Pry.last_exception.line - 1)
"=> #{line}"
else
" #{line}"
end
end.join
end
if Pry.color
contents = syntax_highlight_by_file_type_or_specified(contents, file_name, opts[:type])
end