require'byebug/command'moduleByebug## List parts of the source code.#classListCommand<Commanddefregexp/^\s* l(?:ist)? (?:\s*([-=])|\s+(\S+))? \s*$/xenddefexecuteexist=File.exist?(@state.file)returnerrmsg"No sourcefile available for #{@state.file}\n"unlessexist@match||=match('list')max_lines=n_lines(@state.file)b,e=range(@match[2],max_lines)returnerrmsg('Invalid line range')unlessvalid_range?(b,e,max_lines)display_lines(b,e)@state.prev_line=bendclass<<selfdefnames%w(list)enddefdescriptionprettify<<-EOD
l[ist][[-=]][ nn-mm]
Lists lines of code forward from current line or from the place where
code was last listed. If "list-" is specified, lists backwards
instead. If "list=" is specified, lists from current line regardless
of where code was last listed. A line range can also be specified to
list specific sections of code.
EODendendprivate## Line range to be printed by `list`.## If <input> is set, range is parsed from it.## Otherwise it's automatically chosen.#defrange(input,max_line)size=[Setting[:listsize],max_line].minreturnset_range(size,max_line)unlessinputparse_range(input,size,max_line)enddefvalid_range?(first,last,max)first<=last&&(1..max).include?(first)&&(1..max).include?(last)end## Set line range to be printed by list## @param size - number of lines to be printed# @param max_line - max line number that can be printed## @return first line number to list# @return last line number to list#defset_range(size,max_line)first=amend(lower(size,@match[1]||'+'),max_line-size+1)[first,move(first,size-1)]enddefparse_range(input,size,max_line)first,err=get_int(input.split(/[-,]/)[0],'List',1,max_line)return[-1,-1]iferrifinput.split(/[-,]/)[1]last,_=get_int(input.split(/[-,]/)[1],'List',1,max_line)return[-1,-1]unlesslastlast=amend(last,max_line)elsefirst-=(size/2)end[first,last||move(first,size-1)]enddefamend(line,max_line)return1ifline<1[max_line,line].minenddeflower(size,direction='+')return@state.line-size/2ifdirection=='='||!@state.prev_linemove(@state.prev_line,size,direction)enddefmove(line,size,direction='+')line.send(direction,size)end## Show lines in @state.file from line number <min> to line number <max>.#defdisplay_lines(min,max)puts"\n[#{min}, #{max}] in #{@state.file}"File.foreach(@state.file).with_indexdo|line,lineno|returniflineno+1>maxnextunless(min..max).include?(lineno+1)mark=lineno+1==@state.line?'=> ':' 'putsformat("#{mark}%#{max.to_s.size}d: %s",lineno+1,line)endendendend