larkery / zsh-histdb

A slightly better history for zsh
MIT License
1.29k stars 75 forks source link

Example query doesn't seem to do what it purports to #108

Closed ctranstrum closed 3 years ago

ctranstrum commented 3 years ago

In the README, there is an example query that claims "This will find the most frequently issued command issued exactly in this directory, or if there are no matches it will find the most frequently issued command in any directory."

The query is as follows.

_zsh_autosuggest_strategy_histdb_top() {
    local query="
        select commands.argv from history
        left join commands on history.command_id = commands.rowid
        left join places on history.place_id = places.rowid
        where commands.argv LIKE '$(sql_escape $1)%'
        group by commands.argv
        order by places.dir != '$(sql_escape $PWD)', count(*) desc
        limit 1
    "
    suggestion=$(_histdb_query "$query")
}

However, this doesn't work for me. If I make the following change, though, it does.

_zsh_autosuggest_strategy_histdb_top() {
    local query="
        select commands.argv from history
        left join commands on history.command_id = commands.rowid
        left join places on history.place_id = places.rowid
        where commands.argv LIKE '$(sql_escape $1)%'
        group by commands.argv, places.dir
        order by places.dir != '$(sql_escape $PWD)', count(*) desc
        limit 1
    "
    suggestion=$(_histdb_query "$query")
}

Grouping by commands.argv alone omits results from multiple directories in the final output, so it can sometimes exclude a command you've issued many times in the current directory and instead include that same command but in another directory, which makes it less preferred, allowing a less frequently issued command to be selected. Adding places.dir to the group by clause corrects this.

I realize that this is just an example query and not necessarily intended to be used. But if other people are looking for the behavior that I was looking for, I think it would be useful to update the query in the README.

larkery commented 3 years ago

My bad - if you want to submit a pull request I will merge it.

ctranstrum commented 3 years ago

Pull request added in #114