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

Initialize McFly only in interactive shell #428

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
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
47 changes: 24 additions & 23 deletions mcfly.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function mcfly_initialize {
# Ensure stdin is a tty
[[ -t 0 ]] || return 0

# Ensure an interactive shell
[[ $- =~ .*i.* ]] || return 0

# Avoid loading this file more than once
[[ "${__MCFLY_LOADED-}" != "loaded" ]] || return 0
__MCFLY_LOADED="loaded"
Expand Down Expand Up @@ -113,30 +116,28 @@ function mcfly_initialize {
PROMPT_COMMAND="mcfly_prompt_command;${PROMPT_COMMAND#;}"
fi

# If this is an interactive shell, take ownership of ctrl-r.
if [[ $- =~ .*i.* ]]; then
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
# shellcheck disable=SC2016
if [[ ${MCFLY_BASH_USE_TIOCSTI-} = 1 ]]; then
bind -x '"\C-r": "mcfly_search_with_tiocsti"'
else
# Bind ctrl+r to 2 keystrokes, the first one is used to search in McFly, the second one is used to run the command (if mcfly_search binds it to accept-line).
bind -x "\"$MCFLY_BASH_SEARCH_KEYBINDING\":\"mcfly_search\""
bind "\"\C-r\":\"$MCFLY_BASH_SEARCH_KEYBINDING$MCFLY_BASH_ACCEPT_LINE_KEYBINDING\""
fi
# Take ownership of ctrl-r.
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
# shellcheck disable=SC2016
if [[ ${MCFLY_BASH_USE_TIOCSTI-} = 1 ]]; then
bind -x '"\C-r": "mcfly_search_with_tiocsti"'
else
# Bind ctrl+r to 2 keystrokes, the first one is used to search in McFly, the second one is used to run the command (if mcfly_search binds it to accept-line).
bind -x "\"$MCFLY_BASH_SEARCH_KEYBINDING\":\"mcfly_search\""
bind "\"\C-r\":\"$MCFLY_BASH_SEARCH_KEYBINDING$MCFLY_BASH_ACCEPT_LINE_KEYBINDING\""
fi
else
# The logic here is:
# 1. Jump to the beginning of the edit buffer, add 'mcfly: ', and comment out the current line. We comment out the line
# to ensure that all possible special characters, including backticks, are ignored. This commented out line will
# end up as the most recent entry in the $MCFLY_HISTORY file.
# 2. Type "mcfly search" and then run the command. McFly will pull the last line from the $MCFLY_HISTORY file,
# which should be the commented-out search from step #1. It will then remove that line from the history file and
# render the search UI pre-filled with it.
if set -o | grep "vi " | grep -q on; then
bind '"\C-r": "\e0i#mcfly: \e\C-m mcfly search\C-m"'
else
# The logic here is:
# 1. Jump to the beginning of the edit buffer, add 'mcfly: ', and comment out the current line. We comment out the line
# to ensure that all possible special characters, including backticks, are ignored. This commented out line will
# end up as the most recent entry in the $MCFLY_HISTORY file.
# 2. Type "mcfly search" and then run the command. McFly will pull the last line from the $MCFLY_HISTORY file,
# which should be the commented-out search from step #1. It will then remove that line from the history file and
# render the search UI pre-filled with it.
if set -o | grep "vi " | grep -q on; then
bind '"\C-r": "\e0i#mcfly: \e\C-m mcfly search\C-m"'
else
bind '"\C-r": "\C-amcfly: \e# mcfly search\C-m"'
fi
bind '"\C-r": "\C-amcfly: \e# mcfly search\C-m"'
fi
fi
}
Expand Down
71 changes: 36 additions & 35 deletions mcfly.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,48 @@
if test "$__MCFLY_LOADED" != "loaded"
set -g __MCFLY_LOADED "loaded"

# Note: we only use the history file for the session when this file was sourced.
# Would have to reset this before calling mcfly if you want commands from another session later.
if not set -q MCFLY_HISTFILE
set -gx MCFLY_HISTFILE (set -q XDG_DATA_HOME; and echo $XDG_DATA_HOME; or echo $HOME/.local/share)/fish/(set -q fish_history; and echo $fish_history; or echo fish)_history
end
if not test -r "$MCFLY_HISTFILE"
echo "McFly: $MCFLY_HISTFILE does not exist or is not readable. Please fix this or set MCFLY_HISTFILE to something else before using McFly." >&2
exit 1
end
# If this is an interactive shell
if status is-interactive
# Note: we only use the history file for the session when this file was sourced.
# Would have to reset this before calling mcfly if you want commands from another session later.
if not set -q MCFLY_HISTFILE
set -gx MCFLY_HISTFILE (set -q XDG_DATA_HOME; and echo $XDG_DATA_HOME; or echo $HOME/.local/share)/fish/(set -q fish_history; and echo $fish_history; or echo fish)_history
end
if not test -r "$MCFLY_HISTFILE"
echo "McFly: $MCFLY_HISTFILE does not exist or is not readable. Please fix this or set MCFLY_HISTFILE to something else before using McFly." >&2
exit 1
end

# MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session.
set -gx MCFLY_SESSION_ID (dd if=/dev/urandom bs=256 count=1 2>/dev/null | env LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 24)
# MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session.
set -gx MCFLY_SESSION_ID (dd if=/dev/urandom bs=256 count=1 2>/dev/null | env LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 24)

# Find the binary
set -q MCFLY_PATH; or set -l MCFLY_PATH (command which mcfly)
if test -z "$MCFLY_PATH"; or test "$MCFLY_PATH" = "mcfly not found"
echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.fish"
exit 1
end
# We don't need a MCFLY_HISTORY file because we can get the last command in fish_postexec.
set -gx MCFLY_HISTORY /dev/null
set -g __MCFLY_CMD $MCFLY_PATH --mcfly_history $MCFLY_HISTORY --history_format fish
# Find the binary
set -q MCFLY_PATH; or set -l MCFLY_PATH (command which mcfly)
if test -z "$MCFLY_PATH"; or test "$MCFLY_PATH" = "mcfly not found"
echo "Cannot find the mcfly binary, please make sure that mcfly is in your path before sourcing mcfly.fish"
exit 1
end
# We don't need a MCFLY_HISTORY file because we can get the last command in fish_postexec.
set -gx MCFLY_HISTORY /dev/null
set -g __MCFLY_CMD $MCFLY_PATH --mcfly_history $MCFLY_HISTORY --history_format fish

function __mcfly_save_old_pwd -d 'Save PWD before running command' -e fish_preexec
set -g __MCFLY_OLD_PWD "$PWD"
end
function __mcfly_save_old_pwd -d 'Save PWD before running command' -e fish_preexec
set -g __MCFLY_OLD_PWD "$PWD"
end

function __mcfly_add_command -d 'Add run commands to McFly database' -e fish_postexec
# Retain return code of last command before we lose it
set -l last_status $status
# Check for the private mode
test -n "$fish_private_mode"; and return
# Handle first call of this function after sourcing mcfly.fish, when the old PWD won't be set
set -q __MCFLY_OLD_PWD; or set -g __MCFLY_OLD_PWD "$PWD"
function __mcfly_add_command -d 'Add run commands to McFly database' -e fish_postexec
# Retain return code of last command before we lose it
set -l last_status $status
# Check for the private mode
test -n "$fish_private_mode"; and return
# Handle first call of this function after sourcing mcfly.fish, when the old PWD won't be set
set -q __MCFLY_OLD_PWD; or set -g __MCFLY_OLD_PWD "$PWD"

test -n "$MCFLY_DEBUG"; and echo mcfly.fish: Run eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]'
eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]'
end
test -n "$MCFLY_DEBUG"; and echo mcfly.fish: Run eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]'
eval $__MCFLY_CMD add --exit '$last_status' --old-dir '$__MCFLY_OLD_PWD' -- '$argv[1]'
end

# If this is an interactive shell, set up key binding functions.
if status is-interactive
# Set up key binding functions.
function __mcfly-history-widget -d "Search command history with McFly"
set tmpdir $TMPDIR
if test -z "$tmpdir"
Expand Down
58 changes: 28 additions & 30 deletions mcfly.zsh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/zsh

() {
# Ensure stdin is a tty
# Ensure an interactive shell
[[ -o interactive ]] || return 0

# Setup MCFLY_HISTFILE and make sure it exists.
Expand Down Expand Up @@ -71,34 +71,32 @@
[ -n "$MCFLY_DEBUG" ] && echo "mcfly_exit_logger already in zshexit_functions, skipping"
fi

# If this is an interactive shell, take ownership of ctrl-r.
if [[ $- =~ .*i.* ]]; then
mcfly-history-widget() {
() {
echoti rmkx
exec </dev/tty
local mcfly_output=$(mktemp ${TMPDIR:-/tmp}/mcfly.output.XXXXXXXX)
$MCFLY_PATH --history_format $MCFLY_HISTORY_FORMAT search -o "${mcfly_output}" "${LBUFFER}"
echoti smkx

# Interpret commandline/run requests from McFly
while read -r key val; do
if [[ "$key" = "mode" ]]; then local mode="$val"; fi
if [[ "$key" = "commandline" ]]; then local commandline="$val"; fi
done < "${mcfly_output}"
command rm -f $mcfly_output

if [[ -n $commandline ]]; then
RBUFFER=""
LBUFFER="${commandline}"
fi
if [[ "${mode}" == "run" ]]; then
zle accept-line
fi
zle redisplay
}
# Take ownership of ctrl-r.
mcfly-history-widget() {
() {
echoti rmkx
exec </dev/tty
local mcfly_output=$(mktemp ${TMPDIR:-/tmp}/mcfly.output.XXXXXXXX)
$MCFLY_PATH --history_format $MCFLY_HISTORY_FORMAT search -o "${mcfly_output}" "${LBUFFER}"
echoti smkx

# Interpret commandline/run requests from McFly
while read -r key val; do
if [[ "$key" = "mode" ]]; then local mode="$val"; fi
if [[ "$key" = "commandline" ]]; then local commandline="$val"; fi
done < "${mcfly_output}"
command rm -f $mcfly_output

if [[ -n $commandline ]]; then
RBUFFER=""
LBUFFER="${commandline}"
fi
if [[ "${mode}" == "run" ]]; then
zle accept-line
fi
zle redisplay
}
zle -N mcfly-history-widget
bindkey '^R' mcfly-history-widget
fi
}
zle -N mcfly-history-widget
bindkey '^R' mcfly-history-widget
}
Loading