【问题标题】:tput is very slow on MinTTY (Git Bash)tput 在 MinTTY 上非常慢(Git Bash)
【发布时间】:2021-08-21 20:28:44
【问题描述】:

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

鉴于我对 *nix 工具在 Windows 上的性能的了解,我怀疑减速是来自所有子 shell。

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

【问题讨论】:

    标签: performance git-bash mintty tput


    【解决方案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_...

    【讨论】:

    • @SeanAllred 你看到了什么性能提升?
    猜你喜欢
    • 2017-08-10
    • 2016-02-28
    • 2015-11-17
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 2018-11-18
    • 2020-08-16
    • 2017-08-19
    相关资源
    最近更新 更多