style(bash): use consistent coding style (#1528)

* style(bash): make indentation consistent

Initially, in this file, the first level is indented by four spaces,
and additional levels are indented by adding two spaces.  However,
this does not seem intentional because the other files, such as
atuin.zsh, are consistently indented by four spaces for any levels.
The indentation was gradually fixed to use four spaces when the
relevant code is updated, but there are still remaining parts using
two spaces.  In this patch, the remaining parts are updated to use the
consistent indentation of four spaces.

* style(bash): remove extra quotations on rhs of assignments

On the right-hand sides of assignments, the quoting of the variable
expansions are not needed because they are not subject to the word
splitting and the pathname expansions.

* style(bash): strip `{` and `}` from `${var}` when not needeed

* style(bash): do not use unnecessary quoting in the conditional commands

In the conditional commands [[ ... ]], the words are not subject to
the word splitting and the pathname expansions, so we do not need to
quote the expansions except for the right-hand sides of ==, !=, and
=~, where the quoting has a special meaning.  In the first place, the
syntax [[ .. ]] is introduced to resolve the issue of the quoting, so
it is natural to use the unquoted form inside [[ ... ]] by default.
In this patch, we use the unquoted form of expansions.

* style(bash): prefer [[ $a && ! $b ]] to [[ -n $a && -z $b ]]

* style(bash): put "then" in the same line as "if"

This is also the format that Bash outputs with `bash --pretty-print
FILE` or `declare -f FUNC`.
This commit is contained in:
Koichi Murase 2024-01-10 05:42:27 +09:00 committed by GitHub
parent 3a0e070e74
commit f63f24699e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,24 +14,24 @@ __atuin_preexec() {
local id local id
id=$(atuin history start -- "$1") id=$(atuin history start -- "$1")
export ATUIN_HISTORY_ID="${id}" export ATUIN_HISTORY_ID=$id
} }
__atuin_precmd() { __atuin_precmd() {
local EXIT="$?" local EXIT=$?
[[ -z "${ATUIN_HISTORY_ID}" ]] && return [[ ! $ATUIN_HISTORY_ID ]] && return
local duration="" local duration=""
# shellcheck disable=SC2154,SC2309 # shellcheck disable=SC2154,SC2309
if [[ -n "${BLE_ATTACHED-}" && _ble_bash -ge 50000 && -n "${_ble_exec_time_ata-}" ]]; then if [[ ${BLE_ATTACHED-} && _ble_bash -ge 50000 && ${_ble_exec_time_ata-} ]]; then
# We use the high-resolution duration based on EPOCHREALTIME (bash >= # We use the high-resolution duration based on EPOCHREALTIME (bash >=
# 5.0) that is recorded by ble.sh. The shell variable # 5.0) that is recorded by ble.sh. The shell variable
# `_ble_exec_time_ata` contains the execution time in microseconds. # `_ble_exec_time_ata` contains the execution time in microseconds.
duration=${_ble_exec_time_ata}000 duration=${_ble_exec_time_ata}000
fi fi
(ATUIN_LOG=error atuin history end --exit "${EXIT}" ${duration:+--duration "$duration"} -- "${ATUIN_HISTORY_ID}" &) >/dev/null 2>&1 (ATUIN_LOG=error atuin history end --exit "$EXIT" ${duration:+--duration "$duration"} -- "$ATUIN_HISTORY_ID" &) >/dev/null 2>&1
export ATUIN_HISTORY_ID="" export ATUIN_HISTORY_ID=""
} }
@ -71,38 +71,39 @@ __atuin_accept_line() {
local __atuin_preexec_function_ret_value local __atuin_preexec_function_ret_value
local __atuin_preexec_ret_value=0 local __atuin_preexec_ret_value=0
for __atuin_preexec_function in "${preexec_functions[@]:-}"; do for __atuin_preexec_function in "${preexec_functions[@]:-}"; do
if type -t "$__atuin_preexec_function" 1>/dev/null; then if type -t "$__atuin_preexec_function" 1>/dev/null; then
__atuin_set_ret_value "${__bp_last_ret_value:-}" __atuin_set_ret_value "${__bp_last_ret_value:-}"
"$__atuin_preexec_function" "$__atuin_command" "$__atuin_preexec_function" "$__atuin_command"
__atuin_preexec_function_ret_value="$?" __atuin_preexec_function_ret_value=$?
if [[ "$__atuin_preexec_function_ret_value" != 0 ]]; then if [[ $__atuin_preexec_function_ret_value != 0 ]]; then
__atuin_preexec_ret_value="$__atuin_preexec_function_ret_value" __atuin_preexec_ret_value=$__atuin_preexec_function_ret_value
fi
fi fi
fi
done done
# If extdebug is turned on and any preexec function returns non-zero # If extdebug is turned on and any preexec function returns non-zero
# exit status, we do not run the user command. # exit status, we do not run the user command.
if ! { shopt -q extdebug && ((__atuin_preexec_ret_value)); }; then if ! { shopt -q extdebug && ((__atuin_preexec_ret_value)); }; then
# Juggle the terminal settings so that the command can be interacted with # Juggle the terminal settings so that the command can be interacted
local __atuin_stty_backup # with
__atuin_stty_backup=$(stty -g) local __atuin_stty_backup
stty "$ATUIN_STTY" __atuin_stty_backup=$(stty -g)
stty "$ATUIN_STTY"
# Execute the command. Note: We need to record $? and $_ after the # Execute the command. Note: We need to record $? and $_ after the
# user command within the same call of "eval" because $_ is otherwise # user command within the same call of "eval" because $_ is otherwise
# overwritten by the last argument of "eval". # overwritten by the last argument of "eval".
__atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}" __atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}"
eval -- "$__atuin_command"$'\n__bp_last_ret_value=$? __bp_last_argument_prev_command=$_' eval -- "$__atuin_command"$'\n__bp_last_ret_value=$? __bp_last_argument_prev_command=$_'
stty "$__atuin_stty_backup" stty "$__atuin_stty_backup"
fi fi
# Execute preprompt commands # Execute preprompt commands
local __atuin_prompt_command local __atuin_prompt_command
for __atuin_prompt_command in "${PROMPT_COMMAND[@]}"; do for __atuin_prompt_command in "${PROMPT_COMMAND[@]}"; do
__atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}" __atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}"
eval -- "$__atuin_prompt_command" eval -- "$__atuin_prompt_command"
done done
# Bash will redraw only the line with the prompt after we finish, # Bash will redraw only the line with the prompt after we finish,
# so to work for a multiline prompt we need to print it ourselves, # so to work for a multiline prompt we need to print it ourselves,
@ -134,32 +135,31 @@ __atuin_history() {
fi fi
fi fi
HISTORY="$(ATUIN_SHELL_BASH=t ATUIN_LOG=error atuin search "$@" -i -- "${READLINE_LINE}" 3>&1 1>&2 2>&3)" HISTORY=$(ATUIN_SHELL_BASH=t ATUIN_LOG=error atuin search "$@" -i -- "$READLINE_LINE" 3>&1 1>&2 2>&3)
# We do nothing when the search is canceled. # We do nothing when the search is canceled.
[[ $HISTORY ]] || return 0 [[ $HISTORY ]] || return 0
if [[ $HISTORY == __atuin_accept__:* ]] if [[ $HISTORY == __atuin_accept__:* ]]; then
then HISTORY=${HISTORY#__atuin_accept__:}
HISTORY=${HISTORY#__atuin_accept__:}
if [[ -n "${BLE_ATTACHED-}" ]]; then if [[ ${BLE_ATTACHED-} ]]; then
ble-edit/content/reset-and-check-dirty "$HISTORY" ble-edit/content/reset-and-check-dirty "$HISTORY"
ble/widget/accept-line ble/widget/accept-line
else else
__atuin_accept_line "$HISTORY" __atuin_accept_line "$HISTORY"
fi fi
READLINE_LINE="" READLINE_LINE=""
READLINE_POINT=${#READLINE_LINE} READLINE_POINT=${#READLINE_LINE}
else else
READLINE_LINE=${HISTORY} READLINE_LINE=$HISTORY
READLINE_POINT=${#READLINE_LINE} READLINE_POINT=${#READLINE_LINE}
fi fi
} }
# shellcheck disable=SC2154 # shellcheck disable=SC2154
if [[ -n "${BLE_VERSION-}" ]] && ((_ble_version >= 400)); then if [[ ${BLE_VERSION-} ]] && ((_ble_version >= 400)); then
ble-import contrib/integration/bash-preexec ble-import contrib/integration/bash-preexec
# Define and register an autosuggestion source for ble.sh's auto-complete. # Define and register an autosuggestion source for ble.sh's auto-complete.