Skip to content

Commit

Permalink
Add ignore_empty_commands setting.
Browse files Browse the repository at this point in the history
Add the `ignore_empty_commands` boolean setting. It defaults to false, which causes an empty command to repeat the last command (the current behavior). If true, an empty command is a no-op.

I want this setting because I frequently press enter a few times in my Rails server terminal (to put a vertical space in the log entries printed to stdout) and then later I try to debug some code in the server. When the debugger line is executed, ByeBug reads those newlines and runs the last command (often continue).

Fixes deivid-rodriguez#700

# Demo

## When the setting is false

```
$ cat ~/.byebugrc
set histfile /home/gabrielx/byebug.history
#set ignore_empty_commands true
$
$ bundle exec ./test.rb

[3, 12] in /home/gabrielx/projects/byebug/test.rb
    3:
    4: require 'byebug'
    5:
    6: debugger
    7:
=>  8: sleep 2.1
    9: debugger
   10:
   11: sleep 2.2
   12: debugger
(byebug) c

[3, 12] in /home/gabrielx/projects/byebug/test.rb
    3:
    4: require 'byebug'
    5:
    6: debugger
    7:
    8: sleep 2.1
    9: debugger
   10:
=> 11: sleep 2.2
   12: debugger
(byebug)

[21, 30] in /home/gabrielx/.rvm/gems/ruby-2.5.3/gems/bundler-2.2.0.rc.2/lib/bundler/cli.rb
   21:     }.freeze
   22:
   23:     def self.start(*)
   24:       super
   25:     ensure
=> 26:       Bundler::SharedHelpers.print_major_deprecations!
   27:     end
   28:
   29:     def self.dispatch(*)
   30:       super do |i|
(byebug)
$
```

### When the setting is true
```
$ cat ~/.byebugrc
set histfile /home/gabrielx/byebug.history
set ignore_empty_commands true
$
$ bundle exec ./test.rb

[3, 12] in /home/gabrielx/projects/byebug/test.rb
    3:
    4: require 'byebug'
    5:
    6: debugger
    7:
=>  8: sleep 2.1
    9: debugger
   10:
   11: sleep 2.2
   12: debugger
(byebug) c

[3, 12] in /home/gabrielx/projects/byebug/test.rb
    3:
    4: require 'byebug'
    5:
    6: debugger
    7:
    8: sleep 2.1
    9: debugger
   10:
=> 11: sleep 2.2
   12: debugger
(byebug)
(byebug)
(byebug) c

[21, 30] in /home/gabrielx/.rvm/gems/ruby-2.5.3/gems/bundler-2.2.0.rc.2/lib/bundler/cli.rb
   21:     }.freeze
   22:
   23:     def self.start(*)
   24:       super
   25:     ensure
=> 26:       Bundler::SharedHelpers.print_major_deprecations!
   27:     end
   28:
   29:     def self.dispatch(*)
   30:       super do |i|
(byebug)
(byebug)
(byebug) c
$
```
  • Loading branch information
gabrieldeal committed Dec 12, 2020
1 parent ffe2acb commit c7122b3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ Command | Aliases | Subcommands
`quit!` | `q!` |
`restart` | |
`save` | `sa` |
`set` | | `autoirb` `autolist` `autopry` `autosave` `basename` `callstyle` `fullpath` `histfile` `histsize` `linetrace` `listsize` `post_mortem` `savefile` `stack_on_error` `width`
`show` | | `autoirb` `autolist` `autopry` `autosave` `basename` `callstyle` `fullpath` `histfile` `histsize` `linetrace` `listsize` `post_mortem` `savefile` `stack_on_error` `width`
`set` | | `autoirb` `autolist` `autopry` `autosave` `basename` `callstyle` `fullpath` `ignore_empty_commands` `histfile` `histsize` `linetrace` `listsize` `post_mortem` `savefile` `stack_on_error` `width`
`show` | | `autoirb` `autolist` `autopry` `autosave` `basename` `callstyle` `fullpath` `ignore_empty_commands` `histfile` `histsize` `linetrace` `listsize` `post_mortem` `savefile` `stack_on_error` `width`
`skip` | `sk` |
`source` | `so` |
`step` | `s` |
Expand Down
2 changes: 2 additions & 0 deletions lib/byebug/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def prepare_input(prompt)
line = readline(prompt)
return unless line

return '' if line.empty? && Setting[:ignore_empty_commands]

last_if_empty(line)
end

Expand Down
17 changes: 17 additions & 0 deletions lib/byebug/settings/ignore_empty_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative "../setting"

module Byebug
#
# Setting to control what Byebug does when the user enters an empty
# command (presses enter without a command).
#
class IgnoreEmptyCommandsSetting < Setting
DEFAULT = false

def banner
"Enable/disable running the last command upon empty commands"
end
end
end
2 changes: 1 addition & 1 deletion test/commands/set_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def program
end

settings =
%i[autolist autosave basename fullpath post_mortem stack_on_error]
%i[autolist autosave basename fullpath ignore_empty_commands post_mortem stack_on_error]

settings.each do |set|
["on", "1", "true", ""].each do |key|
Expand Down
8 changes: 8 additions & 0 deletions test/processors/command_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def test_empty_command_repeats_last_command
debug_code(program) { assert_equal 6, frame.line }
end

def test_empty_command_does_not_repeat_last_command_when_ignoring_empty_commands
with_setting :ignore_empty_commands, true do
enter "n", ""

debug_code(program) { assert_equal 5, frame.line }
end
end

def test_multiple_commands_are_executed_sequentially
enter "n ; n"

Expand Down

0 comments on commit c7122b3

Please sign in to comment.