pet icon indicating copy to clipboard operation
pet copied to clipboard

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

Open roeniss opened this issue 4 years ago • 13 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.

roeniss avatar Jan 09 '21 16:01 roeniss

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'

ofey404 avatar Mar 17 '21 09:03 ofey404

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?

roeniss avatar Mar 17 '21 14:03 roeniss

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.

https://user-images.githubusercontent.com/35857538/111558120-1bd43380-87c9-11eb-90d7-26cc5308228a.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...

ofey404 avatar Mar 18 '21 01:03 ofey404

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

ofey404 avatar Mar 18 '21 01:03 ofey404

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 :(

https://user-images.githubusercontent.com/26613280/111558780-cb61d380-87d2-11eb-99c3-95306b9619bd.mov

roeniss avatar Mar 18 '21 01:03 roeniss

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

ofey404 avatar Mar 18 '21 01:03 ofey404

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.

ofey404 avatar Mar 18 '21 01:03 ofey404

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

ofey404 avatar Mar 18 '21 02:03 ofey404

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

roeniss avatar Mar 18 '21 03:03 roeniss

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

ofey404 avatar Mar 18 '21 10:03 ofey404

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

roeniss avatar Mar 18 '21 15:03 roeniss

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

commandline -i (pet search)

sbaildon avatar Aug 23 '21 14:08 sbaildon

...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

sbaildon avatar Aug 23 '21 14:08 sbaildon

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.

RamiAwar avatar Feb 10 '24 07:02 RamiAwar