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

Include autosuggest strategies #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions autosuggest-strategy.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# This query will find the most frequently issued command that is issued in
# the current directory or any subdirectory.
# You can get other behaviours by changing the query.
_zsh_autosuggest_strategy_histdb_top_here() {
local query="
SELECT
commands.argv
FROM
history
LEFT JOIN
commands
ON
history.command_id = commands.id
LEFT JOIN
places
ON
history.place_id = places.id
WHERE
places.dir
LIKE
'$(sql_escape $PWD)%'
AND
commands.argv
LIKE
'$(sql_escape $1)%'
GROUP BY
commands.argv
ORDER BY
COUNT(commands.argv) DESC
LIMIT 1"
suggestion=$(_histdb_query "$query")
}

# 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.
# You could use other fields like the hostname to restrict to suggestions on this host, etc.
_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(commands.argv) DESC
LIMIT 1"
suggestion=$(_histdb_query "$query")
}

# This query will find the most recently issued command that is issued in
# the current directory or any subdirectory preferring commands in the current session.
_zsh_autosuggest_strategy_histdb_top_here_and_now() {
local query="
SELECT
commands.argv
FROM
history
LEFT JOIN
commands
ON
history.command_id = commands.id
LEFT JOIN
places
ON
history.place_id = places.id
WHERE
places.dir
LIKE
'$(sql_escape $PWD)%'
AND
commands.argv
LIKE
'$(sql_escape $1)%'
AND
history.exit_status = 0
GROUP BY
commands.argv,
history.session,
history.start_time
ORDER BY
history.session = '$(sql_escape $HISTDB_SESSION)' DESC,
history.start_time DESC
LIMIT 1"
suggestion=$(_histdb_query "$query")
}