From 8db3755395592f1d6c53f6c35150939ff485d013 Mon Sep 17 00:00:00 2001 From: ReDTerraN Date: Sat, 15 Apr 2023 22:14:06 +0200 Subject: [PATCH 1/2] Fix minor typos --- README.md | 2 +- bin/xpanes | 90 +++++++++++++++++------------------------------ test/cases_all.sh | 16 ++++----- 3 files changed, 42 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index c7a0510..d9d130d 100644 --- a/README.md +++ b/README.md @@ -485,7 +485,7 @@ $ xpanes -c "ssh -t {} bash -ci 'll'" host-{1,2,3} +------------------------------------+-------------------------------------+ ``` -#### Run commands promtply +#### Run commands promptly `-s` option is useful if you have following issues. diff --git a/bin/xpanes b/bin/xpanes index e08ca4f..d1feab0 100755 --- a/bin/xpanes +++ b/bin/xpanes @@ -54,7 +54,7 @@ readonly XP_ABS_THIS_FILE_NAME="${XP_THIS_DIR}/${XP_THIS_FILE_NAME}" # This is quite rare case (but it can be happened). readonly XP_USER_HOME="${HOME:-/tmp}" -# Basically xpanes follows XDG Base Direcotry Specification. +# Basically xpanes follows XDG Base Directory Specification. # https://specifications.freedesktop.org/basedir-spec/basedir-spec-0.6.html XDG_CACHE_HOME="${XDG_CACHE_HOME:-${XP_USER_HOME}/.cache}" readonly XP_CACHE_HOME="${XDG_CACHE_HOME}/xpanes" @@ -244,8 +244,8 @@ xpns_get_tmux_version() { echo "${_ver//next-/}" } -# Check whether the prefered tmux version is greater than host's tmux version. -# $1 ... Prefered version. +# Check whether the preferred tmux version is greater than host's tmux version. +# $1 ... Preferred version. # $2 ... Host tmux version(optional). # In case of tmux version is 1.7, the result will be like this. # 0 is true, 1 is false. @@ -443,7 +443,7 @@ xpns_adjust_col_row() { row=0 fix_row_flg=0 fi - # This algorhythm is almost same as tmux default + # This algorithm is almost same as tmux default # https://github.com/tmux/tmux/blob/2.8/layout-set.c#L436 while ((col * row < N)); do ((fix_row_flg != 1)) && ((row = row + 1)) @@ -514,7 +514,7 @@ xpns_log_filenames() { ## -------------------------------- xpns_normalize_directory() { - # Remove end of slash '/' for the first arguement + # Remove end of slash '/' for the first argument local _dir="${1%/}" # tilde expansion printf "%s\\n" "${_dir/#~/$HOME}" @@ -884,7 +884,7 @@ xpns_prepare_extra_panes() { shift # specify a pane which has the biggest index number. # Because pane_id may not be immutable. - # If the small number of index is specified here, correspondance between pane_title and command can be slip off. + # If the small number of index is specified here, correspondence between pane_title and command can be slip off. ${TMUX_XPANES_EXEC} select-pane -t "${_window_name}.${_pane_base_index}" # split window into multiple panes @@ -1218,7 +1218,7 @@ xpns_check_env() { : "Supported tmux version" else xpns_msg_warning \ - "'${XP_THIS_FILE_NAME}' may not work correctly! Please check followings. + "'${XP_THIS_FILE_NAME}' may not work correctly! Please check following: * tmux is installed correctly. * Supported tmux version is installed. Version ${XP_SUPPORT_TMUX_VERSION_LOWER} and over is officially supported." @@ -1840,44 +1840,22 @@ xpns_warning_before_extra() { fi } -xpns_opt_check_num() { +xpns_opt_checker() { local _option="$1" - shift - local _arg="$1" - if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9]/}" ]]; then - return 0 - fi - xpns_msg_error "Invalid argument '$_arg' for $_option option" - exit ${XP_EINVAL} -} - -xpns_opt_check_float() { - local _option="$1" - shift - local _arg="$1" - if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9.]/}" ]]; then - return 0 - fi - xpns_msg_error "Invalid argument '$_arg' for $_option option" - exit ${XP_EINVAL} -} - -xpns_opt_check_str() { - local _option="$1" - shift - local _arg="$1" - if [[ -n "$_arg" ]]; then - return 0 + local _arg="$2" + local _check_value="${3:-default}" + # if we do not define a positional parameter in options, we expect an integer + local _pattern="^[0-9]+$" + if [[ "$_check_value" == "float" ]]; then + _pattern="^[0-9]*\.?[0-9]*$" + elif [[ "$_check_value" == "csv" ]]; then + _pattern="^[0-9]+(,[0-9]+)*$" + elif [[ "$_check_value" == "string" ]]; then + if [[ -n "$_arg" ]]; then + return 0 + fi fi - xpns_msg_error "Invalid argument '$_arg' for $_option option" - exit ${XP_EINVAL} -} - -xpns_opt_check_num_csv() { - local _option="$1" - shift - local _arg="$1" - if [[ "$1" =~ ^([0-9][0-9]*,?)+$ ]]; then + if [[ -n "$_arg" ]] && [[ "$_arg" =~ $_pattern ]]; then return 0 fi xpns_msg_error "Invalid argument '$_arg' for $_option option" @@ -1932,7 +1910,7 @@ xpns_parse_options() { XP_OPT_IS_SYNC=0 ;; --log-format) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_OPT_LOG_STORE=1 TMUX_XPANES_LOG_FORMAT="$1" @@ -1943,7 +1921,7 @@ xpns_parse_options() { XP_OPT_LOG_STORE=1 XP_OPTIONS+=("$opt") TMUX_XPANES_LOG_DIRECTORY="${opt#--log=}" - xpns_opt_check_str "${opt%=}" "$TMUX_XPANES_LOG_DIRECTORY" + xpns_opt_checker "${opt%=}" "$TMUX_XPANES_LOG_DIRECTORY" "string" ;; --log) XP_OPT_LOG_STORE=1 @@ -1964,28 +1942,28 @@ xpns_parse_options() { XP_OPT_ATTACH=0 ;; --cols) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_COLS="$1" XP_OPTIONS+=("$1") shift ;; --rows) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_ROWS="$1" XP_OPTIONS+=("$1") shift ;; --bulk-cols) - xpns_opt_check_num_csv "$opt" "$1" + xpns_opt_checker "$opt" "$1" "csv" XP_OPTIONS+=("$opt") XP_OPT_BULK_COLS="$1" XP_OPTIONS+=("$1") shift ;; --interval) - xpns_opt_check_float "$opt" "$1" + xpns_opt_checker "$opt" "$1" "float" XP_OPTIONS+=("$opt") XP_OPT_INTERVAL="$1" XP_OPTIONS+=("$1") @@ -2055,14 +2033,14 @@ xpns_parse_options() { # Short options with argument ## ---------------- -I) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_REPSTR="$1" XP_OPTIONS+=("$1") shift ;; -l) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_OPT_USE_PRESET_LAYOUT=1 XP_LAYOUT="$(printf '%s\n' "$1" | xpns_layout_short2long)" @@ -2072,7 +2050,6 @@ xpns_parse_options() { ;; -c) # -c allows empty - # xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_CMD_UTILITY="$1" XP_OPT_CMD_UTILITY=1 @@ -2080,28 +2057,28 @@ xpns_parse_options() { shift ;; -n) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_MAX_PANE_ARGS="$1" XP_OPTIONS+=("$1") shift ;; -S) - xpns_opt_check_str "$opt" "$1" + xpns_opt_checker "$opt" "$1" "string" XP_OPTIONS+=("$opt") XP_SOCKET_PATH="$1" XP_OPTIONS+=("$1") shift ;; -C) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_COLS="$1" XP_OPTIONS+=("$1") shift ;; -R) - xpns_opt_check_num "$opt" "$1" + xpns_opt_checker "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_ROWS="$1" XP_OPTIONS+=("$1") @@ -2109,7 +2086,6 @@ xpns_parse_options() { ;; -B) # -B allows empty - # xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_BEGIN_ARGS+=("$1") XP_OPTIONS+=("$1") diff --git a/test/cases_all.sh b/test/cases_all.sh index faf8b6d..1725454 100755 --- a/test/cases_all.sh +++ b/test/cases_all.sh @@ -181,7 +181,7 @@ get_window_id_from_prefix() { local _socket_file="$1" ; shift local _window_name_prefix="$1" ; shift local _window_id= - ## tmux bug: tmux does not handle the window_name which has dot(.) at the begining of the name. Use window_id instead. + ## tmux bug: tmux does not handle the window_name which has dot(.) at the beginning of the name. Use window_id instead. _window_id=$(${TMUX_EXEC} -S "${_socket_file}" list-windows -F '#{window_name} #{window_id}' \ | grep "^${_window_name_prefix}" \ | head -n 1 \ @@ -211,7 +211,7 @@ wait_panes_separation() { ${TMUX_EXEC} -S "${_socket_file}" list-panes -t "${_window_id}" >&2 # Wait several seconds to ensure the completion. # Even the number of panes equals to expected number, - # the separation is not complated sometimes. + # the separation is not completed sometimes. sleep 3 break fi @@ -1643,7 +1643,7 @@ test_desync_option_1() { if (is_less_than "1.9");then echo "Skip this test for $(tmux_version_number)." >&2 echo 'Because there is no way to check whether the window has synchronize-panes or not.' >&2 - echo '"#{pane_synchronnized}" is not yet implemented.' >&2 + echo '"#{pane_synchronized}" is not yet implemented.' >&2 echo 'Ref (format.c): https://github.com/tmux/tmux/compare/1.8...1.9#diff-3acde89642f1d5cccab8319fac95e43fR557' >&2 return 0 fi @@ -1711,7 +1711,7 @@ test_desync_option_2() { if (is_less_than "1.9");then echo "Skip this test for $(tmux_version_number)." >&2 echo 'Because there is no way to check whether the window has synchronize-panes or not.' >&2 - echo '"#{pane_synchronnized}" is not yet implemented.' >&2 + echo '"#{pane_synchronized}" is not yet implemented.' >&2 echo 'Ref (format.c): https://github.com/tmux/tmux/compare/1.8...1.9#diff-3acde89642f1d5cccab8319fac95e43fR557' >&2 return 0 fi @@ -2621,7 +2621,7 @@ test_log_format_and_desync_option() { if (is_less_than "1.9");then echo "Skip this test for $(tmux_version_number)." >&2 echo 'Because there is no way to check whether the window has synchronize-panes or not.' >&2 - echo '"#{pane_synchronnized}" is not yet implemented.' >&2 + echo '"#{pane_synchronized}" is not yet implemented.' >&2 echo 'Ref (format.c): https://github.com/tmux/tmux/compare/1.8...1.9#diff-3acde89642f1d5cccab8319fac95e43fR557' >&2 return 0 fi @@ -2733,7 +2733,7 @@ test_log_format_and_desync_option_pipe() { if (is_less_than "1.9");then echo "Skip this test for $(tmux_version_number)." >&2 echo 'Because there is no way to check whether the window has synchronize-panes or not.' >&2 - echo '"#{pane_synchronnized}" is not yet implemented.' >&2 + echo '"#{pane_synchronized}" is not yet implemented.' >&2 echo 'Ref (format.c): https://github.com/tmux/tmux/compare/1.8...1.9#diff-3acde89642f1d5cccab8319fac95e43fR557' >&2 return 0 fi @@ -3453,7 +3453,7 @@ test_ss_option_panes_not_found() { actual=$( cat "${_tmpdir}/exit_status" ) expected=31 - ## This test might be failed if the host sufferes high load or particular conditions. + ## This test might be failed if the host sufferers high load or particular conditions. ## Retry the test if it fails. ### In that case, "actual" might be "0". assertEquals "$expected" "$actual" @@ -4166,7 +4166,7 @@ test_rows_log_ss_t_option() { test_too_small_panes() { local _socket_file="${SHUNIT_TMPDIR}/.xpanes-shunit" local _tmpdir="${SHUNIT_TMPDIR}" - ## Blace expansion does not work if default shell is not bash + ## Brace expansion does not work if default shell is not bash _cmd="${EXEC} -S $_socket_file --stay \$(seq 1 500 | xargs); echo \$? > ${_tmpdir}/status" printf "\\n$ %s\\n" "${_cmd}" From bc134c7ca8a153d1b289517b4fb34e2cc9601f0a Mon Sep 17 00:00:00 2001 From: ReDTerraN Date: Mon, 17 Apr 2023 10:39:50 +0200 Subject: [PATCH 2/2] clean up xpanes --- bin/xpanes | 90 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/bin/xpanes b/bin/xpanes index d1feab0..e08ca4f 100755 --- a/bin/xpanes +++ b/bin/xpanes @@ -54,7 +54,7 @@ readonly XP_ABS_THIS_FILE_NAME="${XP_THIS_DIR}/${XP_THIS_FILE_NAME}" # This is quite rare case (but it can be happened). readonly XP_USER_HOME="${HOME:-/tmp}" -# Basically xpanes follows XDG Base Directory Specification. +# Basically xpanes follows XDG Base Direcotry Specification. # https://specifications.freedesktop.org/basedir-spec/basedir-spec-0.6.html XDG_CACHE_HOME="${XDG_CACHE_HOME:-${XP_USER_HOME}/.cache}" readonly XP_CACHE_HOME="${XDG_CACHE_HOME}/xpanes" @@ -244,8 +244,8 @@ xpns_get_tmux_version() { echo "${_ver//next-/}" } -# Check whether the preferred tmux version is greater than host's tmux version. -# $1 ... Preferred version. +# Check whether the prefered tmux version is greater than host's tmux version. +# $1 ... Prefered version. # $2 ... Host tmux version(optional). # In case of tmux version is 1.7, the result will be like this. # 0 is true, 1 is false. @@ -443,7 +443,7 @@ xpns_adjust_col_row() { row=0 fix_row_flg=0 fi - # This algorithm is almost same as tmux default + # This algorhythm is almost same as tmux default # https://github.com/tmux/tmux/blob/2.8/layout-set.c#L436 while ((col * row < N)); do ((fix_row_flg != 1)) && ((row = row + 1)) @@ -514,7 +514,7 @@ xpns_log_filenames() { ## -------------------------------- xpns_normalize_directory() { - # Remove end of slash '/' for the first argument + # Remove end of slash '/' for the first arguement local _dir="${1%/}" # tilde expansion printf "%s\\n" "${_dir/#~/$HOME}" @@ -884,7 +884,7 @@ xpns_prepare_extra_panes() { shift # specify a pane which has the biggest index number. # Because pane_id may not be immutable. - # If the small number of index is specified here, correspondence between pane_title and command can be slip off. + # If the small number of index is specified here, correspondance between pane_title and command can be slip off. ${TMUX_XPANES_EXEC} select-pane -t "${_window_name}.${_pane_base_index}" # split window into multiple panes @@ -1218,7 +1218,7 @@ xpns_check_env() { : "Supported tmux version" else xpns_msg_warning \ - "'${XP_THIS_FILE_NAME}' may not work correctly! Please check following: + "'${XP_THIS_FILE_NAME}' may not work correctly! Please check followings. * tmux is installed correctly. * Supported tmux version is installed. Version ${XP_SUPPORT_TMUX_VERSION_LOWER} and over is officially supported." @@ -1840,22 +1840,44 @@ xpns_warning_before_extra() { fi } -xpns_opt_checker() { +xpns_opt_check_num() { local _option="$1" - local _arg="$2" - local _check_value="${3:-default}" - # if we do not define a positional parameter in options, we expect an integer - local _pattern="^[0-9]+$" - if [[ "$_check_value" == "float" ]]; then - _pattern="^[0-9]*\.?[0-9]*$" - elif [[ "$_check_value" == "csv" ]]; then - _pattern="^[0-9]+(,[0-9]+)*$" - elif [[ "$_check_value" == "string" ]]; then - if [[ -n "$_arg" ]]; then - return 0 - fi + shift + local _arg="$1" + if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9]/}" ]]; then + return 0 + fi + xpns_msg_error "Invalid argument '$_arg' for $_option option" + exit ${XP_EINVAL} +} + +xpns_opt_check_float() { + local _option="$1" + shift + local _arg="$1" + if [[ -n "$_arg" ]] && [[ -z "${_arg//[0-9.]/}" ]]; then + return 0 + fi + xpns_msg_error "Invalid argument '$_arg' for $_option option" + exit ${XP_EINVAL} +} + +xpns_opt_check_str() { + local _option="$1" + shift + local _arg="$1" + if [[ -n "$_arg" ]]; then + return 0 fi - if [[ -n "$_arg" ]] && [[ "$_arg" =~ $_pattern ]]; then + xpns_msg_error "Invalid argument '$_arg' for $_option option" + exit ${XP_EINVAL} +} + +xpns_opt_check_num_csv() { + local _option="$1" + shift + local _arg="$1" + if [[ "$1" =~ ^([0-9][0-9]*,?)+$ ]]; then return 0 fi xpns_msg_error "Invalid argument '$_arg' for $_option option" @@ -1910,7 +1932,7 @@ xpns_parse_options() { XP_OPT_IS_SYNC=0 ;; --log-format) - xpns_opt_checker "$opt" "$1" "string" + xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_LOG_STORE=1 TMUX_XPANES_LOG_FORMAT="$1" @@ -1921,7 +1943,7 @@ xpns_parse_options() { XP_OPT_LOG_STORE=1 XP_OPTIONS+=("$opt") TMUX_XPANES_LOG_DIRECTORY="${opt#--log=}" - xpns_opt_checker "${opt%=}" "$TMUX_XPANES_LOG_DIRECTORY" "string" + xpns_opt_check_str "${opt%=}" "$TMUX_XPANES_LOG_DIRECTORY" ;; --log) XP_OPT_LOG_STORE=1 @@ -1942,28 +1964,28 @@ xpns_parse_options() { XP_OPT_ATTACH=0 ;; --cols) - xpns_opt_checker "$opt" "$1" + xpns_opt_check_num "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_COLS="$1" XP_OPTIONS+=("$1") shift ;; --rows) - xpns_opt_checker "$opt" "$1" + xpns_opt_check_num "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_ROWS="$1" XP_OPTIONS+=("$1") shift ;; --bulk-cols) - xpns_opt_checker "$opt" "$1" "csv" + xpns_opt_check_num_csv "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_BULK_COLS="$1" XP_OPTIONS+=("$1") shift ;; --interval) - xpns_opt_checker "$opt" "$1" "float" + xpns_opt_check_float "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_INTERVAL="$1" XP_OPTIONS+=("$1") @@ -2033,14 +2055,14 @@ xpns_parse_options() { # Short options with argument ## ---------------- -I) - xpns_opt_checker "$opt" "$1" "string" + xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_REPSTR="$1" XP_OPTIONS+=("$1") shift ;; -l) - xpns_opt_checker "$opt" "$1" "string" + xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_USE_PRESET_LAYOUT=1 XP_LAYOUT="$(printf '%s\n' "$1" | xpns_layout_short2long)" @@ -2050,6 +2072,7 @@ xpns_parse_options() { ;; -c) # -c allows empty + # xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_CMD_UTILITY="$1" XP_OPT_CMD_UTILITY=1 @@ -2057,28 +2080,28 @@ xpns_parse_options() { shift ;; -n) - xpns_opt_checker "$opt" "$1" + xpns_opt_check_num "$opt" "$1" XP_OPTIONS+=("$opt") XP_MAX_PANE_ARGS="$1" XP_OPTIONS+=("$1") shift ;; -S) - xpns_opt_checker "$opt" "$1" "string" + xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_SOCKET_PATH="$1" XP_OPTIONS+=("$1") shift ;; -C) - xpns_opt_checker "$opt" "$1" + xpns_opt_check_num "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_COLS="$1" XP_OPTIONS+=("$1") shift ;; -R) - xpns_opt_checker "$opt" "$1" + xpns_opt_check_num "$opt" "$1" XP_OPTIONS+=("$opt") XP_OPT_CUSTOM_SIZE_ROWS="$1" XP_OPTIONS+=("$1") @@ -2086,6 +2109,7 @@ xpns_parse_options() { ;; -B) # -B allows empty + # xpns_opt_check_str "$opt" "$1" XP_OPTIONS+=("$opt") XP_BEGIN_ARGS+=("$1") XP_OPTIONS+=("$1")