kovidgoyal / kitty

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

Python errors on example kitten scripts… #3167

Closed hedefalk closed 3 years ago

hedefalk commented 3 years ago

Sorry to post as bug as this most likely is just a stupid question. I'm trying to get the functionality of this example to toggle fullscreen:

https://sw.kovidgoyal.net/kitty/kittens/custom.html#using-kittens-to-script-kitty-without-any-terminal-ui

So I've created the file

✗ cat ~/.config/kitty/zoom_toggle.py 
from kittens.tui.loop import debug

def main(args):
    pass

from kittens.tui.handler import result_handler
@result_handler(no_ui=True)
def handle_result(args, answer, target_window_id, boss):
    debug('zoom toggle!')
    tab = boss.active_tab
    if tab is not None:
        if tab.current_layout.name == 'stack':
            tab.last_used_layout()
        else:
            tab.goto_layout('stack')⏎   

and set a shortcut:

map cmd+shift+enter kitten zoom_toggle.py

Running that gets me nothing.

I've tried the even simpler example:

✗ cat ~/.config/kitty/mykitten.py 
def main(args: List[str]) -> str:
    # this is the main entry point of the kitten, it will be executed in
    # the overlay window when the kitten is launched
    answer = input('Enter some text: ')
    # whatever this function returns will be available in the
    # handle_result() function
    return answer

def handle_result(args: List[str], answer: str, target_window_id: int, boss: kitty.boss.Boss) -> None:
    # get the kitty window into which to paste answer
    w = boss.window_id_map.get(target_window_id)
    if w is not None:
        w.paste(answer)⏎ 

And running it manually I get python errors

❯ kitty +kitten mykitten.py
Traceback (most recent call last):
  File "lib/python3.8/runpy.py", line 282, in run_path
  File "lib/python3.8/runpy.py", line 87, in _run_code
  File "__main__.py", line 127, in <module>
  File "__main__.py", line 118, in main
  File "__main__.py", line 75, in namespaced
  File "__main__.py", line 65, in run_kitten
  File "kittens/runner.py", line 117, in run_kitten
  File "lib/python3.8/runpy.py", line 265, in run_path
  File "lib/python3.8/runpy.py", line 97, in _run_module_code
  File "lib/python3.8/runpy.py", line 87, in _run_code
  File "/Users/viktor/.config/kitty/mykitten.py", line 1, in <module>
    def main(args: List[str]) -> str:
NameError: name 'List' is not defined
✗ kitty --debug-config
kitty 0.19.2 created by Kovid Goyal
Darwin polina 20.1.0 Darwin Kernel Version 20.1.0: Sat Oct 31 00:07:11 PDT 2020; root:xnu-7195.50.7~2/RELEASE_X86_64 x86_64
ProductName:    macOS ProductVersion:   11.0.1 BuildVersion:    20B50
Loaded config files: /Users/viktor/.config/kitty/kitty.conf

Config options different from defaults:
allow_remote_control y
editor               /usr/local/bin/code
enabled_layouts      ['splits:split_axis=horizontal']
font_family          JetBrains Mono
font_size            13.0
open_url_modifiers   8
shell                /usr/local/bin/fish --login
tab_bar_edge         1
tab_bar_style        separator
url_prefixes         ('http', 'https', 'file', 'ftp', '/')
Added shortcuts:
    f11 KeyAction(func='kitten', args=['zoom_toggle.py'])
    control+right KeyAction(func='neighboring_window', args=['right'])
    control+left KeyAction(func='neighboring_window', args=['left'])
    super+d KeyAction(func='launch', args=['--location=vsplit'])
    shift+super+enter KeyAction(func='kitten', args=['zoom_toggle.py'])
Changed shortcuts:
    shift+control+p > n KeyAction(func='kitten', args=['hints', '--type linenum --linenum-action=background /usr/local/bin/code -g {path}:{line}'])
    shift+control+right KeyAction(func='resize_window', args=['wider', 1])
    shift+control+left KeyAction(func='resize_window', args=['narrower', 1])
    shift+control+down KeyAction(func='resize_window', args=['shorter', 3])
    shift+control+up KeyAction(func='resize_window', args=['taller', 1])
    super+1 KeyAction(func='goto_tab', args=(1,))
    super+2 KeyAction(func='goto_tab', args=(2,))
    super+3 KeyAction(func='goto_tab', args=(3,))
    super+4 KeyAction(func='goto_tab', args=(4,))
    super+5 KeyAction(func='goto_tab', args=(5,))
    super+6 KeyAction(func='goto_tab', args=(6,))
    super+7 KeyAction(func='goto_tab', args=(7,))
    super+8 KeyAction(func='goto_tab', args=(8,))
    super+9 KeyAction(func='goto_tab', args=(9,))
    super+t KeyAction(func='new_tab_with_cwd', args=())
    super+w KeyAction(func='close_window', args=())
    shift+super+d KeyAction(func='launch', args=['--location=hsplit'])

Couldn't find any docs on Python requirements but maybe I'm just not looking in the right place?

kovidgoyal commented 3 years ago

Whether it does anything or not will depend on whether you have more than one kitty window and what layout you are in. As for the other one you need to add

from typing import List

at the top. In future, please use the new discussions for kitty instead of issues for this kind of thing. https://github.com/kovidgoyal/kitty/discussions