Improve the @@ command to include cycling
Courtesy gemini.
This commit is contained in:
parent
d2c2fee733
commit
341fd1057e
1 changed files with 46 additions and 15 deletions
|
|
@ -1,20 +1,51 @@
|
||||||
function bind_at
|
function bind_at
|
||||||
|
# 1. Get the list of files (Sorted by time, newest first)
|
||||||
|
# -p adds '/' to dirs so we can filter them out with grep -v
|
||||||
|
set -l files (command ls -tAp | string match -v '*/')
|
||||||
|
|
||||||
|
# 2. Get the current token on the command line
|
||||||
set -l token (commandline -t)
|
set -l token (commandline -t)
|
||||||
if string match -q -- "*@" "$token"
|
|
||||||
commandline -f backward-delete-char
|
# --- CYCLE MODE ---
|
||||||
set -l newest_file
|
# Check if the current token matches a file in our list.
|
||||||
for f in (command ls -tA)
|
# If it does, we assume you're hitting @ again to "scroll" back in time.
|
||||||
if test -f "$f"
|
set -l found_index 0
|
||||||
set newest_file "$f"
|
for i in (seq (count $files))
|
||||||
|
# We compare against the escaped version because that's what sits on the command line
|
||||||
|
if test "$token" = (string escape -- $files[$i])
|
||||||
|
set found_index $i
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if test -n "$newest_file"
|
|
||||||
commandline -i (string escape -- "$newest_file")
|
if test $found_index -gt 0
|
||||||
else
|
# Found it! Calculate the next index (wrapping around if needed)
|
||||||
commandline -i "@"
|
set -l next_index (math $found_index + 1)
|
||||||
|
if test $next_index -gt (count $files)
|
||||||
|
set next_index 1
|
||||||
end
|
end
|
||||||
else
|
|
||||||
commandline -i "@"
|
# Replace the current token with the next file
|
||||||
|
set -l next_file (string escape -- $files[$next_index])
|
||||||
|
commandline -t -- $next_file
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# --- TRIGGER MODE (@@) ---
|
||||||
|
# If it wasn't a file, check if we just finished typing "@@"
|
||||||
|
if string match -q -- "*@" "$token"
|
||||||
|
commandline -f backward-delete-char # Delete the last @
|
||||||
|
|
||||||
|
# Insert the very first (newest) file
|
||||||
|
if test (count $files) -gt 0
|
||||||
|
commandline -i (string escape -- $files[1])
|
||||||
|
else
|
||||||
|
commandline -i "@" # No files? fallback
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
# --- DEFAULT MODE ---
|
||||||
|
# Just insert an @ literally
|
||||||
|
commandline -i "@"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue