-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cmd2 loses lines of history on Windows when use_rawinput=True
#1331
Comments
I'm on Windows 10 and I've tested with the built-in Powershell (5.1.19041.5007) and I installed Powershell 7 (7.4.5). Can you run |
Thanks for the reply! I was using I'm managing my environment with I also tried uninstalling and reinstalling the latest version of You mentioned you're using Windows 10, and I'm not sure if you're working with the new Windows Terminal or the older Command Prompt. If you're using the older Command Prompt without any issues, this might be a Windows 11 or Windows Terminal-specific issue. |
After some debugging, I think this issue is causes by the usage of package DebuggingFirst of all, this issue only occurred when Lines 3231 to 3246 in 3062aaa
At first I suspected the issue is somehow caused by Then I started to check this input line: Line 3239 in 3062aaa
I used the debugger to step into the execution of this
The Full code of hook_wrapper_23def hook_wrapper_23(stdin, stdout, prompt):
"""Wrap a Python readline so it behaves like GNU readline."""
try:
# call the Python hook
res = ensure_str(readline_hook(prompt)) # <----------------- here
# make sure it returned the right sort of thing
if res and not isinstance(res, bytes):
raise TypeError("readline must return a string.")
except KeyboardInterrupt:
# GNU readline returns 0 on keyboard interrupt
return 0
except EOFError:
# It returns an empty string on EOF
res = ensure_str("")
except BaseException:
print("Readline internal error", file=sys.stderr)
traceback.print_exc()
res = ensure_str("\n")
# we have to make a copy because the caller expects to free the result
n = len(res)
p = Console.PyMem_Malloc(n + 1)
_strncpy(cast(p, c_char_p), res, n + 1)
return p The hooks above triggered Then we reached def readline_setup(self, prompt=""):
BaseReadline.readline_setup(self, prompt)
self._print_prompt()
self._update_line() It seems that the def _update_line(self):
c = self.console
# ...
if (y >= h - 1) or (n > 0):
c.scroll_window(-1) # <--------
c.scroll((0, 0, w, h), 0, -1) # <--------
n += 1 Here two scroll-related functions has been called. Then I checked the def scroll(self, rect, dx, dy, attr=None, fill=" "):
"""Scroll a rectangle."""
if attr is None:
attr = self.attr
x0, y0, x1, y1 = rect
source = SMALL_RECT(x0, y0, x1 - 1, y1 - 1)
dest = self.fixcoord(x0 + dx, y0 + dy)
style = CHAR_INFO()
style.Char.AsciiChar = ensure_str(fill[0])
style.Attributes = attr
return self.ScrollConsoleScreenBufferW( # <------- Here
self.hout, byref(source), byref(source), dest, byref(style)
) As the docstring suggests, I looked into the console function used in The documentation describes the behavior of this API as follows:
I believe this API is causing the issue. Additionally, the PSReadLine GitHub PR seems also removed the usage of
WorkaroundsAfter having all the info above, I tried to replace the # pyreadline3/rlmain.py
class Readline(BaseReadline):
# ...
def _update_line()
# ...
if (y >= h - 1) or (n > 0):
# replace scroll and scroll_window with a single console.write('\n')
c.write('\n')
# c.scroll_window(-1)
# c.scroll((0, 0, w, h), 0, -1)
n += 1 Also, the comment in
If we just let the program ignore this (change the if condition # pyreadline3/rlmain.py
class Readline(BaseReadline):
# ...
def _update_line(self):
c = self.console
# ...
x, y = c.pos() # Preserve one line for Asian IME(Input Method Editor) statusbar
w, h = c.size()
# if (y >= h - 1) or (n > 0):
if (y >= h) or (n > 0):
# after changing the condition, the demo python program
# will never reach inside this if block, thus no more
# scrolling issue.
c.scroll_window(-1)
c.scroll((0, 0, w, h), 0, -1)
n += 1 These are two possible workarounds for my specific case. However, I am currently unable to find the approach to resolve this issue. |
One possibility we might be able to explore would be to detect if the cmd2 application is running in My understanding is that the new @kmvanbrunt Based on all of the data provided by @nfnfgo do you have any smart ideas? |
Not using We should just document the limitations of running cmd2 on Windows due to current bugs in Some that come to mind are:
|
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 whenuse_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:cmd2_on_win_terminal.mp4
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
This issue disappeared once I change the code above into:
After some simple investigation, it seems that this issue is related to either readline, Windows Powershell or Windows Terminal, following are some relevant links:
microsoft/terminal#10975 (comment)
PowerShell/PSReadLine#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.
The text was updated successfully, but these errors were encountered: