zsh-users
 help / color / mirror / Atom feed
* save a command in history from within the widget
@ 2020-10-24 19:00 Ahmad Ismail
  2020-10-25 20:24 ` Daniel Shahaf
  2020-10-29 11:07 ` Lewis Butler
  0 siblings, 2 replies; 10+ messages in thread
From: Ahmad Ismail @ 2020-10-24 19:00 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1381 bytes --]

Hi All,

I am currently working on a widget. It is used so that I can use my git
alias also for my dotfiles.

The widget is given bellow:

function _check-if-dotfile-command {

if [[ $BUFFER =~ ^"g d untrack-all" ]]
then
zle .kill-whole-line
BUFFER="g d rm --cached -r ~"
zle .accept-line
elif [[ $BUFFER =~ ^"g d add-and-commit" ]]
then
BUFFERz=$(echo $BUFFER | cut -d '"' -f2)
zle .kill-whole-line
BUFFER="git d add ~ && g d commit -am \"${BUFFERz}\""
zle .accept-line
elif [[ $BUFFER =~ ^"g d sb" ]]
then
zle .kill-whole-line
BUFFER="git d branch | rofi -dmenu | xargs git checkout"
zle .accept-line
elif [[ $BUFFER =~ ^"g d stash-and-reset" ]]
then
zle .kill-whole-line
BUFFER="git d stash && git d reset --hard HEAD"
zle .accept-line
elif [[ $BUFFER =~ ^"g d last" ]]
then
zle .kill-whole-line
BUFFER="git d --no-pager log -1 --oneline"
zle .accept-line
else
zle .accept-line
fi
}

zle -N accept-line _check-if-dotfile-command

Now, the problem is, the commands I am invoking are not being saved in the
history file.
For example, if I use:

% g d add-and-commit "Few Modifications"

it saves:

: 1603565685:0;git d add ~ && g d commit -am "Few Modifications"

I know the command below is the command I actually ran. But I want to save
the command I used as well. Is there any way I can save a command in
history from within the widget?

*Thanks and Best Regards,Ahmad Ismail*

[-- Attachment #2: Type: text/html, Size: 9022 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-24 19:00 save a command in history from within the widget Ahmad Ismail
@ 2020-10-25 20:24 ` Daniel Shahaf
  2020-10-25 21:13   ` Ahmad Ismail
  2020-10-26  7:11   ` Roman Perepelitsa
  2020-10-29 11:07 ` Lewis Butler
  1 sibling, 2 replies; 10+ messages in thread
From: Daniel Shahaf @ 2020-10-25 20:24 UTC (permalink / raw)
  To: Ahmad Ismail; +Cc: Zsh Users

Ahmad Ismail wrote on Sun, 25 Oct 2020 01:00 +0600:
> I am currently working on a widget. It is used so that I can use my git
> alias also for my dotfiles.
> 
> The widget is given bellow:
> Now, the problem is, the commands I am invoking are not being saved in the
> history file.
> For example, if I use:
> 
> % g d add-and-commit "Few Modifications"
> 
> it saves:
> 
> : 1603565685:0;git d add ~ && g d commit -am "Few Modifications"
> 
> I know the command below is the command I actually ran. But I want to save
> the command I used as well. Is there any way I can save a command in
> history from within the widget?

I don't understand your use-case.  You stated your purpose is to use
your «g» alias in your dotfiles, but then you gave an example from
interactive use, and in any case dotfiles don't run widgets.

In any case, I'd scratch the approach of checking [[ $BUFFER =~ '^g d foo' ]],
because it'll break as soon as you want to interactively use complex
commands (e.g., sublists, lists, conditions, loops).

I don't understand why you can't drop a script literally called «g»
into a directory in $path and implement in it whatever logic you want.
(Or possibly a function, depending on your use-case.)

As to your widget code, you don't need kill-whole-line if you set
BUFFER explicitly.  Also, you check BUFFER without first checking
PREBUFFER, which is subtly wrong because $PREBUFFER may end with a line
continuation.

Cheers,

Daniel


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-25 20:24 ` Daniel Shahaf
@ 2020-10-25 21:13   ` Ahmad Ismail
  2020-10-26 22:04     ` Daniel Shahaf
  2020-10-26  7:11   ` Roman Perepelitsa
  1 sibling, 1 reply; 10+ messages in thread
From: Ahmad Ismail @ 2020-10-25 21:13 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 3264 bytes --]

Hi Daniel Shahaf,

Thank you very much for responding. To get some context to my use case you
can check:

https://www.reddit.com/r/git/comments/jdl11c/can_not_use_some_of_my_aliases_with_bare_repo/
https://stackoverflow.com/questions/64416625/can-not-use-some-of-my-aliases-with-bare-repo

Long story short, as far I know, there is no easy way to use some of my
aliases with bare repo. There are some solutions like special git alias
like:

example1 = "!f() { .....; }; f"

However, I am feeling much comfortable with this approach. Here, I am
trying to change my command on the fly by running it before accept-line.
The widget is working perfectly for me except, it is not saving the
original buffer in the history.

I need a command or widget like *save-to-history *so that I can use it the
following way:

if [[ $BUFFER =~ ^"g d untrack-all" ]]
then
*zle save-to-history $BUFFER*
BUFFER="g d rm --cached -r ~"
zle .accept-line
....

That way I can save the value of the $BUFFER in the .zsh_history file in
proper format.

And yes you are right when you said:

I'd scratch the approach of checking [[ $BUFFER =~ '^g d foo' ]],
> because it'll break as soon as you want to interactively use complex
> commands (e.g., sublists, lists, conditions, loops).
>

But, this script is only for my personal use, so I will continue tweaking
it as per my requirement.

Being said that, I actually do not know how to check PREBUFFER before
checking BUFFER. I will be glad if you can give me some code to fix this
issue.

*Thanks and Best Regards,Ahmad Ismail*


On Mon, Oct 26, 2020 at 2:24 AM Daniel Shahaf <d.s@daniel.shahaf.name>
wrote:

> Ahmad Ismail wrote on Sun, 25 Oct 2020 01:00 +0600:
> > I am currently working on a widget. It is used so that I can use my git
> > alias also for my dotfiles.
> >
> > The widget is given bellow:
> ⋮
> > Now, the problem is, the commands I am invoking are not being saved in
> the
> > history file.
> > For example, if I use:
> >
> > % g d add-and-commit "Few Modifications"
> >
> > it saves:
> >
> > : 1603565685:0;git d add ~ && g d commit -am "Few Modifications"
> >
> > I know the command below is the command I actually ran. But I want to
> save
> > the command I used as well. Is there any way I can save a command in
> > history from within the widget?
>
> I don't understand your use-case.  You stated your purpose is to use
> your «g» alias in your dotfiles, but then you gave an example from
> interactive use, and in any case dotfiles don't run widgets.
>
> In any case, I'd scratch the approach of checking [[ $BUFFER =~ '^g d foo'
> ]],
> because it'll break as soon as you want to interactively use complex
> commands (e.g., sublists, lists, conditions, loops).
>
> I don't understand why you can't drop a script literally called «g»
> into a directory in $path and implement in it whatever logic you want.
> (Or possibly a function, depending on your use-case.)
>
> As to your widget code, you don't need kill-whole-line if you set
> BUFFER explicitly.  Also, you check BUFFER without first checking
> PREBUFFER, which is subtly wrong because $PREBUFFER may end with a line
> continuation.
>
> Cheers,
>
> Daniel
>

[-- Attachment #2: Type: text/html, Size: 6370 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-25 20:24 ` Daniel Shahaf
  2020-10-25 21:13   ` Ahmad Ismail
@ 2020-10-26  7:11   ` Roman Perepelitsa
  2020-10-26 11:15     ` Ahmad Ismail
  1 sibling, 1 reply; 10+ messages in thread
From: Roman Perepelitsa @ 2020-10-26  7:11 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Ahmad Ismail, Zsh Users

On Sun, Oct 25, 2020 at 9:24 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> I don't understand why you can't drop a script literally called «g»
> into a directory in $path and implement in it whatever logic you want.
> (Or possibly a function, depending on your use-case.)

I highly recommend following this advice. Write a script or a function
called "g" and remove the custom accept-line widget.

> But, this script is only for my personal use, so I will continue
> tweaking it as per my requirement.

The value of the advice is not diminished by "g" being for your personal use.

Roman.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-26  7:11   ` Roman Perepelitsa
@ 2020-10-26 11:15     ` Ahmad Ismail
  2020-10-26 14:19       ` Ahmad Ismail
  0 siblings, 1 reply; 10+ messages in thread
From: Ahmad Ismail @ 2020-10-26 11:15 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Daniel Shahaf, Zsh Users

[-- Attachment #1: Type: text/plain, Size: 830 bytes --]

Thank you very much all. Now I understand your point.

*Best Regards,Ahmad Ismail*


On Mon, Oct 26, 2020 at 1:11 PM Roman Perepelitsa <
roman.perepelitsa@gmail.com> wrote:

> On Sun, Oct 25, 2020 at 9:24 PM Daniel Shahaf <d.s@daniel.shahaf.name>
> wrote:
> >
> > I don't understand why you can't drop a script literally called «g»
> > into a directory in $path and implement in it whatever logic you want.
> > (Or possibly a function, depending on your use-case.)
>
> I highly recommend following this advice. Write a script or a function
> called "g" and remove the custom accept-line widget.
>
> > But, this script is only for my personal use, so I will continue
> > tweaking it as per my requirement.
>
> The value of the advice is not diminished by "g" being for your personal
> use.
>
> Roman.
>

[-- Attachment #2: Type: text/html, Size: 2015 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-26 11:15     ` Ahmad Ismail
@ 2020-10-26 14:19       ` Ahmad Ismail
  0 siblings, 0 replies; 10+ messages in thread
From: Ahmad Ismail @ 2020-10-26 14:19 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Daniel Shahaf, Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1475 bytes --]

I have taken your suggestion and written the following function

if  [[ $@ =~ ^"d untrack-all" ]]
then
    g d rm --cached -r ~
elif [[ $@ =~ ^"d add-and-commit" ]]
then
    g d add ~ && g d commit -am \"$_\"
elif [[ $@ =~ ^"d sb" ]]
then
    g d branch | rofi -dmenu | xargs git checkout
elif [[ $@ =~ ^"d stash-and-reset" ]]
then
    g d stash && git d reset --hard HEAD
elif [[ $@ =~ ^"d last" ]]
then
    g d --no-pager log -1 --oneline
else
    zsh -c "git $@"
fi

*Thanks and Best Regards,Ahmad Ismail*


On Mon, Oct 26, 2020 at 5:15 PM Ahmad Ismail <ismail783@gmail.com> wrote:

> Thank you very much all. Now I understand your point.
>
> *Best Regards,Ahmad Ismail*
>
>
> On Mon, Oct 26, 2020 at 1:11 PM Roman Perepelitsa <
> roman.perepelitsa@gmail.com> wrote:
>
>> On Sun, Oct 25, 2020 at 9:24 PM Daniel Shahaf <d.s@daniel.shahaf.name>
>> wrote:
>> >
>> > I don't understand why you can't drop a script literally called «g»
>> > into a directory in $path and implement in it whatever logic you want.
>> > (Or possibly a function, depending on your use-case.)
>>
>> I highly recommend following this advice. Write a script or a function
>> called "g" and remove the custom accept-line widget.
>>
>> > But, this script is only for my personal use, so I will continue
>> > tweaking it as per my requirement.
>>
>> The value of the advice is not diminished by "g" being for your personal
>> use.
>>
>> Roman.
>>
>

[-- Attachment #2: Type: text/html, Size: 3783 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-25 21:13   ` Ahmad Ismail
@ 2020-10-26 22:04     ` Daniel Shahaf
  2020-10-27 19:01       ` Ahmad Ismail
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Shahaf @ 2020-10-26 22:04 UTC (permalink / raw)
  To: Ahmad Ismail; +Cc: Zsh Users

Ahmad Ismail wrote on Mon, 26 Oct 2020 03:13 +0600:
> Thank you very much for responding. To get some context to my use case you
> can check:
> 
> https://www.reddit.com/r/git/comments/jdl11c/can_not_use_some_of_my_aliases_with_bare_repo/
> https://stackoverflow.com/questions/64416625/can-not-use-some-of-my-aliases-with-bare-repo

For future reference, when asking a question, it's considered good
netiquette to (a) state the use-case explicitly, (b) cross-link to
other copies of the same question that have been (previously or
concurrently) asked elsewhere.

Consider writing a 'd' command that sets GIT_WORK_TREE and GIT_DIR in
the environment and executes git.

See also vcsh(1).

Daniel


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-26 22:04     ` Daniel Shahaf
@ 2020-10-27 19:01       ` Ahmad Ismail
  0 siblings, 0 replies; 10+ messages in thread
From: Ahmad Ismail @ 2020-10-27 19:01 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1123 bytes --]

Thank you very much all. I will remember to:

(a) state the use-case explicitly,
(b) cross-link to other copies of the same question that have been
(previously or concurrently) asked elsewhere.

And I will In-Shah-Allah set the GIT_WORK_TREE and GIT_DIR variables now.

*Thanks and Best Regards,Ahmad Ismail*


On Tue, Oct 27, 2020 at 4:04 AM Daniel Shahaf <d.s@daniel.shahaf.name>
wrote:

> Ahmad Ismail wrote on Mon, 26 Oct 2020 03:13 +0600:
> > Thank you very much for responding. To get some context to my use case
> you
> > can check:
> >
> >
> https://www.reddit.com/r/git/comments/jdl11c/can_not_use_some_of_my_aliases_with_bare_repo/
> >
> https://stackoverflow.com/questions/64416625/can-not-use-some-of-my-aliases-with-bare-repo
>
> For future reference, when asking a question, it's considered good
> netiquette to (a) state the use-case explicitly, (b) cross-link to
> other copies of the same question that have been (previously or
> concurrently) asked elsewhere.
>
> Consider writing a 'd' command that sets GIT_WORK_TREE and GIT_DIR in
> the environment and executes git.
>
> See also vcsh(1).
>
> Daniel
>

[-- Attachment #2: Type: text/html, Size: 2605 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-24 19:00 save a command in history from within the widget Ahmad Ismail
  2020-10-25 20:24 ` Daniel Shahaf
@ 2020-10-29 11:07 ` Lewis Butler
  2020-10-29 23:54   ` Ahmad Ismail
  1 sibling, 1 reply; 10+ messages in thread
From: Lewis Butler @ 2020-10-29 11:07 UTC (permalink / raw)
  To: Zsh Users

On 24 Oct 2020, at 13:00, Ahmad Ismail <ismail783@gmail.com> wrote:
> I am currently working on a widget. It is used so that I can use my git alias also for my dotfiles.

This is, to me, very advanced zsh and while reading through it I am still not exactly sure what it does or what the purpose is.

> The widget is given bellow:
> 
> function _check-if-dotfile-command {
> 
>     if  [[ $BUFFER =~ ^"g d untrack-all" ]]
>     then
>       zle .kill-whole-line
>       BUFFER="g d rm --cached -r ~"
>       zle .accept-line
>     elif [[ $BUFFER =~ ^"g d add-and-commit" ]]
>     then 
>       BUFFERz=$(echo $BUFFER | cut -d '"' -f2)
>       zle .kill-whole-line
>       BUFFER="git d add ~ && g d commit -am \"${BUFFERz}\""
>       zle .accept-line
>     elif [[ $BUFFER =~ ^"g d sb" ]]
>     then 
>       zle .kill-whole-line
>       BUFFER="git d branch | rofi -dmenu | xargs git checkout"
>       zle .accept-line
>     elif [[ $BUFFER =~ ^"g d stash-and-reset" ]]
>     then 
>       zle .kill-whole-line
>       BUFFER="git d stash && git d reset --hard HEAD"
>       zle .accept-line
>     elif [[ $BUFFER =~ ^"g d last" ]]
>     then 
>       zle .kill-whole-line
>       BUFFER="git d --no-pager log -1 --oneline"
>       zle .accept-line
>     else
>         zle .accept-line
>     fi 
> }
> 
> zle -N accept-line _check-if-dotfile-command

I currently have a directory named ~/.shell which is a git repo for all the various . Files in my home folder (via hard links). What is the widget intended to do and how is it triggered?

(I realize you've moved away from this widget, but what were you trying for).

-- 
ɹןʇnqן
<mailto:lbutler@covisp.net>
tel:+1.303.219.0564





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: save a command in history from within the widget
  2020-10-29 11:07 ` Lewis Butler
@ 2020-10-29 23:54   ` Ahmad Ismail
  0 siblings, 0 replies; 10+ messages in thread
From: Ahmad Ismail @ 2020-10-29 23:54 UTC (permalink / raw)
  To: Lewis Butler; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 2709 bytes --]

Hi Lewis Butler,

The problem I was trying to solve is given in

https://www.reddit.com/r/git/comments/jdl11c/can_not_use_some_of_my_aliases_with_bare_repo/
https://stackoverflow.com/questions/64416625/can-not-use-some-of-my-aliases-with-bare-repo

let me explain the code you referred to. accept-line widget is invoked
after we press enter in the command line. I replace that with
_check-if-dotfile-command using the following line

zle -N accept-line _check-if-dotfile-command

The code took the command (which is in the  $BUFFER) and replaced it with
another string and then ran accept-line widget. For example any line
starting with `g d stash-and-reset` was replaced with `git d stash && git d
reset --hard HEAD`.


*Thanks and Best Regards,Ahmad Ismail*


On Thu, Oct 29, 2020 at 5:07 PM Lewis Butler <lbutler@covisp.net> wrote:

> On 24 Oct 2020, at 13:00, Ahmad Ismail <ismail783@gmail.com> wrote:
> > I am currently working on a widget. It is used so that I can use my git
> alias also for my dotfiles.
>
> This is, to me, very advanced zsh and while reading through it I am still
> not exactly sure what it does or what the purpose is.
>
> > The widget is given bellow:
> >
> > function _check-if-dotfile-command {
> >
> >     if  [[ $BUFFER =~ ^"g d untrack-all" ]]
> >     then
> >       zle .kill-whole-line
> >       BUFFER="g d rm --cached -r ~"
> >       zle .accept-line
> >     elif [[ $BUFFER =~ ^"g d add-and-commit" ]]
> >     then
> >       BUFFERz=$(echo $BUFFER | cut -d '"' -f2)
> >       zle .kill-whole-line
> >       BUFFER="git d add ~ && g d commit -am \"${BUFFERz}\""
> >       zle .accept-line
> >     elif [[ $BUFFER =~ ^"g d sb" ]]
> >     then
> >       zle .kill-whole-line
> >       BUFFER="git d branch | rofi -dmenu | xargs git checkout"
> >       zle .accept-line
> >     elif [[ $BUFFER =~ ^"g d stash-and-reset" ]]
> >     then
> >       zle .kill-whole-line
> >       BUFFER="git d stash && git d reset --hard HEAD"
> >       zle .accept-line
> >     elif [[ $BUFFER =~ ^"g d last" ]]
> >     then
> >       zle .kill-whole-line
> >       BUFFER="git d --no-pager log -1 --oneline"
> >       zle .accept-line
> >     else
> >         zle .accept-line
> >     fi
> > }
> >
> > zle -N accept-line _check-if-dotfile-command
>
> I currently have a directory named ~/.shell which is a git repo for all
> the various . Files in my home folder (via hard links). What is the widget
> intended to do and how is it triggered?
>
> (I realize you've moved away from this widget, but what were you trying
> for).
>
> --
> ɹןʇnqן
> <mailto:lbutler@covisp.net>
> tel:+1.303.219.0564
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 4742 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-10-29 23:54 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-24 19:00 save a command in history from within the widget Ahmad Ismail
2020-10-25 20:24 ` Daniel Shahaf
2020-10-25 21:13   ` Ahmad Ismail
2020-10-26 22:04     ` Daniel Shahaf
2020-10-27 19:01       ` Ahmad Ismail
2020-10-26  7:11   ` Roman Perepelitsa
2020-10-26 11:15     ` Ahmad Ismail
2020-10-26 14:19       ` Ahmad Ismail
2020-10-29 11:07 ` Lewis Butler
2020-10-29 23:54   ` Ahmad Ismail

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git