From f63f24699e910be41a02682ad4fdd5c25febd0e5 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Wed, 10 Jan 2024 05:42:27 +0900 Subject: 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`. --- atuin/src/shell/atuin.bash | 90 +++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/atuin/src/shell/atuin.bash b/atuin/src/shell/atuin.bash index a1ef38b5..8db23b4b 100644 --- a/atuin/src/shell/atuin.bash +++ b/atuin/src/shell/atuin.bash @@ -14,24 +14,24 @@ __atuin_preexec() { local id id=$(atuin history start -- "$1") - export ATUIN_HISTORY_ID="${id}" + export ATUIN_HISTORY_ID=$id } __atuin_precmd() { - local EXIT="$?" + local EXIT=$? - [[ -z "${ATUIN_HISTORY_ID}" ]] && return + [[ ! $ATUIN_HISTORY_ID ]] && return local duration="" # shellcheck disable=SC2154,SC2309 - if [[ -n "${BLE_ATTACHED-}" && _ble_bash -ge 50000 && -n "${_ble_exec_time_ata-}" ]]; then - # We use the high-resolution duration based on EPOCHREALTIME (bash >= - # 5.0) that is recorded by ble.sh. The shell variable - # `_ble_exec_time_ata` contains the execution time in microseconds. - duration=${_ble_exec_time_ata}000 + if [[ ${BLE_ATTACHED-} && _ble_bash -ge 50000 && ${_ble_exec_time_ata-} ]]; then + # We use the high-resolution duration based on EPOCHREALTIME (bash >= + # 5.0) that is recorded by ble.sh. The shell variable + # `_ble_exec_time_ata` contains the execution time in microseconds. + duration=${_ble_exec_time_ata}000 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="" } @@ -71,38 +71,39 @@ __atuin_accept_line() { local __atuin_preexec_function_ret_value local __atuin_preexec_ret_value=0 for __atuin_preexec_function in "${preexec_functions[@]:-}"; do - if type -t "$__atuin_preexec_function" 1>/dev/null; then - __atuin_set_ret_value "${__bp_last_ret_value:-}" - "$__atuin_preexec_function" "$__atuin_command" - __atuin_preexec_function_ret_value="$?" - if [[ "$__atuin_preexec_function_ret_value" != 0 ]]; then - __atuin_preexec_ret_value="$__atuin_preexec_function_ret_value" + if type -t "$__atuin_preexec_function" 1>/dev/null; then + __atuin_set_ret_value "${__bp_last_ret_value:-}" + "$__atuin_preexec_function" "$__atuin_command" + __atuin_preexec_function_ret_value=$? + if [[ $__atuin_preexec_function_ret_value != 0 ]]; then + __atuin_preexec_ret_value=$__atuin_preexec_function_ret_value + fi fi - fi done # If extdebug is turned on and any preexec function returns non-zero # exit status, we do not run the user command. if ! { shopt -q extdebug && ((__atuin_preexec_ret_value)); }; then - # Juggle the terminal settings so that the command can be interacted with - local __atuin_stty_backup - __atuin_stty_backup=$(stty -g) - stty "$ATUIN_STTY" - - # Execute the command. Note: We need to record $? and $_ after the - # user command within the same call of "eval" because $_ is otherwise - # overwritten by the last argument of "eval". - __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=$_' - - stty "$__atuin_stty_backup" + # Juggle the terminal settings so that the command can be interacted + # with + local __atuin_stty_backup + __atuin_stty_backup=$(stty -g) + stty "$ATUIN_STTY" + + # Execute the command. Note: We need to record $? and $_ after the + # user command within the same call of "eval" because $_ is otherwise + # overwritten by the last argument of "eval". + __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=$_' + + stty "$__atuin_stty_backup" fi # Execute preprompt commands local __atuin_prompt_command for __atuin_prompt_command in "${PROMPT_COMMAND[@]}"; do - __atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}" - eval -- "$__atuin_prompt_command" + __atuin_set_ret_value "${__bp_last_ret_value-}" "${__bp_last_argument_prev_command-}" + eval -- "$__atuin_prompt_command" done # 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, @@ -134,32 +135,31 @@ __atuin_history() { 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. [[ $HISTORY ]] || return 0 - if [[ $HISTORY == __atuin_accept__:* ]] - then - HISTORY=${HISTORY#__atuin_accept__:} + if [[ $HISTORY == __atuin_accept__:* ]]; then + HISTORY=${HISTORY#__atuin_accept__:} - if [[ -n "${BLE_ATTACHED-}" ]]; then - ble-edit/content/reset-and-check-dirty "$HISTORY" - ble/widget/accept-line - else - __atuin_accept_line "$HISTORY" - fi + if [[ ${BLE_ATTACHED-} ]]; then + ble-edit/content/reset-and-check-dirty "$HISTORY" + ble/widget/accept-line + else + __atuin_accept_line "$HISTORY" + fi - READLINE_LINE="" - READLINE_POINT=${#READLINE_LINE} + READLINE_LINE="" + READLINE_POINT=${#READLINE_LINE} else - READLINE_LINE=${HISTORY} - READLINE_POINT=${#READLINE_LINE} + READLINE_LINE=$HISTORY + READLINE_POINT=${#READLINE_LINE} fi } # shellcheck disable=SC2154 -if [[ -n "${BLE_VERSION-}" ]] && ((_ble_version >= 400)); then +if [[ ${BLE_VERSION-} ]] && ((_ble_version >= 400)); then ble-import contrib/integration/bash-preexec # Define and register an autosuggestion source for ble.sh's auto-complete. -- cgit v1.3.1