Skip to content

Commit

Permalink
Fix command history traversal
Browse files Browse the repository at this point in the history
Up arrow in command textbox showed first command from history file.
Down arrow showed 3rd command.
Do not cycle commands and do not select them on send and traversal.
  • Loading branch information
volconst committed Aug 17, 2020
1 parent f02f4ef commit 9d5620f
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions printrun/pronterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,12 @@ def sash_position_changed(event):
if self.fgcode:
self.start_viz_thread()
self.ui_ready = True
self.settings.monitor=temp_monitor
self.commandbox.history=read_history_from(self.history_file)
self.commandbox.histindex == len(self.commandbox.history)
self.settings.monitor = temp_monitor
self.commandbox.history = read_history_from(self.history_file)
self.commandbox.histindex = len(self.commandbox.history)
self.Thaw()
if self.settings.monitor:
self.update_monitor()
self.update_monitor()

def on_resize(self, event):
wx.CallAfter(self.on_resize_real)
Expand Down Expand Up @@ -529,21 +529,28 @@ def rescanports(self, event = None):
elif portslist:
self.serialport.SetValue(portslist[0])

def appendCommandHistory(self):
cmd = self.commandbox.Value
hist = self.commandbox.history
append = cmd and (not hist or hist[-1] != cmd)
if append:
self.commandbox.history.append(cmd)
return append

def cbkey(self, e):
if e.GetKeyCode() == wx.WXK_UP:
dir = {wx.WXK_UP: -1, wx.WXK_DOWN: 1}.get(e.KeyCode)
if dir:
if self.commandbox.histindex == len(self.commandbox.history):
self.commandbox.history.append(self.commandbox.GetValue()) # save current command
if len(self.commandbox.history):
self.commandbox.histindex = (self.commandbox.histindex - 1) % len(self.commandbox.history)
self.commandbox.SetValue(self.commandbox.history[self.commandbox.histindex])
self.commandbox.SetSelection(0, len(self.commandbox.history[self.commandbox.histindex]))
elif e.GetKeyCode() == wx.WXK_DOWN:
if self.commandbox.histindex == len(self.commandbox.history):
self.commandbox.history.append(self.commandbox.GetValue()) # save current command
if len(self.commandbox.history):
self.commandbox.histindex = (self.commandbox.histindex + 1) % len(self.commandbox.history)
self.commandbox.SetValue(self.commandbox.history[self.commandbox.histindex])
self.commandbox.SetSelection(0, len(self.commandbox.history[self.commandbox.histindex]))
if dir == 1:
# do not cycle top => bottom
return
#save unsent command before going back
self.appendCommandHistory()
self.commandbox.histindex = max(0, min(self.commandbox.histindex + dir, len(self.commandbox.history)))
self.commandbox.Value = (self.commandbox.history[self.commandbox.histindex]
if self.commandbox.histindex < len(self.commandbox.history)
else '')
self.commandbox.SetInsertionPointEnd()
else:
e.Skip()

Expand Down Expand Up @@ -733,15 +740,15 @@ def set_autoscrolldisable(self,e):
self.autoscrolldisable = e.IsChecked()

def sendline(self, e):
command = self.commandbox.GetValue()
command = self.commandbox.Value
if not len(command):
return
logging.info(">>> " + command)
line = self.precmd(str(command))
self.onecmd(line)
self.commandbox.SetSelection(0, len(command))
self.commandbox.history.append(command)
self.appendCommandHistory()
self.commandbox.histindex = len(self.commandbox.history)
self.commandbox.Value = ''

# --------------------------------------------------------------
# Main menu handling & actions
Expand Down

0 comments on commit 9d5620f

Please sign in to comment.