Skip to content
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

make a kitten that changes font size based on terminal's width and height #2933

Closed
wis opened this issue Aug 22, 2020 · 13 comments
Closed

make a kitten that changes font size based on terminal's width and height #2933

wis opened this issue Aug 22, 2020 · 13 comments

Comments

@wis
Copy link

wis commented Aug 22, 2020

Is your feature request related to a problem? Please describe.
No.
I want to make a kitten that changes font size automatically now I do it manually every time I open a terminal or change its size.

Describe the solution you'd like
a kitten that changes font-size setting based on terminal size

Describe alternatives you've considered
none that I can think of

is this possible to do with the kitten API?

@kovidgoyal
Copy link
Owner

@wis
Copy link
Author

wis commented Aug 22, 2020

I have
allow_remote_control yes
in my kitty.conf but when I run kitty @ launch --watcher /home/x/.config/kitty/kittens/autofontsize-watcher/main.py in the (kitty) terminal it errors:
Remote control is disabled. Add allow_remote_control to your kitty.conf

@Luflosi
Copy link
Contributor

Luflosi commented Aug 22, 2020

Did you restart kitty after changing the setting?

@wis
Copy link
Author

wis commented Aug 22, 2020

nice! thank you. I did this mistake before, forgetting that configs are read when kitty is started and don't update for already running processes.
now, running this command is starting a new pane in the terminal, I only want the kitten to run in the background of the main terminal.

@kovidgoyal
Copy link
Owner

There's no kitten here, use a session file to run the launch command with the watcher and start your main terminal with --session

@wis
Copy link
Author

wis commented Aug 22, 2020

aha got it (barely).
I just started noticing that kitty doesn't do a relayout on every window size change and things are not looking good because of that, text is getting pixelized when the framebuffer is resized and things are getting cut off.
and I think I found a recursion stack overflow bug (happens about 1 every ~6 times the window is resized):

Traceback (most recent call last):
  File "/usr/bin/../lib/kitty/kitty/window.py", line 637, in call_watchers
    w(boss, self, data)
  File "/home/x/.config/kitty/kittens/autofontsize-watcher/main.py", line 12, in on_resize
    boss.set_font_size(new_font_size)
  File "/usr/bin/../lib/kitty/kitty/boss.py", line 541, in set_font_size
    self.change_font_size(True, None, new_size)
  File "/usr/bin/../lib/kitty/kitty/boss.py", line 576, in change_font_size
    self._change_font_size(final_windows)
  File "/usr/bin/../lib/kitty/kitty/boss.py", line 583, in _change_font_size
    tm.resize()
  File "/usr/bin/../lib/kitty/kitty/tabs.py", line 582, in resize
    tab.relayout()
  File "/usr/bin/../lib/kitty/kitty/tabs.py", line 193, in relayout
    self.current_layout(self.windows)
  File "/usr/bin/../lib/kitty/kitty/layout/base.py", line 310, in __call__
    self.do_layout(all_windows)
  File "/usr/bin/../lib/kitty/kitty/layout/tall.py", line 127, in do_layout
    self.layout_single_window_group(next(all_windows.iter_all_layoutable_groups()))
  File "/usr/bin/../lib/kitty/kitty/layout/base.py", line 323, in layout_single_window_group
    wg.set_geometry(geom)
  File "/usr/bin/../lib/kitty/kitty/window_list.py", line 100, in set_geometry
    w.set_geometry(geom)
  File "/usr/bin/../lib/kitty/kitty/window.py", line 384, in set_geometry
    sg = self.update_position(new_geometry)
  File "/usr/bin/../lib/kitty/kitty/window.py", line 376, in update_position
    self.screen_geometry = sg = calculate_gl_geometry(window_geometry, vw, vh, cw, ch)
  File "/usr/bin/../lib/kitty/kitty/window.py", line 100, in calculate_gl_geometry
    return ScreenGeometry(xstart, ystart, window_geometry.xnum, window_geometry.ynum, dx, dy)
  File "<string>", line 1, in __new__
RecursionError: maximum recursion depth exceeded while calling a Python object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/../lib/kitty/kitty/window.py", line 637, in call_watchers
    w(boss, self, data)
  File "/home/x/.config/kitty/kittens/autofontsize-watcher/main.py", line 12, in on_resize
    boss.set_font_size(new_font_size)
  File "/usr/bin/../lib/kitty/kitty/boss.py", line 541, in set_font_size
    self.change_font_size(True, None, new_size)
  File "/usr/bin/../lib/kitty/kitty/boss.py", line 576, in change_font_size
    self._change_font_size(final_windows)
  File "/usr/bin/../lib/kitty/kitty/boss.py", line 583, in _change_font_size
    tm.resize()
  File "/usr/bin/../lib/kitty/kitty/tabs.py", line 582, in resize
    tab.relayout()
  File "/usr/bin/../lib/kitty/kitty/tabs.py", line 193, in relayout
    self.current_layout(self.windows)
  File "/usr/bin/../lib/kitty/kitty/layout/base.py", line 310, in __call__
    self.do_layout(all_windows)
  File "/usr/bin/../lib/kitty/kitty/layout/tall.py", line 127, in do_layout
    self.layout_single_window_group(next(all_windows.iter_all_layoutable_groups()))
  File "/usr/bin/../lib/kitty/kitty/layout/base.py", line 323, in layout_single_window_group
    wg.set_geometry(geom)
  File "/usr/bin/../lib/kitty/kitty/window_list.py", line 100, in set_geometry
    w.set_geometry(geom)
  File "/usr/bin/../lib/kitty/kitty/window.py", line 386, in set_geometry
    self.call_watchers(self.watchers.on_resize, {'old_geometry': self.geometry, 'new_geometry': new_geometry})
  File "/usr/bin/../lib/kitty/kitty/window.py", line 639, in call_watchers
    import traceback
  File "/usr/lib/python3.8/traceback.py", line 5, in <module>
    import linecache
  File "/usr/lib/python3.8/linecache.py", line 11, in <module>
    import tokenize
  File "/usr/lib/python3.8/tokenize.py", line 45, in <module>
    class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')):
  File "/usr/lib/python3.8/collections/__init__.py", line 394, in namedtuple
    exec(s, namespace)
RecursionError: maximum recursion depth exceeded during compilation

because I am sure the watcher is not the issue:
~/.config/kitty/kittens/autofontsize-watcher/main.py:

def on_resize(boss, window, data):
    # configure mul, fmax and fmin for your monitor size and liking
    mul, fmax, fmin = 0.01, 72, 10
    width = data['new_geometry'].right
    print("new width", width)
    new_font_size = round(width*mul)
    if new_font_size < fmin:
        new_font_size = fmin
    elif new_font_size > fmax:
        new_font_size = fmax
    print("new font size", new_font_size)
    boss.set_font_size(new_font_size)

add to ~/.config/kitty/kitty.conf:
startup_session ~/.config/kitty/sessions/default
~/.config/kitty/sessions/default:
launch kitty @ launch --watcher ~/.config/kitty/kittens/autofontsize-watcher/main.py

also is there a way to configure kitty not to do these optimizations that makes things look bad, i.e to always do a relayout on every window size change?

@wis
Copy link
Author

wis commented Aug 23, 2020

I just noticed using a session causes the custom shortcuts I defined in my kitty.conf to stop working, when I comment startup_session ~/.config/kitty/sessions/default out they start working again. why?

@kovidgoyal
Copy link
Owner

You want

launch --watcher

not launch kitty @ launch

And your watcher is the issue. When you change font sizes in a resize event, you are causing the window to resize as well, hence the recursion. So instead of directly calling set_font_size() in boss, run a subprocess that uses kitty @ set-font-size instead, which will fix your recursion.

@wis
Copy link
Author

wis commented Aug 23, 2020

that's what I tried first, when ~/.config/kitty/sessions/default's content is launch --watcher ~/.config/kitty/kittens/autofontsize-watcher/main.py kitty errors on launch:

Failed to launch child: --watcher
With error: No such file or directory
Press Enter to exit.

is there an issue with the session parser? that it's trying to run --watcher (which is not a file)?

@kovidgoyal
Copy link
Owner

Oh yeah, I forgot the session file does not currently support the full launch syntax, its on my TODO list.

@kovidgoyal
Copy link
Owner

db64aef should take care of the recursion

@kovidgoyal
Copy link
Owner

And I just committed code to allow specifying watchers on the command line when launching kitty so no need to use either session files or remote control for this.

@rickalex21
Copy link

Hello,

I'm looking to set the font for two monitors. My LG I want to increase the font and my other monitor
leave it with the default font. The problems are:

  1. I can't resize with ctrl+shift+= when I use the watcher.
  2. When I open on the big monitor and I close it then open on the small monitor it will open
    in big font. However, when I close it and open another window it will open in the normal font as usual.

This is my watcher.py. If the width is bigger than 3800 increase the font.

def on_resize(boss, window, data):
    width = data['new_geometry'].right
    lg  = 3800
    if width > lg:
        boss.set_font_size(50)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants