diff --git a/CodeEdit.xcodeproj/project.pbxproj b/CodeEdit.xcodeproj/project.pbxproj index 2247fb80cf..dbb3b7faf8 100644 --- a/CodeEdit.xcodeproj/project.pbxproj +++ b/CodeEdit.xcodeproj/project.pbxproj @@ -62,6 +62,8 @@ 2B7AC06B282452FB0082A5B8 /* Media.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2B7AC06A282452FB0082A5B8 /* Media.xcassets */; }; 2BE487EF28245162003F3F64 /* FinderSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BE487EE28245162003F3F64 /* FinderSync.swift */; }; 2BE487F428245162003F3F64 /* OpenWithCodeEdit.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 2BE487EC28245162003F3F64 /* OpenWithCodeEdit.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 3E0196732A3921AC002648D8 /* codeedit_shell_integration.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */; }; + 3E01967A2A392B45002648D8 /* codeedit_shell_integration.bash in Resources */ = {isa = PBXBuildFile; fileRef = 3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */; }; 474397C52893AC4B00518C8C /* codeedit-midnight.json in Resources */ = {isa = PBXBuildFile; fileRef = 474397C42893AC4B00518C8C /* codeedit-midnight.json */; }; 4E7F066629602E7B00BB3C12 /* CodeEditSplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */; }; 4EE96ECB2960565E00FFBEA8 /* DocumentsUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */; }; @@ -517,6 +519,8 @@ 2BE487EC28245162003F3F64 /* OpenWithCodeEdit.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = OpenWithCodeEdit.appex; sourceTree = BUILT_PRODUCTS_DIR; }; 2BE487EE28245162003F3F64 /* FinderSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FinderSync.swift; sourceTree = ""; }; 2BE487F028245162003F3F64 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = codeedit_shell_integration.zsh; sourceTree = ""; }; + 3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = codeedit_shell_integration.bash; sourceTree = ""; }; 474397C42893AC4B00518C8C /* codeedit-midnight.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "codeedit-midnight.json"; sourceTree = ""; }; 4E7F066529602E7B00BB3C12 /* CodeEditSplitViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeEditSplitViewController.swift; sourceTree = ""; }; 4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentsUnitTests.swift; sourceTree = ""; }; @@ -1099,6 +1103,15 @@ path = OpenWithCodeEdit; sourceTree = ""; }; + 3E0196712A392170002648D8 /* ShellIntegration */ = { + isa = PBXGroup; + children = ( + 3E0196792A392B45002648D8 /* codeedit_shell_integration.bash */, + 3E0196722A3921AC002648D8 /* codeedit_shell_integration.zsh */, + ); + path = ShellIntegration; + sourceTree = ""; + }; 4EE96EC82960562000FFBEA8 /* Documents */ = { isa = PBXGroup; children = ( @@ -2217,6 +2230,7 @@ B658FB2E27DA9E0F00EA4DBD /* CodeEdit */ = { isa = PBXGroup; children = ( + 3E0196712A392170002648D8 /* ShellIntegration */, 5831E3C52933E6CB00D5A6D2 /* Features */, D7211D4427E066D4008F2ED7 /* Localization */, B658FB3527DA9E1000EA4DBD /* Preview Content */, @@ -2594,8 +2608,10 @@ 283BDCBD2972EEBD002AFF81 /* Package.resolved in Resources */, B658FB3727DA9E1000EA4DBD /* Preview Assets.xcassets in Resources */, 28A51006281701B40087B0CC /* codeedit-github-dark.json in Resources */, + 3E0196732A3921AC002648D8 /* codeedit_shell_integration.zsh in Resources */, 28F43DE029738792008BBA45 /* codeedit-solarized-dark.json in Resources */, 58A5DFA529339F6400D1BD5D /* default_keybindings.json in Resources */, + 3E01967A2A392B45002648D8 /* codeedit_shell_integration.bash in Resources */, 28A51002281673530087B0CC /* codeedit-xcode-light.json in Resources */, 28A51005281701B40087B0CC /* codeedit-github-light.json in Resources */, D7211D4727E06BFE008F2ED7 /* Localizable.strings in Resources */, diff --git a/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalTab.swift b/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalTab.swift index 061f9aa442..e27929a913 100644 --- a/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalTab.swift +++ b/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalTab.swift @@ -21,8 +21,22 @@ struct DebugAreaTerminalTab: View { private var isFocused: Bool var body: some View { + var terminalTitle = Binding( + get: { + self.terminal.title + }, set: { + if $0.trimmingCharacters(in: .whitespaces) == "" && !isFocused { + self.terminal.title = self.terminal.terminalTitle + self.terminal.customTitle = false + } else { + self.terminal.title = $0 + self.terminal.customTitle = true + } + } + ) + Label { - TextField("Name", text: $terminal.title) + TextField("Name", text: terminalTitle) .focused($isFocused) .padding(.leading, -8) .background { diff --git a/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalView.swift b/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalView.swift index 1d344d75b0..4654f63b7b 100644 --- a/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalView.swift +++ b/CodeEdit/Features/DebugArea/Views/DebugAreaTerminalView.swift @@ -11,13 +11,17 @@ struct DebugAreaTerminal: Identifiable, Equatable { var id: UUID var url: URL? var title: String + var terminalTitle: String var shell: String + var customTitle: Bool init(id: UUID, url: URL, title: String, shell: String) { self.id = id self.title = title + self.terminalTitle = title self.url = url self.shell = shell + self.customTitle = false } } @@ -107,7 +111,10 @@ struct DebugAreaTerminalView: View { title: String? = nil ) { if let newTitle = title { - terminal.title = newTitle + if !terminal.customTitle { + terminal.title = newTitle + } + terminal.terminalTitle = newTitle } } diff --git a/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView+Coordinator.swift b/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView+Coordinator.swift index f180c365d8..1f35e59fee 100644 --- a/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView+Coordinator.swift +++ b/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView+Coordinator.swift @@ -14,8 +14,11 @@ extension TerminalEmulatorView { @State private var url: URL - init(url: URL) { + public var onTitleChange: (_ title: String) -> Void + + init(url: URL, onTitleChange: @escaping (_ title: String) -> Void) { self._url = .init(wrappedValue: url) + self.onTitleChange = onTitleChange super.init() } @@ -23,7 +26,9 @@ extension TerminalEmulatorView { func sizeChanged(source: LocalProcessTerminalView, newCols: Int, newRows: Int) {} - func setTerminalTitle(source: LocalProcessTerminalView, title: String) {} + func setTerminalTitle(source: LocalProcessTerminalView, title: String) { + onTitleChange(title) + } func processTerminated(source: TerminalView, exitCode: Int32?) { guard let exitCode else { diff --git a/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView.swift b/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView.swift index 4371264c86..c59c535ca2 100644 --- a/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView.swift +++ b/CodeEdit/Features/TerminalEmulator/TerminalEmulatorView.swift @@ -92,6 +92,15 @@ struct TerminalEmulatorView: NSViewRepresentable { } } + private func setupShellTitle(shell: String) { + if let shellSetupScript = Bundle.main.url(forResource: "codeedit_shell_integration", withExtension: shell) { + let scriptPath = (shellSetupScript.absoluteString[7.. CursorStyle { let blink = terminalSettings.cursorBlink switch terminalSettings.cursorStyle { @@ -195,7 +204,9 @@ struct TerminalEmulatorView: NSViewRepresentable { terminal.getTerminal().silentLog = true if TerminalEmulatorView.lastTerminal[url.path] == nil { let shell = getShell() - let shellIdiom = "-" + NSString(string: shell).lastPathComponent + let shellName = NSString(string: shell).lastPathComponent + onTitleChange(shellName) + let shellIdiom = "-" + shellName // changes working directory to project root // TODO: Get rid of FileManager shared instance to prevent problems @@ -214,6 +225,8 @@ struct TerminalEmulatorView: NSViewRepresentable { terminal.cursorStyleChanged(source: terminal.getTerminal(), newStyle: getTerminalCursor()) terminal.layer?.backgroundColor = .clear terminal.optionAsMetaKey = optionAsMeta + + setupShellTitle(shell: shellName) } terminal.appearance = colorAppearance scroller?.isHidden = true @@ -251,6 +264,6 @@ struct TerminalEmulatorView: NSViewRepresentable { } func makeCoordinator() -> Coordinator { - Coordinator(url: url) + Coordinator(url: url, onTitleChange: onTitleChange) } } diff --git a/CodeEdit/ShellIntegration/codeedit_shell_integration.bash b/CodeEdit/ShellIntegration/codeedit_shell_integration.bash new file mode 100644 index 0000000000..236547b1c3 --- /dev/null +++ b/CodeEdit/ShellIntegration/codeedit_shell_integration.bash @@ -0,0 +1,419 @@ +#!/bin/sh + +# codeedit_shell_intergration.bash +# CodeEdit +# +# Created by Qian Qian "Cubik" (@Cubik65536) on 2023-06-13. +# +# This script is used to configure bash shells +# so the terminal title would be setted properly +# with shell name or program's command name +# +# bash-preexec.sh (https://github.com/rcaloras/bash-preexec, licensed under MIT) +# is used so we can use ZSH-like 'preexec' and 'precmd' functions +# + +# Wrap bash-preexec.sh in a function so that, if it exits early due to having +# been sourced elsewhere, it doesn't exit our entire script. +_install_bash_preexec () { + +# -- BEGIN BASH-PREEXEC.SH -- +# 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; +# -- END BASH-PREEXEC.SH -- + +} + +_install_bash_preexec +unset -f _install_bash_preexec + +# -- BEGIN CODEEDIT CUSTOMIZATIONS -- +__codeedit_preexec() { + echo -ne "\033]0;${1}\007" +} + +__codeedit_precmd() { + echo -ne "\033]0;bash\007" +} + +preexec_functions+=(__codeedit_preexec) +precmd_functions+=(__codeedit_precmd) +# -- END CODEEDIT CUSTOMIZATIONS -- diff --git a/CodeEdit/ShellIntegration/codeedit_shell_integration.zsh b/CodeEdit/ShellIntegration/codeedit_shell_integration.zsh new file mode 100644 index 0000000000..7e5834a3cf --- /dev/null +++ b/CodeEdit/ShellIntegration/codeedit_shell_integration.zsh @@ -0,0 +1,24 @@ +#!/bin/zsh + +# codeedit-shell_Integration.zsh +# CodeEdit +# +# Created by Qian Qian "Cubik" (@Cubik65536) on 2023-06-13. +# +# This script is used to configure zsh/OhMyZsh shells +# so the terminal title would be setted properly +# with shell name or program's command name +# + +autoload -Uz add-zsh-hook + +__codeedit_preexec() { + echo -n "\033]0;${1}\007" +} + +__codeedit_precmd() { + echo -n "\033]0;zsh\007" +} + +add-zsh-hook preexec __codeedit_preexec +add-zsh-hook precmd __codeedit_precmd