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

mykitten.py not found in ~/.config/kitty #1712

Closed terinjokes closed 5 years ago

terinjokes commented 5 years ago

My configuration file for Kitty is located in ~/.config/kitty. I've created a mykitten.py in the same directory, but I get an error that the kitten isn't found.

$ kitty --debug-config
kitty 0.14.2 created by Kovid Goyal
Linux Rincon 4.19.47-1-lts #1 SMP Fri May 31 16:15:02 CEST 2019 x86_64
Arch Linux \r (\l)
LSB_VERSION=1.4
DISTRIB_ID=Arch
DISTRIB_RELEASE=rolling
DISTRIB_DESCRIPTION="Arch Linux"
Loaded config files: /home/terin/.config/kitty/kitty.conf
Running under: X11

Config options different from defaults:
allow_remote_control True
background           Color(red=46, green=52, blue=64)
color0               Color(red=59, green=66, blue=82)
color1               Color(red=191, green=97, blue=106)
color10              Color(red=163, green=190, blue=140)
color11              Color(red=235, green=203, blue=139)
color12              Color(red=129, green=161, blue=193)
color13              Color(red=180, green=142, blue=173)
color14              Color(red=143, green=188, blue=187)
color15              Color(red=180, green=142, blue=173)
color2               Color(red=163, green=190, blue=140)
color3               Color(red=235, green=203, blue=139)
color4               Color(red=129, green=161, blue=193)
color5               Color(red=180, green=142, blue=173)
color6               Color(red=136, green=192, blue=208)
color7               Color(red=229, green=233, blue=240)
color8               Color(red=76, green=86, blue=106)
color9               Color(red=191, green=97, blue=106)
cursor               Color(red=129, green=161, blue=193)
font_family          Hack
font_size            8.0
foreground           Color(red=216, green=222, blue=233)

$ which -a kitty
/usr/bin/kitty

$ cat ~/.config/kitty/mykitten.py 
def main(args):
   # 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, answer, target_window_id, boss):
   # 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)

$ kitty +kitten
You must specify the name of a kitten to run
Choose from:

unicode_input
resize_window
diff
panel
key_demo
ask
clipboard
choose
ssh
icat
show_error
hints

$ kitty +kitten mykitten
No kitten named mykitten

I'm not sure what I'm doing wrong.

kovidgoyal commented 5 years ago

As is noted at https://sw.kovidgoyal.net/kitty/kittens/custom.html

you need to use

map ctrl+k kitten mykitten.py arg1 arg2

i.e. the kitten is meant to be launched from within kitty using a mapping, not as a standalone program. If you wish to create a standalone python script to run with kitty you can use

kitty +launch myscript.py

terinjokes commented 5 years ago

@kovidgoyal Is it possible to be a kitten invokeable by a binding and from the kitten subcommand?

kovidgoyal commented 5 years ago

There is not much point in invoking a kitten via +kitten -- kittens are meant to integrate with kitty, which they cannot if they are run as independent processes. As I said you can run your kitten using kitty +launch directly as a normal python script, but one where you can import anything from the kitty package.

terinjokes commented 5 years ago

I'm really confused now. How are kittens like icat, diff, ssh, and clipboard different than ones I might write myself?

kovidgoyal commented 5 years ago

They are not different, I just happened to overload the kittens system to write standalone programs (gives them a name and an integration point in the documentation and a way to run them easily without needing to deal with paths).

You can do the same thing, just use +launch instead of +kitten.

In fact you can run the builtin kittens that way as well, for example:

kitty +launch /path/to/ssh/kitten/main.py

terinjokes commented 5 years ago

That doesn't seem to do what I expect.

$ kitty +launch ~/.config/kitty/mykitten.py 

$ kitty +launch /usr/lib/kitty/kittens/unicode_input/main.py    
Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/bin/../lib/kitty/__main__.py", line 117, in <module>
    main()
  File "/usr/bin/../lib/kitty/__main__.py", line 108, in main
    namespaced(['+', first_arg[1:]] + sys.argv[2:])
  File "/usr/bin/../lib/kitty/__main__.py", line 68, in namespaced
    func(args[1:])
  File "/usr/bin/../lib/kitty/__main__.py", line 51, in launch
    runpy.run_path(exe, run_name='__main__')
  File "/usr/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/lib/kitty/kittens/unicode_input/main.py", line 21, in <module>
    from ..tui.line_edit import LineEdit
ImportError: attempted relative import with no known parent package

Otherwise, I think I understand the disconnect. I was expecting behavior similar to the builtin programs. I'll get back to writing my kitten now.

kovidgoyal commented 5 years ago

You have to add

if __name__ == '__main__':
    main(sys.argv)

to the bottom of your kitten.

kovidgoyal commented 5 years ago

And just to reduce confusion for anyone else in the future, I have made it possible to run custom kittens via +kitten.

terinjokes commented 5 years ago

@kovidgoyal Thanks. Knowing that (previously) those were overloaded onto +kitten explains where I was being confused.

Sorry if I didn't explain that as well in the initial post.