Repository: saulpw/visidata

Issue:
[input] screen flickers in edit-cell
**Small description**
If I do `edit-cell` with a guide on screen in the sidebar, and then hit `Tab`, the cursor moves to the correct cell. But the cursor and screen flicker like they are being frequently redrawn. The flicker is mainly in the sidebar and the current cell.

**Actual result with screenshot**
 I can upload a terminal recording, if this isn't easy to reproduce on different machines.

`vd -N sample_data/sample.tsv`, then `edit-cell` and hit `Tab`.

**Additional context**
The behavior is different in gnome-terminal vs alacritty. For gnome-terminal, the flicker shows up after tabbing to a different cell.
For alacritty, I don't have to hit Tab, the flicker shows up as soon as I run `edit-cell`.

For both gnome-terminal and alacritty, bisect shows the flicker started with b859610102dc570573f2db8d7e24a13f9a12397b.

saul.pw/VisiData v3.1dev
Python 3.10.12
Ubuntu 22.04.3
gnome-terminal 3.44.0 using VTE 0.68.0 +BIDI +GNUTLS +ICU +SYSTEMD
alacritty 0.13.1


Discussion:
If this helps, I extracted a list of the important drawing calls to try to trace what was going on in `editText()`. But I ran out of steam.
```
editText():
  v = vd.editline(vd.activeSheet._scr, y, x, w, display=display, **kwargs)
  editline():
    disp_help = HelpCycler(scr, '')
    while True:
      disp_help.draw()
        vd.drawInputHelp(scr, '')
          curhelp = vd.getHelpPane('input', module='visidata')
          sheet.drawSidebarText(scr, curhelp, title='Input Keystrokes Help')
            sidebarscr = vd.subwindow(scr, x, y, w, h)   #(call this the A subwindow)
            text.draw(sidebarscr)  #(is actually curhelp.draw(sidebarscr) which is HelpPane.draw())
              #has lots of calls to:
              HelpPane.draw(vs.subwindow(scr))
                  #which makes a new B subwindow in the A subwindow:
                  vd.subwindow(scr of the new subwindow)
                  # and calls erase(), refresh() on its parent the A subwindow
```
Another related effect:  some input prompts, like `go-col-regex` or `go-col-number` leads visidata's CPU usage to go up from 0% to a nonstop 20-30%. Surprisingly, `search-col` does not.

To reproduce:  `vd -N sample_data/sample.tsv`, then `go-col-regex` and don't enter any characters.

This behavior started with the same commit, b859610102dc570573f2db8d7e24a13f9a12397b.

That commit is dated Jan 14 so it's not the cause of #2243, which was reported Jan 9.

Resolving patch:
diff --git a/visidata/mainloop.py b/visidata/mainloop.py
--- a/visidata/mainloop.py
+++ b/visidata/mainloop.py
@@ -168,6 +168,7 @@ def mainloop(vd, scr):
     vd.disp_help = vd.options.disp_help
 
     vd.keystrokes = ''
+    vd.drawThread = threading.current_thread()
     while True:
         if not vd.stackedSheets and vd.currentReplay is None:
             return
@@ -178,7 +179,6 @@ def mainloop(vd, scr):
             continue  # waiting for replay to push sheet
 
         threading.current_thread().sheet = sheet
-        vd.drawThread = threading.current_thread()
 
         vd.setWindows(vd.scrFull)
 
@@ -250,12 +250,15 @@ def mainloop(vd, scr):
         # no idle redraw unless background threads are running
         time.sleep(0)  # yield to other threads which may not have started yet
         if vd._nextCommands:
-            vd.curses_timeout = int(vd.options.replay_wait*1000)
+            if vd.options.replay_wait > 0:
+                vd.curses_timeout = int(vd.options.replay_wait*1000)
+            else:
+                vd.curses_timeout = nonidle_timeout
         elif vd.unfinishedThreads:
             vd.curses_timeout = nonidle_timeout
         else:
             numTimeouts += 1
-            if vd.timeouts_before_idle >= 0 and numTimeouts > vd.timeouts_before_idle:
+            if vd.timeouts_before_idle >= 0 and numTimeouts >= vd.timeouts_before_idle:
                 vd.curses_timeout = -1
             else:
                 vd.curses_timeout = nonidle_timeout


Test patch:
diff --git a/tests/quit-nosave.vdj b/tests/quit-nosave.vdj
new file mode 100644
--- /dev/null
+++ b/tests/quit-nosave.vdj
@@ -0,0 +1,3 @@
+#!vd -p
+{"sheet": "", "col": "", "row": "", "longname": "open-file", "input": "sample_data/2m.tsv", "keystrokes": null, "comment": null}
+{"sheet": "", "col": "", "row": "", "longname": "quit-all", "input": ""}