mixmastamyk / console

Comprehensive utility library for terminals. “Better… Stronger… Faster.”
GNU Lesser General Public License v3.0
34 stars 5 forks source link

Unresponsive in VS Code Terminal #3

Closed devsys closed 4 years ago

devsys commented 4 years ago

Greetings,

I am using the terminal in Visual Studio Code on Ubuntu 18.04.1 LTS. I have the following code:

from console import fg, bg, fx

print(fg.navyblue + 'Hello World!' + fg.default)
print(fg.red, fx.italic, '♥ Heart', fx.end, ' python…', sep='')
print(fg.yellow('Woot!'))  # --> '\x1b[33mWoot!\x1b[39m'

When I run this inside VS Code, it seems to freeze and go into a loop until Ctl+C is hit, and I get this output:

$ python3 consoleTest.py 
Traceback (most recent call last):
  File "consoleTest.py", line 1, in <module>
    from console import fg, bg, fx
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/__init__.py", line 44, in <module>
    _CHOSEN_PALETTE = _choose_palette()
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 95, in choose_palette
    result, pal = detect_palette_support(basic_palette=pal)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 199, in detect_palette_support
    name, pal_name, basic_palette = _find_basic_palette(name)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 307, in _find_basic_palette
    if get_color('index', 2)[0][:2] == '85':
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 497, in get_color
    colors = _get_color_xterm(name, number)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 415, in _get_color_xterm
    resp = _read_until(maxchars=26, end=(BEL, ST)).rstrip(ESC)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 390, in _read_until
    char = read(1)
KeyboardInterrupt

Seems like it's blowing up doing some detection or something. I plan to try this test under VS Code on Windows with WSL to see if it has the same result. I'm guessing MS is doing something weird as usual...

The same code works fine if I pop open a regular terminal outside VS Code:

$ python3 consoleTest.py 
Hello World!
♥ Heart python…
Woot!
mixmastamyk commented 4 years ago

Thanks, had a feeling that might be a problem at some point.

It does some querying when the terminal tells it that it is an xterm or compatible. However some of them lie and don't fully support it. Must be another example.

Try this environment variable to shut it off temporarily: PY_CONSOLE_AUTODETECT=0 and I'll look into what is causing it on VSC shortly, and/or how to skip it there.

devsys commented 4 years ago

Yep, that fixed the hanging!

I get the following output with the env var set... there are no colors in the output, but it runs and isn't broken, so I'd consider that a win!

$ python3 consoleTest.py 
Hello World!
♥ Heart python…
Woot!

Thanks!

mixmastamyk commented 4 years ago

I'll download the editor to see what it reports. If you turn on DEBUG logging before console you can see what it says, if you are interested in the investigation as well:

https://mixmastamyk.bitbucket.io/console/additional.html#troubleshooting

devsys commented 4 years ago

No, I'm using bash in the Visual Studio Code terminal. I'm running VS Code under Linux, so I suspect this is different than if you try the same thing in Windows which would likely use all the WSL stuff.

I turned on debug logging in my test script (Trace started when I gave it the Ctl+c):

$ python3 consoleTest.py 
  DEBUG   detection/choose_palette:88 console version: 0.96
  DEBUG   detection/color_is_forced:149 None (CLICOLOR_FORCE=None)
  DEBUG   detection/is_a_tty:348 True
  DEBUG   detection/color_is_disabled:125 None (NO_COLOR=None, CLICOLOR=None)
  DEBUG   detection/is_a_tty:348 True
Traceback (most recent call last):
  File "consoleTest.py", line 8, in <module>
    from console import fg, bg, fx
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/__init__.py", line 44, in <module>
    _CHOSEN_PALETTE = _choose_palette()
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 95, in choose_palette
    result, pal = detect_palette_support(basic_palette=pal)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 199, in detect_palette_support
    name, pal_name, basic_palette = _find_basic_palette(name)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 307, in _find_basic_palette
    if get_color('index', 2)[0][:2] == '85':
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 497, in get_color
    colors = _get_color_xterm(name, number)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 415, in _get_color_xterm
    resp = _read_until(maxchars=26, end=(BEL, ST)).rstrip(ESC)
  File "/home/nate/python-dev/sshMan/.venv/lib/python3.6/site-packages/console/detection.py", line 390, in _read_until
    char = read(1)
KeyboardInterrupt
devsys commented 4 years ago

Hah yeah it's weirdly wonderful that I can do this and honestly it works great.

mixmastamyk commented 4 years ago

Ok, I turned that off for vscode on posix and and am thinking about deactivating entirely. Published a new version of console: 0.97a2

Please give it a try and let me know if it fixes the problem. Probably need to use something like: pip3 install -U --pre console

mixmastamyk commented 4 years ago

I also put in a timeout with select for all read functions, so they should never hang more than a moment.

devsys commented 4 years ago

I updated and it works great now (colors included):

$ python3 consoleTest.py 
  DEBUG   detection/choose_palette:89 console version: 0.97a2
  DEBUG   detection/choose_palette:90 os.name/sys.platform: posix/linux
  DEBUG   detection/color_is_forced:151 None (CLICOLOR_FORCE=None)
  DEBUG   detection/is_a_tty:351 True
  DEBUG   detection/color_is_disabled:127 None (NO_COLOR=None, CLICOLOR=None)
  DEBUG   detection/is_a_tty:351 True
  DEBUG   detection/_find_basic_palette:319 get_color return value failed: tuple index out of range
  DEBUG   detection/detect_palette_support:209 Term support: 'extended' (posix, TERM=xterm-256color, COLORTERM=, ANSICON=, webcolors=False, basic_palette=default (xterm))
  DEBUG   detection/choose_palette:100 Basic palette: ((0, 0, 0), (205, 0, 0), (0, 205, 0), (205, 205, 0), (0, 0, 238), (205, 0, 205), (0, 205, 205), (229, 229, 229), (127, 127, 127), (255, 0, 0), (0, 255, 0), (255, 255, 0), (92, 92, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255))
  DEBUG   detection/choose_palette:101 'extended' is available
Hello World!
♥ Heart python…
Woot!

Thanks!

mixmastamyk commented 4 years ago

Thanks for letting me know.

mixmastamyk commented 4 years ago

I'd like to keep this open a bit, as I'm not ready to push a non-pre release yet, and it will be informative to vscode users.

mixmastamyk commented 4 years ago

Now in .98 as well.