Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

[Experimental] Improved SSH Support #14

Draft
wants to merge 3 commits into
base: sean/pty_integration
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions fig.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
if [ -d /Applications/Fig.app -o -d ~/Applications/Fig.app ] \
&& command -v fig 1>/dev/null 2>/dev/null

if [ -t 1 ] && [ -z "$FIG_ENV_VAR" ] || [ -n "$TMUX" ]
# Gives fig context for cwd in each window.
fig bg:init $fish_pid (tty)

# Run aliases shell script
if [ -s ~/.fig/user/aliases/_myaliases.sh ]
bash ~/.fig/user/aliases/*.sh
end

# Check for prompts or onboarding.
if [ -s ~/.fig/tools/prompts.sh ]
bash ~/.fig/tools/prompts.sh
end

export TTY=(tty)
export FIG_ENV_VAR=1
end

if [ -z "$FIG_SHELL_VAR" ]
# Prevents weird interaction where setting the title with ANSI escape
# sequence triggers prompt redraw.
fig settings autocomplete.addStatusToTerminalTitle false &
fig bg:exec $fish_pid (tty) &

source ~/.fig/shell/post.fish
end
end
6 changes: 1 addition & 5 deletions fig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ if ([[ -d /Applications/Fig.app ]] || [[ -d ~/Applications/Fig.app ]]) \
[[ -s ~/.fig/tools/prompts.sh ]] && source ~/.fig/tools/prompts.sh
fi

# We use a shell variable to make sure this doesn't load twice
if [[ -z "${FIG_SHELL_VAR}" ]]; then
source ~/.fig/shell/post.sh
FIG_SHELL_VAR=1
fi
source ~/.fig/shell/post.sh

# todo: Add a check to confirm "add-zle-hook-widget" facility exists
# Not included in fig.zsh, because should be run last
Expand Down
4 changes: 2 additions & 2 deletions shell/post.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function fig_osc { printf "\033]697;"; printf $@; printf "\007"; }
function __fig_preexec() {
__fig_ret_value="$?"

$(fig bg:exec $$ $(tty) &)
$(command -v fig > /dev/null 2>&1 && fig bg:exec $$ $(tty) &)

fig_osc PreExec

Expand All @@ -38,7 +38,7 @@ function __fig_preexec() {
function __fig_prompt () {
__fig_ret_value="$?"

$(fig bg:prompt $$ $TTY &)
$(command -v fig > /dev/null 2>&1 && fig bg:prompt $$ $TTY &)

# Work around bug in CentOS 7.2 where preexec doesn't run if you press ^C
# while entering a command.
Expand Down
119 changes: 46 additions & 73 deletions shell/post.fish
Original file line number Diff line number Diff line change
@@ -1,95 +1,68 @@
if [ -d /Applications/Fig.app -o -d ~/Applications/Fig.app ] \
&& command -v fig 1>/dev/null 2>/dev/null
if [ -z "$FIG_SHELL_VAR" ]
function fig_keybuffer --on-signal SIGUSR1
echo fig bg:fish-keybuffer "$TERM_SESSION_ID" $FIG_INTEGRATION_VERSION 0 (commandline -C) \"(commandline)\" \
| base64 \
| /usr/bin/nc -U /tmp/fig.socket
end

if [ -t 1 ] && [ -z "$FIG_ENV_VAR" ] || [ -n "$TMUX" ]
# Gives fig context for cwd in each window.
fig bg:init $fish_pid (tty)
function fig_osc; printf "\033]697;"; printf $argv; printf "\007"; end
function fig_copy_fn; functions -e $argv[2]; functions -c $argv[1] $argv[2]; end
function fig_fn_defined; test (functions $argv[1] | grep -vE '^ *(#|function |end$|$)' | wc -l | xargs) != 0; end

# Run aliases shell script
if [ -s ~/.fig/user/aliases/_myaliases.sh ]
bash ~/.fig/user/aliases/*.sh
end
function fig_wrap_prompt
set -l last_status $status
fig_osc StartPrompt

# Check for prompts or onboarding.
if [ -s ~/.fig/tools/prompts.sh ]
bash ~/.fig/tools/prompts.sh
end
sh -c "exit $last_status"
printf "%b" (string join "\n" $argv)
fig_osc EndPrompt

export TTY=(tty)
export FIG_ENV_VAR=1
sh -c "exit $last_status"
end

if [ -z "$FIG_SHELL_VAR" ]
function fig_keybuffer --on-signal SIGUSR1
echo fig bg:fish-keybuffer "$TERM_SESSION_ID" $FIG_INTEGRATION_VERSION 0 (commandline -C) \"(commandline)\" \
| base64 \
| /usr/bin/nc -U /tmp/fig.socket
end

function fig_osc; printf "\033]697;"; printf $argv; printf "\007"; end
function fig_copy_fn; functions -e $argv[2]; functions -c $argv[1] $argv[2]; end
function fig_fn_defined; test (functions $argv[1] | grep -vE '^ *(#|function |end$|$)' | wc -l | xargs) != 0; end
function fig_preexec --on-event fish_preexec
command -v fig > /dev/null 2>&1 && fig bg:exec $fish_pid (tty) &; disown
fig_osc PreExec

function fig_wrap_prompt
set -l last_status $status
fig_osc StartPrompt

sh -c "exit $last_status"
printf "%b" (string join "\n" $argv)
fig_osc EndPrompt

sh -c "exit $last_status"
if fig_fn_defined fig_user_mode_prompt
fig_copy_fn fig_user_mode_prompt fish_mode_prompt
end

function fig_preexec --on-event fish_preexec
fig bg:exec $fish_pid (tty) &; disown
fig_osc PreExec

if fig_fn_defined fig_user_mode_prompt
fig_copy_fn fig_user_mode_prompt fish_mode_prompt
end

if fig_fn_defined fig_user_right_prompt
fig_copy_fn fig_user_right_prompt fish_right_prompt
end

fig_copy_fn fig_user_prompt fish_prompt

set fig_has_set_prompt 0
if fig_fn_defined fig_user_right_prompt
fig_copy_fn fig_user_right_prompt fish_right_prompt
end

function fig_precmd --on-event fish_prompt
fig bg:prompt $fish_pid (tty) &; disown
fig_copy_fn fig_user_prompt fish_prompt

if [ $fig_has_set_prompt = 1 ]
fig_preexec
end
set fig_has_set_prompt 0
end

fig_osc "Dir=%s" $PWD
fig_osc "Shell=fish" $PWD
function fig_precmd --on-event fish_prompt
command -v fig > /dev/null 2>&1 && fig bg:prompt $fish_pid (tty) &; disown

if fig_fn_defined fish_mode_prompt
fig_copy_fn fish_mode_prompt fig_user_mode_prompt
function fish_mode_prompt; fig_wrap_prompt (fig_user_mode_prompt); end
end
if [ $fig_has_set_prompt = 1 ]
fig_preexec
end

if fig_fn_defined fish_right_prompt
fig_copy_fn fish_right_prompt fig_user_right_prompt
function fish_right_prompt; fig_wrap_prompt (fig_user_right_prompt); end
end
fig_osc "Dir=%s" $PWD
fig_osc "Shell=fish" $PWD

fig_copy_fn fish_prompt fig_user_prompt
function fish_prompt; fig_wrap_prompt (fig_user_prompt); fig_osc NewCmd; end
if fig_fn_defined fish_mode_prompt
fig_copy_fn fish_mode_prompt fig_user_mode_prompt
function fish_mode_prompt; fig_wrap_prompt (fig_user_mode_prompt); end
end

set fig_has_set_prompt 1
if fig_fn_defined fish_right_prompt
fig_copy_fn fish_right_prompt fig_user_right_prompt
function fish_right_prompt; fig_wrap_prompt (fig_user_right_prompt); end
end

set FIG_SHELL_VAR 1
set fig_has_set_prompt 0
fig_copy_fn fish_prompt fig_user_prompt
function fish_prompt; fig_wrap_prompt (fig_user_prompt); fig_osc NewCmd; end

# Prevents weird interaction where setting the title with ANSI escape
# sequence triggers prompt redraw.
fig settings autocomplete.addStatusToTerminalTitle false &
fig bg:exec $fish_pid (tty) &
set fig_has_set_prompt 1
end

set FIG_SHELL_VAR 1
set fig_has_set_prompt 0
end
34 changes: 19 additions & 15 deletions shell/post.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
if [[ -n "$BASH" ]]; then
source ~/.fig/shell/post.bash
elif [[ -n "$ZSH_NAME" ]]; then
source ~/.fig/shell/post.zsh
else
# Fallback to naive method, exposed alteration of prompt variables if in unrecognized shell.
if [ -z "${FIG_PTY_SHELL_VAR}" ]; then
FIG_SHELL="\001\033]697;Shell=unknown\007\002"
FIG_START_PROMPT="${FIG_SHELL}\001\033]697;StartPrompt\007\002"
FIG_END_PROMPT="\001\033]697;EndPrompt\007\002"
FIG_NEW_CMD="\001\033]697;NewCmd\007\002"
# We use a shell variable to make sure this doesn't load twice
if [[ -z "${FIG_SHELL_VAR}" ]]; then
if [[ -n "$BASH" ]]; then
source ~/.fig/shell/post.bash
elif [[ -n "$ZSH_NAME" ]]; then
source ~/.fig/shell/post.zsh
else
# Fallback to naive method, exposed alteration of prompt variables if in unrecognized shell.
if [ -z "${FIG_PTY_SHELL_VAR}" ]; then
FIG_SHELL="\001\033]697;Shell=unknown\007\002"
FIG_START_PROMPT="${FIG_SHELL}\001\033]697;StartPrompt\007\002"
FIG_END_PROMPT="\001\033]697;EndPrompt\007\002"
FIG_NEW_CMD="\001\033]697;NewCmd\007\002"

PS1="${FIG_START_PROMPT}${PS1}${FIG_END_PROMPT}${FIG_NEW_CMD}"
PS2="${FIG_START_PROMPT}${PS2}${FIG_END_PROMPT}"
PS3="${FIG_START_PROMPT}${PS3}${FIG_END_PROMPT}${FIG_NEW_CMD}"
PS1="${FIG_START_PROMPT}${PS1}${FIG_END_PROMPT}${FIG_NEW_CMD}"
PS2="${FIG_START_PROMPT}${PS2}${FIG_END_PROMPT}"
PS3="${FIG_START_PROMPT}${PS3}${FIG_END_PROMPT}${FIG_NEW_CMD}"

FIG_PTY_SHELL_VAR=1
FIG_PTY_SHELL_VAR=1
fi
fi
FIG_SHELL_VAR=1
fi
4 changes: 2 additions & 2 deletions shell/post.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FIG_HAS_ZSH_PTY_HOOKS=1
FIG_HAS_SET_PROMPT=0

fig_preexec() {
fig bg:exec $$ $TTY &!
command -v fig > /dev/null 2>&1 && fig bg:exec $$ $TTY &!

# Restore user defined prompt before executing.
PS1="$FIG_USER_PS1"
Expand All @@ -19,7 +19,7 @@ fig_preexec() {
}

fig_precmd() {
fig bg:prompt $$ $TTY &!
command -v fig > /dev/null 2>&1 && fig bg:prompt $$ $TTY &!

if [ $FIG_HAS_SET_PROMPT -eq 1 ]; then
# ^C pressed while entering command, call preexec manually to clear fig prompts.
Expand Down
7 changes: 7 additions & 0 deletions shell/remote.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

# We use a shell variable to make sure this doesn't load twice
if [[ -z "${FIG_SHELL_VAR}" ]]; then
source ~/.fig/shell/post.sh
FIG_SHELL_VAR=1
fi
2 changes: 1 addition & 1 deletion ssh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ ControlPath ~/.ssh/%C
ControlMaster auto
ControlPersist 1
PermitLocalCommand yes
LocalCommand fig bg:ssh ~/.ssh/%C &
LocalCommand fig bg:ssh ~/.ssh/%C & ~/.fig/tools/install_remote.sh ~/.ssh/%C %r %h &
35 changes: 5 additions & 30 deletions tools/install_and_upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,12 @@ install_fig() {
fi
}

fig_source() {
printf "#### FIG ENV VARIABLES ####\n"
printf "[ -s ~/.fig/$1 ] && source ~/.fig/$1\n"
printf "#### END FIG ENV VARIABLES ####\n"
}

fig_append() {
# Appends line to a config file to source file from the ~/.fig directory.
# Usage: fig_append fig.sh path/to/rc
# Don't append to files that don't exist to avoid creating file and
# changing shell behavior.
if [ -f "$2" ] && ! grep -q "source ~/.fig/$1" "$2"; then
echo "$(fig_source $1)" >> "$2"
fi
}

fig_prepend() {
# Prepends line to a config file to source file from the ~/.fig directory.
# Usage: fig_prepend fig_pre.sh path/to/rc
# Don't prepend to files that don't exist to avoid creating file and
# changing shell behavior.
if [ -f "$2" ] && ! grep -q "source ~/.fig/$1" "$2"; then
echo -e "$(fig_source $1)\n$(cat $2)" > $2
fi
}

# Add the fig.sh to your profiles so it can be sourced on new terminal window load
append_to_profiles() {
# Replace old sourcing in profiles.
for rc in .profile .zprofile .bash_profile; do
if [[ -e "${HOME}/${rc}" ]]; then
sed -i '' 's/~\/.fig\/exports\/env.sh/~\/.fig\/fig.sh/g' "~/.${rc}" 2> /dev/null
sed -i '' 's/~\/.fig\/exports\/env.sh/~\/.fig\/fig.sh/g' "${HOME}/${rc}" 2> /dev/null
fi
done

Expand All @@ -137,11 +111,11 @@ append_to_profiles() {
done

# Handle fish separately.
mkdir -p ~/.config/fish/conf.d
mkdir -p "${HOME}/.config/fish/conf.d"

# Use 00_/99_ prefix to load script earlier/later in fish startup.
ln -sf ~/.fig/shell/pre.fish ~/.config/fish/conf.d/00_fig_pre.fish
ln -sf ~/.fig/shell/post.fish ~/.config/fish/conf.d/99_fig_post.fish
ln -sf ~/.fig/fig.fish ~/.config/fish/conf.d/99_fig_post.fish

# Remove deprecated fish config file.
if [[ -f -~/.config/fish/conf.d/fig.fish ]]; then
Expand Down Expand Up @@ -182,7 +156,8 @@ install_tmux_integration() {
}

install_fig
append_to_profiles
source ~/.fig/tools/install_helpers.sh
append_to_profiles "${HOME}"
setup_onboarding
install_tmux_integration
echo success
26 changes: 26 additions & 0 deletions tools/install_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fig_source() {
printf "#### FIG ENV VARIABLES ####\n"
printf "[ -s ~/.fig/$1 ] && source ~/.fig/$1\n"
printf "#### END FIG ENV VARIABLES ####\n"
}

fig_append() {
# Appends line to a config file to source file from the ~/.fig directory.
# Usage: fig_append fig.sh path/to/rc
# Don't append to files that don't exist to avoid creating file and
# changing shell behavior.
if [ -f "$2" ] && ! grep -q "source ~/.fig/$1" "$2"; then
echo "$(fig_source $1)" >> "$2"
fi
}

fig_prepend() {
# Prepends line to a config file to source file from the ~/.fig directory.
# Usage: fig_prepend fig_pre.sh path/to/rc
# Don't prepend to files that don't exist to avoid creating file and
# changing shell behavior.
if [ -f "$2" ] && ! grep -q "source ~/.fig/$1" "$2"; then
echo -e "$(fig_source $1)\n$(cat $2)" > $2
fi
}