0

With the method get_terminal_size from the shutil module one can easily determine the width of the terminal at runtime.

The following script works fine when started directly in a terminal emulator like gnome-terminal or Terminator:

from shutil import get_terminal_size
from time import sleep

if __name__ == "__main__":
    print("start")
    for i in range(5):
        print(f"term size is {get_terminal_size().columns}")
        sleep(.5)
    print("finished")

When changing the window size while the script is running, the output looks like this

start
term size is 112
term size is 102
term size is 102
term size is 113
term size is 115
finished

However, when executed in IntelliJ by clicking the run button, method get_terminal_size always returns the fallback value of 80:

start
term size is 80
term size is 80
term size is 80
term size is 80
term size is 80
finished

Starting the script from the terminal included in IntelliJ also works correctly.

Is there some way to get the terminal size in IntelliJ's output console by some other means? Or is it possible to enable terminal emulation to make get_terminal_size work? It seems, at least for PyCharm a corresponding setting exists or existed according to one SO answer.

Lydia van Dyke
  • 2,466
  • 3
  • 13
  • 25

1 Answers1

1

When you run the program via Python script run configuration - IDE does not launch the Terminal. It runs the program in the embedded Console window which is not technically the OS Terminal.

Andrey
  • 15,144
  • 25
  • 91
  • 187