0

Git Bash 总体上相当缓慢(比较 WSL/Ubuntu 下 1.082 秒的平均运行时间与 MinTTY 下的 4.460 秒)。我将高达 1.479 秒的时间缩小到以下代码块:

# Determine if this terminal supports colors
if test -t 1; then
    if [[ -n "$(tput colors)" ]] && [[ "$(tput colors)" -ge 8 ]]; then
        MY_APP_FMT_SUPPORTED=true

        MY_APP_FMT_BOLD="$(tput bold)"
        MY_APP_FMT_UNDERLINE="$(tput smul)"
        MY_APP_FMT_INVERSE="$(tput smso)"
        MY_APP_FMT_BLACK="$(tput setaf 0)"
        MY_APP_FMT_RED="$(tput setaf 1)"
        MY_APP_FMT_GREEN="$(tput setaf 2)"
        MY_APP_FMT_YELLOW="$(tput setaf 3)"
        MY_APP_FMT_BLUE="$(tput setaf 4)"
        MY_APP_FMT_MAGENTA="$(tput setaf 5)"
        MY_APP_FMT_CYAN="$(tput setaf 6)"
        MY_APP_FMT_WHITE="$(tput setaf 7)"

        MY_APP_FMT_CODE=$MY_APP_FMT_CYAN

        # placing it down below so that option -x doesn't cause bad highlighting
        # to persist
        MY_APP_FMT_CLEAR="$(tput sgr0)"
    fi
fi

鉴于我对 Windows 上 *nix 工具性能的理解,我怀疑减速来自所有子外壳。

  • 这些子shell是否应该解释整个放缓?如果没有,我需要继续研究为什么 Git Bash 仍然缓慢。
  • 在保持终端兼容性的同时,有没有更高效的方法来做到这一点?
4

1 回答 1

1

您可以使用-S以下选项对 tput 调用进行分组:

#!/usr/bin/env bash

tkeys=(bold smul "setaf 0" "setaf 1") # You can add the rest

tvalues_s=$(tput -S < <(printf "%s\n" "${tkeys[@]}"))

declare -a tvalues=( ${tvalues_s//$'\e'/ $'\e'} )

declare -p tvalues

现在您在 中有值tvalues,您可以将其分配给 MY_APP_FMT_...

于 2021-08-21T21:45:26.400 回答