cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

Cmd2 loses lines of history on Windows when `use_rawinput=True`

Open nfnfgo opened this issue 1 year ago • 6 comments

Version Info

Here is my version info.

  • python: 3.12.4
  • cmd2: 2.4.3
  • windows: Windows 11
  • powershell: 7.4.5

Issue

I'm using cmd2 to create an interative CLI on Windows. And as the title implies, It seems there is terminal output issue when use_rawinput=True.

Concretely, if the output lines count of cmd2 application is over the vertical size of the Terminal on Windows, the overflowed line will directly disappeared from the terminal console history. A video is attached below to demostrate the issue:

The word "history" in title means the history of the content being displayed on terminal, not the history of input commands.

https://github.com/user-attachments/assets/1b3fc70f-1468-476a-86fc-c85b5ec516cd

The video is using Terminal application on Windows, but the integrated terminal in VSCode could also reproduce this issue on my computer. The code used is also attached below:

Code used in the video
import sys
import os

from typing import Optional, List, Iterable

import cmd2
from cmd2 import Settable, Statement, utils
from cmd2 import (
    Cmd2ArgumentParser,
    with_argparser,
    with_argument_list,
    with_default_category,
)


class ExampleCmd2Application(cmd2.Cmd):
    def __init__(self):
        self.use_rawinput = True
        super().__init__(startup_script=".sudokurc", silence_startup_script=True)
        self.count = 0

    def do_iter(self, *args, **kwargs):
        self.poutput(self.count)
        self.count += 1


def main():
    app = ExampleCmd2Application()
    app.cmdloop()


if __name__ == "__main__":
    main()

This issue disappeared once I change the code above into:

    def __init__(self):
-       self.use_rawinput = True
+       self.use_rawinput = False
        super().__init__(startup_script=".sudokurc", silence_startup_script=True)
        self.count = 0

After some simple investigation, it seems that this issue is related to either readline, Windows Powershell or Windows Terminal, following are some relevant links:

https://github.com/microsoft/terminal/issues/10975#issuecomment-901480065 https://github.com/PowerShell/PSReadLine/issues/724

Based on the search result and the fact that I failed to reproduce this issue in GitHub Codespace in Linux environments, I assumed this is a platform-specific issue which only exists on Windows.

nfnfgo avatar Oct 16 '24 17:10 nfnfgo