kovidgoyal / kitty

Cross-platform, fast, feature-rich, GPU based terminal
https://sw.kovidgoyal.net/kitty/
GNU General Public License v3.0
24.15k stars 972 forks source link

Implement escape sequence to query terminal size in pixels ('\e[14t'). #581

Closed dylanaraps closed 6 years ago

dylanaraps commented 6 years ago

This escape sequence outputs the current terminal's window size in pixels. The output appears as user input in the prompt and requires a read command to store it’s output.

This is supported in all VTE based terminals, XTerm, URxvt, iTerm2 and many others so it makes sense for Kitty to support it as well.

Neofetch uses this escape sequence to correctly size and place the image inside of the terminal window (Neofetch has fallbacks for when this sequence isn’t supported but they depend on an X server).

Neofetch recently added support for kitty icat and this works when an X server is running (thanks to the fallbacks above). However it fails on macOS as the escape sequence is not supported.

There is a way of using osascript on macOS to query the window size but this requires the user to accept a dialog that appears on first run. Kitty supporting the sequence directly would make this feature transparent for users.

Cheers :+1:

kovidgoyal commented 6 years ago

There is no need for this. kitty has a far superior mechanism to query window size, that is synchronous and much more efficient. See https://github.com/kovidgoyal/kitty/blob/master/graphics-protocol.asciidoc And this sis upported in other terminals as well, such as xterm. it should be a two line patch to neofetch to use this method of detecting screen size.

kovidgoyal commented 6 years ago

Oh and just for completeness, if you dont want to implement the method, you can also use:

kitty +runpy "from kitty.utils import *; s = screen_size_function()(); print('x'.join(map(str, s[2:4])))"
kovidgoyal commented 6 years ago

And to make it even easier: https://github.com/kovidgoyal/kitty/commit/20611c7c15aede0f991114256b230a34e99b4c5f

Note that this will actually work in any terminal that implements TIOCGWINSZ correctly, not just kitty.

dylanaraps commented 6 years ago

Thanks for this! 👍

On Mon, 28 May 2018 at 2:43 pm, Kovid Goyal notifications@github.com wrote:

And to make it even easier: 20611c7 https://github.com/kovidgoyal/kitty/commit/20611c7c15aede0f991114256b230a34e99b4c5f

Note that this will actually work in any terminal that implements TIOCGWINSZ correctly, not just kitty.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kovidgoyal/kitty/issues/581#issuecomment-392417607, or mute the thread https://github.com/notifications/unsubscribe-auth/AGfAawtGPblvB7EZ69LIVJCaGBp8J12jks5t24BggaJpZM4UPfJ7 .

kovidgoyal commented 6 years ago

Note I renamed the option to --print-window-size

dylanaraps commented 6 years ago

--print-window-size and the +runpy commands don't work from a bash sub-shell. The error is the same for both commands.

Example code:

IFS=x read -r term_width term_height < <(kitty icat --print-window-size)

Error output:

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/bin/../lib/kitty/__main__.py", line 76, in <module>
    main()
  File "/usr/bin/../lib/kitty/__main__.py", line 72, in main
    func(sys.argv[1:])
  File "/usr/bin/../lib/kitty/__main__.py", line 10, in icat
    main(args)
  File "/usr/bin/../lib/kitty/kittens/icat/main.py", line 256, in main
    print('{}x{}'.format(screen_size().width, screen_size().height))
  File "/usr/bin/../lib/kitty/kitty/utils.py", line 94, in screen_size
    fcntl.ioctl(fd, termios.TIOCGWINSZ, buf)
OSError: [Errno 25] Inappropriate ioctl for device
kovidgoyal commented 6 years ago

https://github.com/kovidgoyal/kitty/commit/8809b8db5681e862b35e122bfda5353033725b7b

dylanaraps commented 6 years ago

Thanks :+1: