Repository: saulpw/visidata

Issue:
[develop] There should be one CommandLog entry per command executed
**Small description**
As a result of this commit: https://github.com/saulpw/visidata/commit/7b01e98c923b8bca2c297e85cc0f892b92f84b02 Using `exec-longname` results in the command executed having multiple entries in the **CommandLog**.

The problem is understood, but the approach needs to be figured out. We do not want complex commands to have undos missing, and we want only one entry in the **CommandLog** per command.

**Additional context**
The develop branch



Discussion:

Resolving patch:
diff --git a/visidata/cmdlog.py b/visidata/cmdlog.py
--- a/visidata/cmdlog.py
+++ b/visidata/cmdlog.py
@@ -138,6 +138,9 @@ def newRow(self, **fields):
         return self._rowtype(**fields)
 
     def beforeExecHook(self, sheet, cmd, args, keystrokes):
+        if not isLoggableCommand(cmd.longname):
+            return
+
         if vd.activeCommand:
             self.afterExecSheet(sheet, False, '')
 
@@ -411,6 +414,13 @@ def cmdlog(vd):
     vd.beforeExecHooks.append(vs.beforeExecHook)
     return vs
 
+@VisiData.property
+def modifyCommand(vd):
+    if vd.activeCommand is not None and isLoggableCommand(vd.activeCommand.longname):
+        return vd.activeCommand
+    return vd.cmdlog.rows[-1]
+
+
 
 globalCommand('gD', 'cmdlog-all', 'vd.push(vd.cmdlog)', 'open global CommandLog for all commands executed in current session')
 globalCommand('D', 'cmdlog-sheet', 'vd.push(sheet.cmdlog)', "open current sheet's CommandLog with all other loose ends removed; includes commands from parent sheets")
diff --git a/visidata/defermods.py b/visidata/defermods.py
--- a/visidata/defermods.py
+++ b/visidata/defermods.py
@@ -119,7 +119,8 @@ def deleteBy(self, func, commit=False):
         else:
             ndeleted += 1
 
-    vd.addUndo(setattr, self, 'rows', oldrows)
+    if not commit:
+        vd.addUndo(setattr, self, 'rows', oldrows)
 
     vd.status('deleted %s %s' % (ndeleted, self.rowtype))
 
diff --git a/visidata/undo.py b/visidata/undo.py
--- a/visidata/undo.py
+++ b/visidata/undo.py
@@ -11,8 +11,12 @@
 def addUndo(vd, undofunc, *args, **kwargs):
     'On undo of latest command, call undofunc()'
     if options.undo:
-        r = vd.activeCommand
-        if not r:
+        # occurs when VisiData is just starting up
+        if not vd.sheet:
+            return
+        r = vd.modifyCommand
+        # some special commands, like open-file, do not have an undofuncs set
+        if not r or not isinstance(r.undofuncs, list):
             return
         r.undofuncs.append((undofunc, args, kwargs))
 


Test patch:
diff --git a/tests/golden/load-pandas-2.csv b/tests/golden/load-pandas-2.csv
--- a/tests/golden/load-pandas-2.csv
+++ b/tests/golden/load-pandas-2.csv
@@ -5,6 +5,7 @@ Month,Day,Resource,Location,Value,Unit,Source
 ,,,,nan,,
 ,,,,nan,,
 ,,,,nan,,
+,,,,nan,,
 Sep,29,Hospitals,Puerto Rico,34.00,number,ASES
 Oct,1,Electricity,Puerto Rico,5.00,percent,AEE
 Oct,1,Cell antennas,Puerto Rico,300.00,number,FCC
diff --git a/tests/load-pandas-2.vd b/tests/load-pandas-2.vd
--- a/tests/load-pandas-2.vd
+++ b/tests/load-pandas-2.vd
@@ -1,5 +1,4 @@
 sheet	col	row	longname	input	keystrokes	comment
-	override	undo	set-option	True		
 	override	filetype	set-option	pandas		
 			open-file	sample_data/StatusPR.csv	o	
 StatusPR	Month		select-col-regex	sep	|	select rows matching regex in current column
@@ -7,10 +6,8 @@ StatusPR	Location		unselect-col-regex	puerto	\	unselect rows matching regex in c
 StatusPR			delete-selected		gd	delete (cut) selected rows and move them to clipboard
 StatusPR		0	delete-row		d	delete (cut) current row and move it to clipboard
 StatusPR		0	delete-row		d	delete (cut) current row and move it to clipboard
-StatusPR		0	undo-last		U	undo the last action
 StatusPR		0	add-row		a	append a blank row
 StatusPR		1	add-row		a	append a blank row
-StatusPR		0	undo-last		U	undo the last action
 StatusPR		1	add-rows	5	ga	append a blank row
 StatusPR			select-cols-regex	sep	g|	select rows matching regex in any visible column
 StatusPR			unselect-cols-regex	ases	g\	unselect rows matching regex in any visible column