knqyf263/pet

feature request : paste command to terminal prompt instead of instant execution

roeniss opened this issue · 14 comments

tldr: what about pet exec --stand to edit command before executing?


For example, if I typed pet exec -q=tmux I can see this screen :

> [tmux new session]: tmux new-session -s main
1/99
> tmux

then press enter, and it runs whatever it is.

But sometimes I need to put some variation to my configured command, like :

tmux new-session -s sub

currently, pet has no such feature like position formatting (ex. tmux new-session -s %s) and it looks big deal.

if the command would be pasted to the current terminal prompt instead of instantly executing, it would give the last chance to put some variation.


ps. BTW, is pet project suspended now, or just not enough workers currently? thank you.

Well, maybe add this bash snippet in your bashrc can help you?

It can paste command to termnal prompt instead of instant execution. Works for me.

pet-select () 
{ 
    BUFFER=$(pet search --query "$READLINE_LINE");
    READLINE_LINE=$BUFFER;
    READLINE_POINT=${#BUFFER}
}

bind -x '"\C-x\C-x": pet-select'

I'm currently use zsh, so basically not working.
When I tried to use yours using bash shell, it still not working.
Actually I think I didn't understand what this script do. I sourced that script, execute pet-select, select one predefined command, and try to paste using ctrl+x but nothing happen. maybe you missed my words?

I want to something like this :

  1. select one command from select-view (after execute pet exec)
  2. that command appear in terminal prompt, which I can edit at all, rather than executed by itself.

anyway thanks for comments! are you maintainer of pet?

I'm not a maintainer. I just start using it in a few days.

In this video I double hited ctrl-x, to call the pet-select function. Directly called pet-select is useless... As you have already find out.

c-x.mp4

The $READLINE_LINE variable in bash represent current input buffer, change the value of the buffer will change the command in terminal prompt.

P.S. Sorry for the low-quality video, but my screen recording software is broken (caused by Wayland), sigh...

You can check whether zsh has feature like READLINE_LINE, and you can write a script, and bind a key to trigger it.

Thank you for patiently explaining it. but still not worked:
image

I newly registered pet-select, bind, then type ctrl-x twice. then the command list showed up just like you.

after I entered on one of them (it was ifconfig.me), nothing came out on screen. then I ctrl-x twice, then error appeared.

I think my bash version (3.2) had a fewer function than yours :(

2021-03-18.10.14.32.mov

Thank you. Gonna work now, I‘ll try it today later.

Maybe some of the syntax is not supported? Maybe you can write sth into READLINE_LINE, and check whether it works. If so you can write your own script to support this feature.

My bash version is 5.0.17.
READLINE_LINE feature is not available under bash 4.0... Ref.

I realized this feature is not easy than I thought first... I want sth like "modify command line buffer by function" and honestly i have no idea how it could be without key-binding (I want to call function directly).

So I decided to make a just copy the command:

pet-search () 
{ 
    BUFFER=$(pet search --query "$1");
    echo $BUFFER | pbcopy
}

pbcopy is macOS' copy command. I have to manually paste(\C+v) and edit.... but that's ok, if other ways need key-binding inevitably.


+) Oh, I found the way :

pet-search () 
{ 
    BUFFER=$(pet search --query "$1");
    print -z $BUFFER
}

BTW this print looks like zsh-builtin function:
http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html

...
-z
	Push the arguments onto the editing buffer stack, separated by spaces.
...

with it, I can make what I needed

Congratulations!

IMO, “make what I needed” is the way terminal tools designed to be —cli is so versatile!

(P.S. I don’t like MacBook too ╮( ̄▽ ̄"")╭ but pbcopy is good

I got what I needed but not sure whether this feature came in pet, so not close for now.

For anyone arriving here and using fish, you can achieve the same with

commandline -i (pet search)

...and with a wrapper function you can define a subcommand

# ~/.config/fish/functions/pet.fish
function pet
	if test (count $argv) -ge 1 -a $argv[1] = "alte"
		commandline -i (command pet search $argv[2..-1])
	else
		command pet $argv
	end
end

i called it alte because my typical usecase for this is using <alt-e> to edit the command before executing

You can definitely do this already by using the zsh/bash snippets in the readme that interface with pet search --query !

Then when you call pet through ctrl+s the command won't get executed, only printed to your terminal for you to press Enter.