From 9ac2a92906415f7c91e6d74fd3f94cddb967422e Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 2 Oct 2022 10:36:05 +0900 Subject: [PATCH] plugins/xterm: Refactor * plugins/xterm: Add header line as an OMB plugin * plugins/xterm: Add link to the upstream version * plugins/xterm: Add the list of the configuration variables * plugins/xterm: Do not rely on the function "cite" * plugins/xterm: Add dependency to bash-preexec * plugins/xterm: Use "function func" to avoid aliasing * plugins/xterm: Namespacing function names * plugins/xterm: Namespacing configuration variables * plugins/xterm (_omb_plugin_xterm_set_title): Avoid expanding escapes in directory names, etc. * plugins/xterm: Always enable precmd/preexec because the plugin should be enabled by the users' decision * plugins/xterm: Do no unneccessarily add braces around ${var} * plugins/xterm: Do no uneccessarily quote words * plugins/xterm: Add README --- plugins/xterm/README.md | 3 ++ plugins/xterm/xterm.plugin.bash | 75 ++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 plugins/xterm/README.md diff --git a/plugins/xterm/README.md b/plugins/xterm/README.md new file mode 100644 index 0000000..eca3b5f --- /dev/null +++ b/plugins/xterm/README.md @@ -0,0 +1,3 @@ +# Xterm plugin + +This plugin sets the xterm title for the current directory and the currently running command. diff --git a/plugins/xterm/xterm.plugin.bash b/plugins/xterm/xterm.plugin.bash index 740460e..3dfde12 100644 --- a/plugins/xterm/xterm.plugin.bash +++ b/plugins/xterm/xterm.plugin.bash @@ -1,45 +1,52 @@ -# shellcheck shell=bash -cite about-plugin -about-plugin 'automatically set your xterm title with host and location info' +#! bash oh-my-bash.module +# Description: automatically set your xterm title with host and location info' +# Source: https://github.com/Bash-it/bash-it/blob/bf2034d13d/plugins/available/xterm.plugin.bash +# +# @var[opt] PROMPT_CHAR ... This variable is shared with powerline +# @var[opt] OMB_PLUGIN_XTERM_SHORT_TERM_LINE +# @var[opt] OMB_PLUGIN_XTERM_SHORT_USER +# @var[opt] OMB_PLUGIN_XTERM_SHORT_HOSTNAME +# -_short-dirname() { - local dir_name="${PWD/~/\~}" - if [[ "${SHORT_TERM_LINE:-}" == true && "${#dir_name}" -gt 8 ]]; then - echo "${dir_name##*/}" - else - echo "${dir_name}" - fi +_omb_module_require plugin:bash-preexec + +function _omb_plugin_xterm_short_dirname { + local dir_name=${PWD/~/\~} + if [[ ${OMB_PLUGIN_XTERM_SHORT_TERM_LINE-} == true ]] && ((${#dir_name} > 8)); then + dir_name=${dir_name##*/} + fi + echo "$dir_name" } -_short-command() { - local input_command="$*" - if [[ "${SHORT_TERM_LINE:-}" == true && "${#input_command}" -gt 8 ]]; then - echo "${input_command%% *}" - else - echo "${input_command}" - fi +function _omb_plugin_xterm_short_command { + local input_command="$*" + if [[ ${OMB_PLUGIN_XTERM_SHORT_TERM_LINE-} == true ]] ((${#input_command} > 8)); then + input_command=${input_command%% *} + fi + echo "$input_command" } -set_xterm_title() { - local title="${1:-}" - echo -ne "\033]0;${title}\007" +function _omb_plugin_xterm_set_title { + local title=${1-} + printf '\e]0;%s\e\\' "$title" } -precmd_xterm_title() { - set_xterm_title "${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}} $(_short-dirname) ${PROMPT_CHAR:-\$}" +function _omb_plugin_xterm_precmd_title { + local user=${OMB_PLUGIN_XTERM_SHORT_USER:-$USER} + local host=${OMB_PLUGIN_XTERM_SHORT_HOSTNAME:-$HOSTNAME} + _omb_plugin_xterm_set_title "$user@$host $(_omb_plugin_xterm_short_dirname) ${PROMPT_CHAR:-\$}" } -preexec_xterm_title() { - local command_line="${BASH_COMMAND:-${1:-}}" - local directory_name short_command - directory_name="$(_short-dirname)" - short_command="$(_short-command "${command_line}")" - set_xterm_title "${short_command} {${directory_name}} (${SHORT_USER:-${USER}}@${SHORT_HOSTNAME:-${HOSTNAME}})" +function _omb_plugin_xterm_preexec_title { + local command_line=${BASH_COMMAND:-${1:-}} + local directory_name=$(_omb_plugin_xterm_short_dirname) + local short_command=$(_omb_plugin_xterm_short_command "$command_line") + local user=${OMB_PLUGIN_XTERM_SHORT_USER:-$USER} + local host=${OMB_PLUGIN_XTERM_SHORT_HOSTNAME:-$HOSTNAME} + _omb_plugin_xterm_set_title "$short_command {$directory_name} ($user@$host)" } -case "${TERM:-dumb}" in - xterm* | rxvt*) - precmd_functions+=(precmd_xterm_title) - preexec_functions+=(preexec_xterm_title) - ;; -esac +function set_xterm_title { _omb_plugin_xterm_set_title "$@"; } + +precmd_functions+=(_omb_plugin_xterm_precmd_title) +preexec_functions+=(_omb_plugin_xterm_preexec_title)