add flag to limit `history --search` results #3244

Closed
krader1961 opened this Issue Jul 16, 2016 · 6 comments

Projects

None yet

2 participants

@krader1961
Member

While working on fixing other problems with the history command it occurred to me that there really should be a flag to limit the number of search results to the n most recent commands. I was used to having such a capability when I used zsh and wrote my own history wrapper function when I switched to fish in order to have a similar capability. This would be similar to git log -nnn and far more efficient than piping the output of history -search through head -nnn or directly iterating over $history in a fish function.

@krader1961 krader1961 added this to the fish-future milestone Jul 16, 2016
@krader1961 krader1961 self-assigned this Jul 16, 2016
@floam
Member
floam commented Jul 16, 2016 edited

I find it hard to make an argument for search options for history when we provide string. Similarly, I find it strange to be providing further special tooling on history for limiting the output here.

I don't really see how this like the git completion optimization. Is there a prompt or something that'd suddenly stop being laggy? Because it's not really slow to just use $history for me.

> string split "\n" -- $history[1..500]  | wc -l
     531
> echo $CMD_DURATION
10

I think this would need to be taking multiple seconds to empathize with.

@floam
Member
floam commented Jul 16, 2016 edited

You wouldn't want to use head, not because it's slow but because you'll mess up multiline commands.

Iterating is fine and that should be how people access history anyhow. The history command has been mostly useful for user-friendly deletion.

$ for i in $history[1..500]; echo $i; end
...
$ echo $CMD_DURATION
72
@krader1961
Member

How would you get the timestamp; e.g., history -t, with your example using string? And, as you noted, piping the output through head or tail is slow and doesn't correctly handle multiline commands. Also, slicing isn't as simple as you suggest given the current behavior that a slice endpoint must be a valid index. So if you fewer than 500 entries that fails. 95% of time time I just want to see the recent history to jog my memory.

@floam
Member
floam commented Jul 16, 2016 edited

How would you get the timestamp; e.g., history -t, with your example using string?

We don't currently expose the timestamp through the $history interface - so I don't think I could. This isn't related to the speed of history, just something we don't do yet that we ought to figure out.

I suppose we could provide another magic array. [timestamp1, timestamp2... ] seems pleasant enough but if someone finds an item in $history and tries to look it up in $historyplus interactively they're asking for pain.

So you'd want to have this array have the same entries it does now but also provide the timestamps. This seems nice:

> echo $exthistory[-5]
make install fish #7/15/2015 4:45PM

I was actually considering assembling just that after the fact just in history.fish for the output that we run through the pager now, because it moved the timestamp away from the left side, was easier to find with a regex, made the lines able to run through eval/fish -n directly, and I thought it would be nice if for display purposes the output could entirely be ran through fish_indent --ansi to syntax highlight them, especially with the comment coloring being not bad at all for the timestamp display.

@floam
Member
floam commented Jul 16, 2016 edited

Also, slicing isn't as simple as you suggest given the current behavior that a slice endpoint must be a valid index.

You could handle the errors and stop the loop in the second example or use count on $history if that's not slow.

@floam floam modified the milestone: next-2.x, fish-future Aug 2, 2016
@krader1961 krader1961 modified the milestone: fish 2.4.0, next-2.x Sep 20, 2016
@krader1961 krader1961 added a commit to krader1961/fish-shell that referenced this issue Sep 21, 2016
@krader1961 krader1961 add a flag to limit history search results
This adds a flag to the `history search` command to limit the number of
matching entries to the first "n". The default is unlimited. This is
mostly useful in conjunction with aliases (i.e., functions) that are
intended to report the "n" most recent matching history entries without
piping the result through the user's pager.

Fixes #3244
d6f7f61
@krader1961
Member

With PR 3399, coupled with the three previous changes to the history command, it's now possible to do something I've wanted since I started using fish a year ago:

alias h 'builtin history search -15 --show-time="%a %m-%d %R  "'

Now I can type h to see the fifteen most recent commands I ran and when. More useful is something like this to show the most recent git commands I ran:

$ h -p git
Wed 09-21 13:42  git push
Wed 09-21 13:42  git diff
Wed 09-21 13:42  git status
Wed 09-21 13:41  git reset head src/builtin.cpp
Wed 09-21 13:41  git checkout src/builtin.cpp
Wed 09-21 13:41  git diff --staged
Wed 09-21 13:39  git log --stat -1000
Tue 09-20 22:32  git commit -a
Tue 09-20 22:20  git show-branch
Tue 09-20 21:07  gitvim
Tue 09-20 21:01  git diff head~1
Tue 09-20 20:57  git push --set-upstream origin history-search-limiting
Tue 09-20 20:45  git checkout share/completions/history.fish
Tue 09-20 20:45  git reset share/completions/history.fish
Tue 09-20 20:37  git stash pop
@krader1961 krader1961 added a commit that closed this issue Sep 24, 2016
@krader1961 krader1961 add a flag to limit history search results
This adds a flag to the `history search` command to limit the number of
matching entries to the first "n". The default is unlimited. This is
mostly useful in conjunction with aliases (i.e., functions) that are
intended to report the "n" most recent matching history entries without
piping the result through the user's pager.

Fixes #3244
e9b5505
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment