Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1.  
  2.  
  3. Logo
  4.  
  5.  
  6. Passing arguments to kittens
  7. Passing the contents of the screen to the kitten
  8. Using kittens to script kitty, without any terminal UI
  9. Kittens created by kitty users
  10.  
  11. Custom kittens
  12.  
  13. You can easily create your own kittens to extend kitty. They are just terminal programs written in Python. When launching a kitten, kitty will open an overlay window over the current window and optionally pass the contents of the current window/scrollback to the kitten over its STDIN. The kitten can then perform whatever actions it likes, just as a normal terminal program. After execution of the kitten is complete, it has access to the running kitty instance so it can perform arbitrary actions such as closing windows, pasting text, etc.
  14.  
  15. Let's see a simple example of creating a kitten. It will ask the user for some input and paste it into the terminal window.
  16.  
  17. Create a file in the kitty config folder, ~/.config/kitty/mykitten.py (you might need to adjust the path to wherever the kitty config folder is on your machine).
  18.  
  19. def main(args):
  20. # this is the main entry point of the kitten, it will be executed in
  21. # the overlay window when the kitten is launched
  22. answer = input('Enter some text: ')
  23. # whatever this function returns will be available in the
  24. # handle_result() function
  25. return answer
  26.  
  27. def handle_result(args, answer, target_window_id, boss):
  28. # get the kitty window into which to paste answer
  29. w = boss.window_id_map.get(target_window_id)
  30. if w is not None:
  31. w.paste(answer)
  32.  
  33. Now in kitty.conf add the lines:
  34.  
  35. map ctrl+k kitten mykitten.py
  36.  
  37. Start kitty and press ctrl+k and you should see the kitten running. The best way to develop your own kittens is to modify one of the built in kittens. Look in the kittens sub-directory of the kitty source code for those. Or see below for a list of third-party kittens, that other kitty users have created.
  38. Passing arguments to kittens
  39.  
  40. You can pass arguments to kittens by defining them in the map directive in kitty.conf. For example:
  41.  
  42. map ctrl+k kitten mykitten.py arg1 arg2
  43.  
  44. These will be available as the args parameter in the main() and handle_result() functions. Note also that the current working directory of the kitten is set to the working directory of whatever program is running in the active kitty window.
  45. Passing the contents of the screen to the kitten
  46.  
  47. If you would like your kitten to have access to the contents of the screen and/or the scrollback buffer, you just need to add an annotation to the handle_result() function, telling kitty what kind of input your kitten would like. For example:
  48.  
  49. # in main, STDIN is for the kitten process and will contain
  50. # the contents of the screen
  51. def main(args):
  52. return sys.stdin.read()
  53.  
  54. # in handle_result, STDIN is for the kitty process itself, rather
  55. # than the kitten process and should not be read from.
  56. def handle_result(args, stdin_data, target_window_id, boss):
  57. pass
  58.  
  59. handle_result.type_of_input = 'text'
  60.  
  61. This will send the plain text of the active window to the kitten's STDIN. For text with formatting escape codes, use ansi instead. If you want line wrap markers as well, use screen-ansi or just screen. For the scrollback buffer as well, use history, ansi-history or screen-history.
  62. Using kittens to script kitty, without any terminal UI
  63.  
  64. If you would like your kitten to script kitty, without bothering to write a terminal program, you can tell the kittens system to run the handle_result() function without first running the main() function.
  65.  
  66. For example, here is a kitten that "zooms/unzooms" the current terminal window by switching to the stack layout or back to the previous layout.
  67.  
  68. Create a file in the kitty config folder, ~/.config/kitty/zoom_toggle.py
  69.  
  70. def main(args):
  71. pass
  72.  
  73. def handle_result(args, answer, target_window_id, boss):
  74. tab = boss.active_tab
  75. if tab is not None:
  76. if tab.current_layout.name == 'stack':
  77. tab.last_used_layout()
  78. else:
  79. tab.goto_layout('stack')
  80.  
  81. handle_result.no_ui = True
  82.  
  83. Now in kitty.conf add:
  84.  
  85. map f11 kitten zoom_toggle.py
  86.  
  87. Pressing F11 will now act as a zoom toggle function. You can get even more fancy, switching the kitty OS window to fullscreen as well as changing the layout, by simply adding the line:
  88.  
  89. boss.toggle_fullscreen()
  90.  
  91. to the handle_result() function, above.
  92. Kittens created by kitty users
  93.  
  94. vim-kitty-navigator
  95.  
  96. Allows you to navigate seamlessly between vim and kitty splits using a consistent set of hotkeys.
  97. smart-scroll
  98.  
  99. Makes the kitty scroll bindings work in full screen applications
  100. insert password
  101.  
  102. Insert a password from a CLI password manager, taking care to only do it at a password prompt.
  103.  
  104. ©2019, Kovid Goyal.
  105. Fork me on GitHub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement