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

New feature: smart history #2248

Open
log69 opened this issue May 5, 2022 · 0 comments
Open

New feature: smart history #2248

log69 opened this issue May 5, 2022 · 0 comments

Comments

@log69
Copy link

log69 commented May 5, 2022

Hi Guys, pry is awesome, thank you for it. I'd like to ask for a new feature that would be awesome, even if not default.

It would be great if the order of command history shown when pressing the up arrow key or searching for a pattern would be weighted by frequency and recency. This would give a smart and highly adaptive order. It is easy to code and runs fast in O(n) time if not counting the sort method in the end.

The solution is to go through all the texts of the command history one by one from old to recent ones and store the info in a hash object. The key of the hash would be the text of the command itself and the value of the hash would be increased by the square of the actual index continuously. And finally the hash needs to be sorted by its values and its keys will show the final result.

See my Ruby solution where the list array contains the command history and the power (**2) determines the weight of recency:
temp = {}; list.each_with_index{|x,i| temp[x] = temp[x].to_f + ( i + 1 )**2 }; result = temp.sort_by{|x,y| y }.map{|x,y| x }

Example (let's say the command is a single number for easier understanding, the values on the right are the more recent ones and the ones to show first):
list = [1, 0, 1, 1, 1, 1, 2, 0, 0, 3]; result = [2, 1, 3, 0]

If someone really needs the last command to show first for convenience, then the code can be easily modified to be adaptive from the second most recent command only. It can also be an option to use it for the search only.

Any feedback is highly appreciated. Cheers.

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

1 participant