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

Confused on how to implement actions #3081

Closed
Thanatoast opened this issue Nov 6, 2020 · 16 comments
Closed

Confused on how to implement actions #3081

Thanatoast opened this issue Nov 6, 2020 · 16 comments

Comments

@Thanatoast
Copy link

This isn't a bug or a feature request. I was unable to find any kind of support forum or wiki (is there one?) so here goes:
First of all I love kitty. I don't fully understand all it does but I think it will do what I want.
What I am having trouble with right now is actions in hyperlinks.
(https://sw.kovidgoyal.net/kitty/open_actions.html)
I think I understood protocol:///FILE_PATH/FILE#FRAGMENT (took a while to figure out what FRAGMENT was, I didn't see it explained anywhere).

As a simple test I added
protocol filelist
action launch --type=overlay ls

As a test, at the prompt I typed:
echo -e '\e]8;;filelist:///This/Doesnt/Matter\aShow me the files\e]8;;\a'

When I ctrl-shift-click on the "Show me the files" link I get the prompt:
What would you like to do with this URL:
filelist:///This/Doesnt/Matter

I hit O and nothing happens, it just goes back to the prompt.
I realized launch is opening a new window (not what I want anyway) so I changed my action to
action launch --type=overlay ls|less

so I wouldn't miss the output (just to see it work). Now I get:
Failed to launch child: ls|less
With error: No such file or directory
Press Enter to exit.

I tried: action launch --type=overlay ls | less
And now I see nothing again.

I gave up on that (as I said I don't want a new window anyway) and simply tried:
protocol filelist
action ls

And still I get nothing.
What am I doing wrong?
Or is this just not possible?

Thanks!

@kovidgoyal
Copy link
Owner

you want sh -c "ls; read"

@Thanatoast
Copy link
Author

Aha. Thanks. FYI: I had to do: action launch sh -c "ls; read x"
Apparently read (bash builtin?) requires a name, otherwise it returns an error: sh: 1: read: arg count.

Is there a way to send the command to the already running instance of kitty rather than launching a new one? This is what I am really after.

Thanks again.

@kovidgoyal
Copy link
Owner

@Thanatoast
Copy link
Author

That is really cool!
It works great from the command line but I was unable to get it to work from open-actions.conf.
I am obviously missing something but I just can't see what.
Starting kitty each time with:
kitty -o allow_remote_control=yes -o enabled_layouts=tall

I tried open-actions.conf with only the following:

protocol filelist
action send-text --match id:1 "ls"

Then did:
echo -e '\e]8;;filelist:///This/Doesnt/Matter\aShow me the files\e]8;;\a'
ctrl-shift-click on "Show me the files" link and nothing happened.

As a test, I directly entered:
kitty @ send-text --match id:1 "ls"

That worked.

I tried all of the following actions (and a lot of other permutations, restarting kitty each time) with no result:
action send-text --match id:1 "ls;read x"
action send-text "ls"
action send-text "ls;read x"
action kitty @ send-text --match id:1 "ls;read x"
action set-window-title "HYPERLINK WORKED"

In case I messed up the hyperlink somehow, I successfully retried:
action launch sh -c "ls;read x"

I tried:
action change_font_size current -2

And that worked! But I can't find that change_font_size is a command in kitty. Which I suppose should be a clue to me.
From command prompt:
kitty @ change_font_size current +2 <--- change_font_size is not a known command...
change_font_size +2 <--- change_font_size: command not found

Clearly I am too dumb to see what I am missing. :/

@kovidgoyal
Copy link
Owner

If you want to use remote control use

remote_control send-text whatever

https://sw.kovidgoyal.net/kitty/remote-control.html#mapping-key-presses-to-remote-control-commands

or if you want to use it via an actual kitty invocation,
launch kitty with --listen-on=whatever and in the action use

launch kitty @ send-text --to=whatever whatever

@Thanatoast
Copy link
Author

I'm sorry to say that I still do not understand what is required.
open-actions.conf now looks like this:

protocol filelist
action remote_control send-text "ls"

I start kitty with:
kitty -o allow_remote_control=yes -o enabled_layouts=tall

At the kitty command prompt I enter:
echo -e '\e]8;;filelist:///This/Doesnt/Matter\aShow me the files\e]8;;\a'

ctrl-shift-click on "Show me the files" hyperlink, hit O and get:

remote_control mapping failed

'NoneType' object has no attribute 'partition'

I guess that makes sense since you referred me to key mapping. (I don't want to map a key.)

If I understand the second half of your answer. I would be launching a second kitty instance, just to send the original one the text? I gave it a try:
open-actions.conf has an action of:
action launch kitty @ send-text --to=unix:/tmp/mykitty ls
I start kitty with:
kitty -o allow_remote_control=yes -o enabled_layouts=tall --listen-on=unix:/tmp/mykitty
At the kitty command prompt I enter:
echo -e '\e]8;;filelist:///This/Doesnt/Matter\aShow me the files\e]8;;\a'
ctrl-shift-click on the hyperlink. After I hit O I see a kitty instance flicker in and out, but nothing gets sent to the original instance. I made a video and frame-by-frame caught "unknown option: --to"

I just want to send text using a hyperlink action to the instance that is calling it. Just like I can from the command line.
(kitty @ send-text ls)
Thanks again for your effort with this.

@kovidgoyal
Copy link
Owner

If all you want to do is send text to the active window, just use

action send_text all ls

@Thanatoast
Copy link
Author

Yes! That is exactly what I needed. I would never have figured that out.
Thank you again. For your help and for kitty! (and calibre)

@Thanatoast
Copy link
Author

Sorry. Almost. Environment variables aren't expanded doing it that way?

@kovidgoyal
Copy link
Owner

No, environment variables are local to the child process, the kitty
process does not have them, though it could read them from the child
process. But, in general, actions dont have access to the child's
environment. If you control how these URs are being generated, you
should insert needed information into them.

@Thanatoast
Copy link
Author

I do control them but, if I can't use ${FILE_PATH} or ${FRAGMENT} with send_text then how do I get that information from them?

@kovidgoyal
Copy link
Owner

Not sure I follow, why cant you pass FILE_PATH to send_text?
You cant access environment variables, but FILE_PATH should still work,
as far as I know.

@Thanatoast
Copy link
Author

That doesn't seem to be working:
kitty -o allow_remote_control=yes -o enabled_layouts=tall

In kitty:

russ@rlap:~$ cat .config/kitty/open-actions.conf
protocol filelist
action send_text all ${FRAGMENT} $FRAGMENT ${FILE_PATH} $FILE_PATH ${URL} $URL
russ@rlap:~$ echo -e '\e]8;;filelist:///whatever.something#afragment\aShow me the files\e]8;;\a'
Show me the files
russ@rlap:~$ ${FRAGMENT} $FRAGMENT ${FILE_PATH} $FILE_PATH ${URL} $URL

@Thanatoast
Copy link
Author

That made the hyperlinks work. Thank you!
I am having a new problem though.
I originally installed kitty using the pre-built binary. So I cloned and make'd. The only error I got was about wayland being the wrong version.
It works, as I said, however when I do kitty @ I get the following error:

Traceback (most recent call last):
  File "/home/russ/git/kitty/kitty/launcher/../../kitty/rc/base.py", line 220, in all_command_names
    from importlib.resources import contents
ModuleNotFoundError: No module named 'importlib.resources'

During handling of the above exception, another exception occurred:

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 "/home/russ/git/kitty/kitty/launcher/../../__main__.py", line 127, in <module>
    main()
  File "/home/russ/git/kitty/kitty/launcher/../../__main__.py", line 123, in main
    func(sys.argv[1:])
  File "/home/russ/git/kitty/kitty/launcher/../../__main__.py", line 23, in remote_control
    rc_main(args)
  File "/home/russ/git/kitty/kitty/launcher/../../kitty/remote_control.py", line 159, in main
    global_opts, items = parse_rc_args(args)
  File "/home/russ/git/kitty/kitty/launcher/../../kitty/remote_control.py", line 149, in parse_rc_args
    cmap = {name: command_for_name(name) for name in sorted(all_command_names())}
  File "/home/russ/git/kitty/kitty/launcher/../../kitty/rc/base.py", line 222, in all_command_names
    from importlib_resources import contents  # type:ignore
ModuleNotFoundError: No module named 'importlib_resources'

I assume that I made some error in how I installed kitty, but I have no idea what I did wrong.
Any ideas?

A minor additional issue is the kitty icon that was on the gui alt-tab menu is gone. If that is because of the wayland error then I'll live with it until there is a new binary.

@kovidgoyal
Copy link
Owner

You need python 3.7 or install the third party importlib_resources
package for python 3.6

@Thanatoast
Copy link
Author

pip install importlib_resources
That did it! It's all good now!

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

No branches or pull requests

2 participants