mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-19 10:16:12 +03:00
Simplify Bash Integration (#299)
* shell-integration: new bash integration that is much simpler * shell-integration: bash fixes
This commit is contained in:

committed by
GitHub

parent
95591115de
commit
9d61212451
380
src/shell-integration/bash/bash-preexec.sh
Normal file
380
src/shell-integration/bash/bash-preexec.sh
Normal file
@ -0,0 +1,380 @@
|
||||
# bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
|
||||
# https://github.com/rcaloras/bash-preexec
|
||||
#
|
||||
#
|
||||
# 'preexec' functions are executed before each interactive command is
|
||||
# executed, with the interactive command as its argument. The 'precmd'
|
||||
# function is executed before each prompt is displayed.
|
||||
#
|
||||
# Author: Ryan Caloras (ryan@bashhub.com)
|
||||
# Forked from Original Author: Glyph Lefkowitz
|
||||
#
|
||||
# V0.5.0
|
||||
#
|
||||
|
||||
# General Usage:
|
||||
#
|
||||
# 1. Source this file at the end of your bash profile so as not to interfere
|
||||
# with anything else that's using PROMPT_COMMAND.
|
||||
#
|
||||
# 2. Add any precmd or preexec functions by appending them to their arrays:
|
||||
# e.g.
|
||||
# precmd_functions+=(my_precmd_function)
|
||||
# precmd_functions+=(some_other_precmd_function)
|
||||
#
|
||||
# preexec_functions+=(my_preexec_function)
|
||||
#
|
||||
# 3. Consider changing anything using the DEBUG trap or PROMPT_COMMAND
|
||||
# to use preexec and precmd instead. Preexisting usages will be
|
||||
# preserved, but doing so manually may be less surprising.
|
||||
#
|
||||
# Note: This module requires two Bash features which you must not otherwise be
|
||||
# using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override
|
||||
# either of these after bash-preexec has been installed it will most likely break.
|
||||
|
||||
# Tell shellcheck what kind of file this is.
|
||||
# shellcheck shell=bash
|
||||
|
||||
# Make sure this is bash that's running and return otherwise.
|
||||
# Use POSIX syntax for this line:
|
||||
if [ -z "${BASH_VERSION-}" ]; then
|
||||
return 1;
|
||||
fi
|
||||
|
||||
# We only support Bash 3.1+.
|
||||
# Note: BASH_VERSINFO is first available in Bash-2.0.
|
||||
if [[ -z "${BASH_VERSINFO-}" ]] || (( BASH_VERSINFO[0] < 3 || (BASH_VERSINFO[0] == 3 && BASH_VERSINFO[1] < 1) )); then
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Avoid duplicate inclusion
|
||||
if [[ -n "${bash_preexec_imported:-}" ]]; then
|
||||
return 0
|
||||
fi
|
||||
bash_preexec_imported="defined"
|
||||
|
||||
# WARNING: This variable is no longer used and should not be relied upon.
|
||||
# Use ${bash_preexec_imported} instead.
|
||||
# shellcheck disable=SC2034
|
||||
__bp_imported="${bash_preexec_imported}"
|
||||
|
||||
# Should be available to each precmd and preexec
|
||||
# functions, should they want it. $? and $_ are available as $? and $_, but
|
||||
# $PIPESTATUS is available only in a copy, $BP_PIPESTATUS.
|
||||
# TODO: Figure out how to restore PIPESTATUS before each precmd or preexec
|
||||
# function.
|
||||
__bp_last_ret_value="$?"
|
||||
BP_PIPESTATUS=("${PIPESTATUS[@]}")
|
||||
__bp_last_argument_prev_command="$_"
|
||||
|
||||
__bp_inside_precmd=0
|
||||
__bp_inside_preexec=0
|
||||
|
||||
# Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
|
||||
__bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
|
||||
|
||||
# Fails if any of the given variables are readonly
|
||||
# Reference https://stackoverflow.com/a/4441178
|
||||
__bp_require_not_readonly() {
|
||||
local var
|
||||
for var; do
|
||||
if ! ( unset "$var" 2> /dev/null ); then
|
||||
echo "bash-preexec requires write access to ${var}" >&2
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Remove ignorespace and or replace ignoreboth from HISTCONTROL
|
||||
# so we can accurately invoke preexec with a command from our
|
||||
# history even if it starts with a space.
|
||||
__bp_adjust_histcontrol() {
|
||||
local histcontrol
|
||||
histcontrol="${HISTCONTROL:-}"
|
||||
histcontrol="${histcontrol//ignorespace}"
|
||||
# Replace ignoreboth with ignoredups
|
||||
if [[ "$histcontrol" == *"ignoreboth"* ]]; then
|
||||
histcontrol="ignoredups:${histcontrol//ignoreboth}"
|
||||
fi;
|
||||
export HISTCONTROL="$histcontrol"
|
||||
}
|
||||
|
||||
# This variable describes whether we are currently in "interactive mode";
|
||||
# i.e. whether this shell has just executed a prompt and is waiting for user
|
||||
# input. It documents whether the current command invoked by the trace hook is
|
||||
# run interactively by the user; it's set immediately after the prompt hook,
|
||||
# and unset as soon as the trace hook is run.
|
||||
__bp_preexec_interactive_mode=""
|
||||
|
||||
# These arrays are used to add functions to be run before, or after, prompts.
|
||||
declare -a precmd_functions
|
||||
declare -a preexec_functions
|
||||
|
||||
# Trims leading and trailing whitespace from $2 and writes it to the variable
|
||||
# name passed as $1
|
||||
__bp_trim_whitespace() {
|
||||
local var=${1:?} text=${2:-}
|
||||
text="${text#"${text%%[![:space:]]*}"}" # remove leading whitespace characters
|
||||
text="${text%"${text##*[![:space:]]}"}" # remove trailing whitespace characters
|
||||
printf -v "$var" '%s' "$text"
|
||||
}
|
||||
|
||||
|
||||
# Trims whitespace and removes any leading or trailing semicolons from $2 and
|
||||
# writes the resulting string to the variable name passed as $1. Used for
|
||||
# manipulating substrings in PROMPT_COMMAND
|
||||
__bp_sanitize_string() {
|
||||
local var=${1:?} text=${2:-} sanitized
|
||||
__bp_trim_whitespace sanitized "$text"
|
||||
sanitized=${sanitized%;}
|
||||
sanitized=${sanitized#;}
|
||||
__bp_trim_whitespace sanitized "$sanitized"
|
||||
printf -v "$var" '%s' "$sanitized"
|
||||
}
|
||||
|
||||
# This function is installed as part of the PROMPT_COMMAND;
|
||||
# It sets a variable to indicate that the prompt was just displayed,
|
||||
# to allow the DEBUG trap to know that the next command is likely interactive.
|
||||
__bp_interactive_mode() {
|
||||
__bp_preexec_interactive_mode="on";
|
||||
}
|
||||
|
||||
|
||||
# This function is installed as part of the PROMPT_COMMAND.
|
||||
# It will invoke any functions defined in the precmd_functions array.
|
||||
__bp_precmd_invoke_cmd() {
|
||||
# Save the returned value from our last command, and from each process in
|
||||
# its pipeline. Note: this MUST be the first thing done in this function.
|
||||
# BP_PIPESTATUS may be unused, ignore
|
||||
# shellcheck disable=SC2034
|
||||
|
||||
__bp_last_ret_value="$?" BP_PIPESTATUS=("${PIPESTATUS[@]}")
|
||||
|
||||
# Don't invoke precmds if we are inside an execution of an "original
|
||||
# prompt command" by another precmd execution loop. This avoids infinite
|
||||
# recursion.
|
||||
if (( __bp_inside_precmd > 0 )); then
|
||||
return
|
||||
fi
|
||||
local __bp_inside_precmd=1
|
||||
|
||||
# Invoke every function defined in our function array.
|
||||
local precmd_function
|
||||
for precmd_function in "${precmd_functions[@]}"; do
|
||||
|
||||
# Only execute this function if it actually exists.
|
||||
# Test existence of functions with: declare -[Ff]
|
||||
if type -t "$precmd_function" 1>/dev/null; then
|
||||
__bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
|
||||
# Quote our function invocation to prevent issues with IFS
|
||||
"$precmd_function"
|
||||
fi
|
||||
done
|
||||
|
||||
__bp_set_ret_value "$__bp_last_ret_value"
|
||||
}
|
||||
|
||||
# Sets a return value in $?. We may want to get access to the $? variable in our
|
||||
# precmd functions. This is available for instance in zsh. We can simulate it in bash
|
||||
# by setting the value here.
|
||||
__bp_set_ret_value() {
|
||||
return ${1:+"$1"}
|
||||
}
|
||||
|
||||
__bp_in_prompt_command() {
|
||||
|
||||
local prompt_command_array IFS=$'\n;'
|
||||
read -rd '' -a prompt_command_array <<< "${PROMPT_COMMAND[*]:-}"
|
||||
|
||||
local trimmed_arg
|
||||
__bp_trim_whitespace trimmed_arg "${1:-}"
|
||||
|
||||
local command trimmed_command
|
||||
for command in "${prompt_command_array[@]:-}"; do
|
||||
__bp_trim_whitespace trimmed_command "$command"
|
||||
if [[ "$trimmed_command" == "$trimmed_arg" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# This function is installed as the DEBUG trap. It is invoked before each
|
||||
# interactive prompt display. Its purpose is to inspect the current
|
||||
# environment to attempt to detect if the current command is being invoked
|
||||
# interactively, and invoke 'preexec' if so.
|
||||
__bp_preexec_invoke_exec() {
|
||||
|
||||
# Save the contents of $_ so that it can be restored later on.
|
||||
# https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702
|
||||
__bp_last_argument_prev_command="${1:-}"
|
||||
# Don't invoke preexecs if we are inside of another preexec.
|
||||
if (( __bp_inside_preexec > 0 )); then
|
||||
return
|
||||
fi
|
||||
local __bp_inside_preexec=1
|
||||
|
||||
# Checks if the file descriptor is not standard out (i.e. '1')
|
||||
# __bp_delay_install checks if we're in test. Needed for bats to run.
|
||||
# Prevents preexec from being invoked for functions in PS1
|
||||
if [[ ! -t 1 && -z "${__bp_delay_install:-}" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
if [[ -n "${COMP_LINE:-}" ]]; then
|
||||
# We're in the middle of a completer. This obviously can't be
|
||||
# an interactively issued command.
|
||||
return
|
||||
fi
|
||||
if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then
|
||||
# We're doing something related to displaying the prompt. Let the
|
||||
# prompt set the title instead of me.
|
||||
return
|
||||
else
|
||||
# If we're in a subshell, then the prompt won't be re-displayed to put
|
||||
# us back into interactive mode, so let's not set the variable back.
|
||||
# In other words, if you have a subshell like
|
||||
# (sleep 1; sleep 2)
|
||||
# You want to see the 'sleep 2' as a set_command_title as well.
|
||||
if [[ 0 -eq "${BASH_SUBSHELL:-}" ]]; then
|
||||
__bp_preexec_interactive_mode=""
|
||||
fi
|
||||
fi
|
||||
|
||||
if __bp_in_prompt_command "${BASH_COMMAND:-}"; then
|
||||
# If we're executing something inside our prompt_command then we don't
|
||||
# want to call preexec. Bash prior to 3.1 can't detect this at all :/
|
||||
__bp_preexec_interactive_mode=""
|
||||
return
|
||||
fi
|
||||
|
||||
local this_command
|
||||
this_command=$(
|
||||
export LC_ALL=C
|
||||
HISTTIMEFORMAT='' builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
|
||||
)
|
||||
|
||||
# Sanity check to make sure we have something to invoke our function with.
|
||||
if [[ -z "$this_command" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# Invoke every function defined in our function array.
|
||||
local preexec_function
|
||||
local preexec_function_ret_value
|
||||
local preexec_ret_value=0
|
||||
for preexec_function in "${preexec_functions[@]:-}"; do
|
||||
|
||||
# Only execute each function if it actually exists.
|
||||
# Test existence of function with: declare -[fF]
|
||||
if type -t "$preexec_function" 1>/dev/null; then
|
||||
__bp_set_ret_value "${__bp_last_ret_value:-}"
|
||||
# Quote our function invocation to prevent issues with IFS
|
||||
"$preexec_function" "$this_command"
|
||||
preexec_function_ret_value="$?"
|
||||
if [[ "$preexec_function_ret_value" != 0 ]]; then
|
||||
preexec_ret_value="$preexec_function_ret_value"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Restore the last argument of the last executed command, and set the return
|
||||
# value of the DEBUG trap to be the return code of the last preexec function
|
||||
# to return an error.
|
||||
# If `extdebug` is enabled a non-zero return value from any preexec function
|
||||
# will cause the user's command not to execute.
|
||||
# Run `shopt -s extdebug` to enable
|
||||
__bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
|
||||
}
|
||||
|
||||
__bp_install() {
|
||||
# Exit if we already have this installed.
|
||||
if [[ "${PROMPT_COMMAND[*]:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
|
||||
return 1;
|
||||
fi
|
||||
|
||||
trap '__bp_preexec_invoke_exec "$_"' DEBUG
|
||||
|
||||
# Preserve any prior DEBUG trap as a preexec function
|
||||
local prior_trap
|
||||
# we can't easily do this with variable expansion. Leaving as sed command.
|
||||
# shellcheck disable=SC2001
|
||||
prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
|
||||
unset __bp_trap_string
|
||||
if [[ -n "$prior_trap" ]]; then
|
||||
eval '__bp_original_debug_trap() {
|
||||
'"$prior_trap"'
|
||||
}'
|
||||
preexec_functions+=(__bp_original_debug_trap)
|
||||
fi
|
||||
|
||||
# Adjust our HISTCONTROL Variable if needed.
|
||||
__bp_adjust_histcontrol
|
||||
|
||||
# Issue #25. Setting debug trap for subshells causes sessions to exit for
|
||||
# backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.
|
||||
#
|
||||
# Disabling this by default. It can be enabled by setting this variable.
|
||||
if [[ -n "${__bp_enable_subshells:-}" ]]; then
|
||||
|
||||
# Set so debug trap will work be invoked in subshells.
|
||||
set -o functrace > /dev/null 2>&1
|
||||
shopt -s extdebug > /dev/null 2>&1
|
||||
fi;
|
||||
|
||||
local existing_prompt_command
|
||||
# Remove setting our trap install string and sanitize the existing prompt command string
|
||||
existing_prompt_command="${PROMPT_COMMAND:-}"
|
||||
# Edge case of appending to PROMPT_COMMAND
|
||||
existing_prompt_command="${existing_prompt_command//$__bp_install_string/:}" # no-op
|
||||
existing_prompt_command="${existing_prompt_command//$'\n':$'\n'/$'\n'}" # remove known-token only
|
||||
existing_prompt_command="${existing_prompt_command//$'\n':;/$'\n'}" # remove known-token only
|
||||
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"
|
||||
if [[ "${existing_prompt_command:-:}" == ":" ]]; then
|
||||
existing_prompt_command=
|
||||
fi
|
||||
|
||||
# Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
|
||||
# actually entered something.
|
||||
PROMPT_COMMAND='__bp_precmd_invoke_cmd'
|
||||
PROMPT_COMMAND+=${existing_prompt_command:+$'\n'$existing_prompt_command}
|
||||
if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1) )); then
|
||||
PROMPT_COMMAND+=('__bp_interactive_mode')
|
||||
else
|
||||
# shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
|
||||
PROMPT_COMMAND+=$'\n__bp_interactive_mode'
|
||||
fi
|
||||
|
||||
# Add two functions to our arrays for convenience
|
||||
# of definition.
|
||||
precmd_functions+=(precmd)
|
||||
preexec_functions+=(preexec)
|
||||
|
||||
# Invoke our two functions manually that were added to $PROMPT_COMMAND
|
||||
__bp_precmd_invoke_cmd
|
||||
__bp_interactive_mode
|
||||
}
|
||||
|
||||
# Sets an installation string as part of our PROMPT_COMMAND to install
|
||||
# after our session has started. This allows bash-preexec to be included
|
||||
# at any point in our bash profile.
|
||||
__bp_install_after_session_init() {
|
||||
# bash-preexec needs to modify these variables in order to work correctly
|
||||
# if it can't, just stop the installation
|
||||
__bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return
|
||||
|
||||
local sanitized_prompt_command
|
||||
__bp_sanitize_string sanitized_prompt_command "${PROMPT_COMMAND:-}"
|
||||
if [[ -n "$sanitized_prompt_command" ]]; then
|
||||
# shellcheck disable=SC2178 # PROMPT_COMMAND is not an array in bash <= 5.0
|
||||
PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
|
||||
fi;
|
||||
# shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
|
||||
PROMPT_COMMAND+=${__bp_install_string}
|
||||
}
|
||||
|
||||
# Run our install so long as we're not delaying it.
|
||||
if [[ -z "${__bp_delay_install:-}" ]]; then
|
||||
__bp_install_after_session_init
|
||||
fi;
|
278
src/shell-integration/bash/ghostty.bash
Executable file → Normal file
278
src/shell-integration/bash/ghostty.bash
Executable file → Normal file
@ -1,217 +1,75 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# This is forked from Kitty's bash integration and is therefore licensed
|
||||
# under the same GPLv3 as Kitty:
|
||||
# https://github.com/kovidgoyal/kitty/blob/master/shell-integration/bash/kitty.bash
|
||||
# This is originally based on the recommended bash integration from
|
||||
# the semantic prompts proposal as well as some logic from Kitty's
|
||||
# bash integration.
|
||||
#
|
||||
# I'm not a bash expert so this probably has some major issues but for
|
||||
# my simple bash usage this is working. If a bash expert wants to
|
||||
# improve this please do!
|
||||
|
||||
if [[ "$-" != *i* ]] ; then builtin return; fi # check in interactive mode
|
||||
# We need to be in interactive mode and we need to have the Ghostty
|
||||
# resources dir set which also tells us we're running in Ghostty.
|
||||
if [[ "$-" != *i* ]] ; then builtin return; fi
|
||||
if [ -z "$GHOSTTY_RESOURCES_DIR" ]; then builtin return; fi
|
||||
|
||||
# Load the normal bash startup files
|
||||
if [[ -n "$GHOSTTY_BASH_INJECT" ]]; then
|
||||
builtin declare ghostty_bash_inject="$GHOSTTY_BASH_INJECT"
|
||||
builtin unset GHOSTTY_BASH_INJECT ENV
|
||||
if [[ -z "$HOME" ]]; then HOME=~; fi
|
||||
if [[ -z "$GHOSTTY_BASH_ETC_LOCATION" ]]; then GHOSTTY_BASH_ETC_LOCATION="/etc"; fi
|
||||
# Import bash-preexec, safe to do multiple times
|
||||
builtin source "$GHOSTTY_RESOURCES_DIR/shell-integration/bash/bash-preexec.sh"
|
||||
|
||||
_ghostty_sourceable() {
|
||||
[[ -f "$1" && -r "$1" ]] && builtin return 0; builtin return 1;
|
||||
}
|
||||
# This is set to 1 when we're executing a command so that we don't
|
||||
# send prompt marks multiple times.
|
||||
_ghostty_executing=""
|
||||
_ghostty_last_reported_cwd=""
|
||||
|
||||
if [[ "$ghostty_bash_inject" == *"posix"* ]]; then
|
||||
_ghostty_sourceable "$GHOSTTY_BASH_POSIX_ENV" && {
|
||||
builtin source "$GHOSTTY_BASH_POSIX_ENV"
|
||||
builtin export ENV="$GHOSTTY_BASH_POSIX_ENV"
|
||||
}
|
||||
else
|
||||
builtin set +o posix
|
||||
builtin shopt -u inherit_errexit 2>/dev/null # resetting posix does not clear this
|
||||
if [[ -n "$GHOSTTY_BASH_UNEXPORT_HISTFILE" ]]; then
|
||||
builtin export -n HISTFILE
|
||||
builtin unset GHOSTTY_BASH_UNEXPORT_HISTFILE
|
||||
fi
|
||||
|
||||
# See run_startup_files() in shell.c in the Bash source code
|
||||
if builtin shopt -q login_shell; then
|
||||
if [[ "$ghostty_bash_inject" != *"no-profile"* ]]; then
|
||||
_ghostty_sourceable "$GHOSTTY_BASH_ETC_LOCATION/profile" && builtin source "$GHOSTTY_BASH_ETC_LOCATION/profile"
|
||||
for _ghostty_i in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do
|
||||
_ghostty_sourceable "$_ghostty_i" && { builtin source "$_ghostty_i"; break; }
|
||||
done
|
||||
fi
|
||||
else
|
||||
if [[ "$ghostty_bash_inject" != *"no-rc"* ]]; then
|
||||
# Linux distros build bash with -DSYS_BASHRC. Unfortunately, there is
|
||||
# no way to to probe bash for it and different distros use different files
|
||||
# Arch, Debian, Ubuntu use /etc/bash.bashrc
|
||||
# Fedora uses /etc/bashrc sourced from ~/.bashrc instead of SYS_BASHRC
|
||||
# Void Linux uses /etc/bash/bashrc
|
||||
for _ghostty_i in "$GHOSTTY_BASH_ETC_LOCATION/bash.bashrc" "$GHOSTTY_BASH_ETC_LOCATION/bash/bashrc" ; do
|
||||
_ghostty_sourceable "$_ghostty_i" && { builtin source "$_ghostty_i"; break; }
|
||||
done
|
||||
if [[ -z "$GHOSTTY_BASH_RCFILE" ]]; then GHOSTTY_BASH_RCFILE="$HOME/.bashrc"; fi
|
||||
_ghostty_sourceable "$GHOSTTY_BASH_RCFILE" && builtin source "$GHOSTTY_BASH_RCFILE"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
builtin unset GHOSTTY_BASH_RCFILE GHOSTTY_BASH_POSIX_ENV GHOSTTY_BASH_ETC_LOCATION
|
||||
builtin unset -f _ghostty_sourceable
|
||||
builtin unset _ghostty_i ghostty_bash_inject
|
||||
fi
|
||||
|
||||
|
||||
if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then
|
||||
builtin printf "%s\n" "Bash version ${BASH_VERSION} too old, Ghostty shell integration disabled" > /dev/stderr
|
||||
builtin return
|
||||
fi
|
||||
|
||||
if [[ "${_ghostty_prompt[sourced]}" == "y" ]]; then
|
||||
# we have already run
|
||||
builtin return
|
||||
fi
|
||||
|
||||
# this is defined outside _ghostty_main to make it global without using declare -g
|
||||
# which is not available on older bash
|
||||
builtin declare -A _ghostty_prompt
|
||||
_ghostty_prompt=(
|
||||
[cursor]='y' [title]='y' [mark]='y' [cwd]='y' [ps0]='' [ps0_suffix]='' [ps1]='' [ps1_suffix]='' [ps2]=''
|
||||
[hostname_prefix]='' [sourced]='y' [last_reported_cwd]=''
|
||||
)
|
||||
|
||||
_ghostty_main() {
|
||||
_ghostty_set_mark() {
|
||||
_ghostty_prompt["${1}_mark"]="\[\e]133;k;${1}_ghostty\a\]"
|
||||
}
|
||||
|
||||
_ghostty_set_mark start
|
||||
_ghostty_set_mark end
|
||||
_ghostty_set_mark start_secondary
|
||||
_ghostty_set_mark end_secondary
|
||||
_ghostty_set_mark start_suffix
|
||||
_ghostty_set_mark end_suffix
|
||||
builtin unset -f _ghostty_set_mark
|
||||
_ghostty_prompt[secondary_prompt]="\n${_ghostty_prompt[start_secondary_mark]}\[\e]133;A;k=s\a\]${_ghostty_prompt[end_secondary_mark]}"
|
||||
|
||||
_ghostty_prompt_command() {
|
||||
# we first remove any previously added ghostty code from the prompt variables and then add
|
||||
# it back, to ensure we have only a single instance
|
||||
if [[ -n "${_ghostty_prompt[ps0]}" ]]; then
|
||||
PS0=${PS0//\\\[\\e\]133;k;start_ghostty\\a\\\]*end_ghostty\\a\\\]}
|
||||
PS0="${_ghostty_prompt[ps0]}$PS0"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps0_suffix]}" ]]; then
|
||||
PS0=${PS0//\\\[\\e\]133;k;start_suffix_ghostty\\a\\\]*end_suffix_ghostty\\a\\\]}
|
||||
PS0="${PS0}${_ghostty_prompt[ps0_suffix]}"
|
||||
fi
|
||||
# restore PS1 to its pristine state without our additions
|
||||
if [[ -n "${_ghostty_prompt[ps1]}" ]]; then
|
||||
PS1=${PS1//\\\[\\e\]133;k;start_ghostty\\a\\\]*end_ghostty\\a\\\]}
|
||||
PS1=${PS1//\\\[\\e\]133;k;start_secondary_ghostty\\a\\\]*end_secondary_ghostty\\a\\\]}
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps1_suffix]}" ]]; then
|
||||
PS1=${PS1//\\\[\\e\]133;k;start_suffix_ghostty\\a\\\]*end_suffix_ghostty\\a\\\]}
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps1]}" ]]; then
|
||||
if [[ "${_ghostty_prompt[mark]}" == "y" && ( "${PS1}" == *"\n"* || "${PS1}" == *$'\n'* ) ]]; then
|
||||
builtin local oldval
|
||||
oldval=$(builtin shopt -p extglob)
|
||||
builtin shopt -s extglob
|
||||
# bash does not redraw the leading lines in a multiline prompt so
|
||||
# mark the last line as a secondary prompt. Otherwise on resize the
|
||||
# lines before the last line will be erased by ghostty.
|
||||
# the first part removes everything from the last \n onwards
|
||||
# the second part appends a newline with the secondary marking
|
||||
# the third part appends everything after the last newline
|
||||
PS1=${PS1%@('\n'|$'\n')*}${_ghostty_prompt[secondary_prompt]}${PS1##*@('\n'|$'\n')}
|
||||
builtin eval "$oldval"
|
||||
fi
|
||||
PS1="${_ghostty_prompt[ps1]}$PS1"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps1_suffix]}" ]]; then
|
||||
PS1="${PS1}${_ghostty_prompt[ps1_suffix]}"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps2]}" ]]; then
|
||||
PS2=${PS2//\\\[\\e\]133;k;start_ghostty\\a\\\]*end_ghostty\\a\\\]}
|
||||
PS2="${_ghostty_prompt[ps2]}$PS2"
|
||||
fi
|
||||
|
||||
if [[ "${_ghostty_prompt[cwd]}" == "y" ]]; then
|
||||
# unfortunately bash provides no hooks to detect cwd changes
|
||||
# in particular this means cwd reporting will not happen for a
|
||||
# command like cd /test && cat. PS0 is evaluated before cd is run.
|
||||
if [[ "${_ghostty_prompt[last_reported_cwd]}" != "$PWD" ]]; then
|
||||
_ghostty_prompt[last_reported_cwd]="$PWD"
|
||||
builtin printf "\e]7;kitty-shell-cwd://%s%s\a" "$HOSTNAME" "$PWD"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ "${_ghostty_prompt[cursor]}" == "y" ]]; then
|
||||
_ghostty_prompt[ps1_suffix]+="\[\e[5 q\]" # blinking bar cursor
|
||||
_ghostty_prompt[ps0_suffix]+="\[\e[0 q\]" # blinking default cursor
|
||||
fi
|
||||
|
||||
if [[ "${_ghostty_prompt[title]}" == "y" ]]; then
|
||||
# see https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html#Controlling-the-Prompt
|
||||
# we use suffix here because some distros add title setting to their bashrc files by default
|
||||
_ghostty_prompt[ps1_suffix]+="\[\e]2;${_ghostty_prompt[hostname_prefix]}\w\a\]"
|
||||
_ghostty_get_current_command() {
|
||||
builtin local last_cmd
|
||||
last_cmd=$(HISTTIMEFORMAT= builtin history 1)
|
||||
last_cmd="${last_cmd#*[[:digit:]]*[[:space:]]}" # remove leading history number
|
||||
last_cmd="${last_cmd#"${last_cmd%%[![:space:]]*}"}" # remove remaining leading whitespace
|
||||
builtin printf "\e]2;%s%s\a" "${_ghostty_prompt[hostname_prefix]@P}" "${last_cmd//[[:cntrl:]]}" # remove any control characters
|
||||
}
|
||||
_ghostty_prompt[ps0_suffix]+='$(_ghostty_get_current_command)'
|
||||
fi
|
||||
|
||||
if [[ "${_ghostty_prompt[mark]}" == "y" ]]; then
|
||||
_ghostty_prompt[ps1]+="\[\e]133;A\a\]"
|
||||
_ghostty_prompt[ps2]+="\[\e]133;A;k=s\a\]"
|
||||
_ghostty_prompt[ps0]+="\[\e]133;C\a\]"
|
||||
fi
|
||||
|
||||
# wrap our prompt additions in markers we can use to remove them using
|
||||
# bash's anemic pattern substitution
|
||||
if [[ -n "${_ghostty_prompt[ps0]}" ]]; then
|
||||
_ghostty_prompt[ps0]="${_ghostty_prompt[start_mark]}${_ghostty_prompt[ps0]}${_ghostty_prompt[end_mark]}"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps0_suffix]}" ]]; then
|
||||
_ghostty_prompt[ps0_suffix]="${_ghostty_prompt[start_suffix_mark]}${_ghostty_prompt[ps0_suffix]}${_ghostty_prompt[end_suffix_mark]}"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps1]}" ]]; then
|
||||
_ghostty_prompt[ps1]="${_ghostty_prompt[start_mark]}${_ghostty_prompt[ps1]}${_ghostty_prompt[end_mark]}"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps1_suffix]}" ]]; then
|
||||
_ghostty_prompt[ps1_suffix]="${_ghostty_prompt[start_suffix_mark]}${_ghostty_prompt[ps1_suffix]}${_ghostty_prompt[end_suffix_mark]}"
|
||||
fi
|
||||
if [[ -n "${_ghostty_prompt[ps2]}" ]]; then
|
||||
_ghostty_prompt[ps2]="${_ghostty_prompt[start_mark]}${_ghostty_prompt[ps2]}${_ghostty_prompt[end_mark]}"
|
||||
fi
|
||||
# BASH aborts the entire script when doing unset with failglob set, somebody should report this upstream
|
||||
oldval=$(builtin shopt -p failglob)
|
||||
builtin shopt -u failglob
|
||||
builtin unset _ghostty_prompt[start_mark] _ghostty_prompt[end_mark] _ghostty_prompt[start_suffix_mark] _ghostty_prompt[end_suffix_mark] _ghostty_prompt[start_secondary_mark] _ghostty_prompt[end_secondary_mark]
|
||||
builtin eval "$oldval"
|
||||
|
||||
# install our prompt command, using an array if it is unset or already an array,
|
||||
# otherwise append a string. We check if _ghostty_prompt_command exists as some shell
|
||||
# scripts stupidly export PROMPT_COMMAND making it inherited by all programs launched
|
||||
# from the shell
|
||||
builtin local pc
|
||||
pc='builtin declare -F _ghostty_prompt_command > /dev/null 2> /dev/null && _ghostty_prompt_command'
|
||||
if [[ -z "${PROMPT_COMMAND}" ]]; then
|
||||
PROMPT_COMMAND=([0]="$pc")
|
||||
elif [[ $(builtin declare -p PROMPT_COMMAND 2> /dev/null) =~ 'declare -a PROMPT_COMMAND' ]]; then
|
||||
PROMPT_COMMAND+=("$pc")
|
||||
else
|
||||
builtin local oldval
|
||||
oldval=$(builtin shopt -p extglob)
|
||||
builtin shopt -s extglob
|
||||
PROMPT_COMMAND="${PROMPT_COMMAND%%+([[:space:]])}"
|
||||
PROMPT_COMMAND="${PROMPT_COMMAND%%+(;)}"
|
||||
builtin eval "$oldval"
|
||||
PROMPT_COMMAND+="; $pc"
|
||||
fi
|
||||
function __ghostty_get_current_command() {
|
||||
builtin local last_cmd
|
||||
last_cmd=$(HISTTIMEFORMAT= builtin history 1)
|
||||
last_cmd="${last_cmd#*[[:digit:]]*[[:space:]]}" # remove leading history number
|
||||
last_cmd="${last_cmd#"${last_cmd%%[![:space:]]*}"}" # remove remaining leading whitespace
|
||||
builtin printf "\e]2;%s\a" "${last_cmd//[[:cntrl:]]}" # remove any control characters
|
||||
}
|
||||
_ghostty_main
|
||||
builtin unset -f _ghostty_main
|
||||
|
||||
function __ghostty_precmd() {
|
||||
local ret="$?"
|
||||
if test "$_ghostty_executing" != "0"; then
|
||||
_GHOSTTY_SAVE_PS1="$PS1"
|
||||
_GHOSTTY_SAVE_PS2="$PS2"
|
||||
|
||||
# Marks
|
||||
PS1=$PS1'\e]133;B\a'
|
||||
PS2=$PS2'\e]133;B\a'
|
||||
|
||||
# Cursor
|
||||
PS1=$PS1'\e[5 q'
|
||||
PS0=$PS0'\e[0 q'
|
||||
|
||||
# Command
|
||||
PS0=$PS0'$(__ghostty_get_current_command)'
|
||||
PS1=$PS1'\e]2;$PWD\a'
|
||||
fi
|
||||
|
||||
if test "$_ghostty_executing" != ""; then
|
||||
builtin printf "\033]133;D;%s;aid=%s\007" "$ret" "$BASHPID"
|
||||
fi
|
||||
|
||||
# unfortunately bash provides no hooks to detect cwd changes
|
||||
# in particular this means cwd reporting will not happen for a
|
||||
# command like cd /test && cat. PS0 is evaluated before cd is run.
|
||||
if [[ "$_ghostty_last_reported_cwd" != "$PWD" ]]; then
|
||||
_ghostty_last_reported_cwd="$PWD"
|
||||
builtin printf "\e]7;kitty-shell-cwd://%s%s\a" "$HOSTNAME" "$PWD"
|
||||
fi
|
||||
|
||||
builtin printf "\033]133;A;aid=%s\007" "$BASHPID"
|
||||
_ghostty_executing=0
|
||||
}
|
||||
|
||||
function __ghostty_preexec() {
|
||||
PS1="$_GHOSTTY_SAVE_PS1"
|
||||
PS2="$_GHOSTTY_SAVE_PS2"
|
||||
builtin printf "\033]133;C;\007"
|
||||
_ghostty_executing=1
|
||||
}
|
||||
|
||||
preexec_functions+=(__ghostty_preexec)
|
||||
precmd_functions+=(__ghostty_precmd)
|
||||
|
Reference in New Issue
Block a user