Skip to content

Commit

Permalink
Use deque with maxsize for command line history
Browse files Browse the repository at this point in the history
A deque handles the size limit internally, eliminating some logic in the
debugger itself.
  • Loading branch information
raphCode committed Apr 25, 2023
1 parent 2b86211 commit be01f05
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions pudb/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from itertools import count
from functools import partial
from collections import deque
from types import TracebackType

from pudb.lowlevel import decode_lines, ui_log
Expand Down Expand Up @@ -850,20 +851,19 @@ def clear_cmdline_history(btn):
del self.cmdline_contents[:]

def initialize_cmdline_history(path):

dq = partial(deque, maxlen=5000)
try:
# Load global history if present
with open(path, "r") as histfile:
return histfile.read().splitlines()
return dq(histfile.read().splitlines())
except FileNotFoundError:
return []
return dq()

self.cmdline_history_path = os.path.join(get_save_config_path(),
"internal-cmdline-history.txt")

self.cmdline_history = initialize_cmdline_history(self.cmdline_history_path)
self.cmdline_history_position = -1
self.cmdline_history_limit = 5000

self.cmdline_edit_bar = urwid.Columns([
self.cmdline_edit_sigwrap,
Expand Down Expand Up @@ -1843,9 +1843,6 @@ def cmdline_exec(w, size, key):

if not self.cmdline_history or cmd != self.cmdline_history[-1]:
self.cmdline_history.append(cmd)
# Limit history size
if len(self.cmdline_history) > self.cmdline_history_limit:
del self.cmdline_history[0]

self.cmdline_history_position = -1

Expand Down

0 comments on commit be01f05

Please sign in to comment.