Skip to content

New feature: smart history #2248

Open
Open
@log69

Description

@log69

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions