sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

init-term.sh (11017B)


      1 #!/bin/bash
      2 
      3 # 2020-02-07 #D12MSYS2 の CR 対策のため更新の必要あり
      4 
      5 _ble_term_tput=
      6 function ble/init:term/tput { return 1; }
      7 if ble/bin#freeze-utility-path tput; then
      8   ble/bin/tput cuu 1 &>/dev/null && _ble_term_tput=${_ble_term_tput}i
      9   ble/bin/tput UP  1 &>/dev/null && _ble_term_tput=${_ble_term_tput}c
     10   if [[ $_ble_term_tput ]]; then
     11     function ble/init:term/tput {
     12       local type=$_ble_term_tput
     13       if [[ $1 == -c ]]; then # termcap 優先
     14         shift
     15         [[ $type == ic ]] && type=c
     16       fi
     17 
     18       if [[ $type != c ]]; then
     19         # terminfo entry
     20         ble/bin/tput "${1%%:*}" "${@:2}" 2>/dev/null
     21       else
     22         # termcap entry
     23         ble/bin/tput "${1#*:}" "${@:2}" 2>/dev/null
     24       fi
     25     }
     26   fi
     27 fi
     28 
     29 function ble/init:term/register-varname {
     30   local name=$1
     31   varnames[${#varnames[@]}]=$name
     32 }
     33 
     34 function ble/init:term/define-cap {
     35   local IFS=$_ble_term_IFS
     36   local name=$1 def=$2
     37   shift 2
     38   ble/util/assign "$name" "ble/init:term/tput $* || ble/util/put \"\$def\""
     39   ble/init:term/register-varname "$name"
     40 }
     41 function ble/init:term/define-cap.2 {
     42   local IFS=$_ble_term_IFS
     43   local name=$1 def=$2
     44   shift 2
     45   ble/util/assign "$name" "ble/util/put x; ble/init:term/tput $* || ble/util/put \"\$def\"; ble/util/put x"
     46   builtin eval "$name=\${$name#x}; $name=\${$name%x}"
     47   ble/init:term/register-varname "$name"
     48 }
     49 
     50 _ble_term_sgr_term2ansi=()
     51 _ble_term_rex_sgr=$'\e''\[([0-9;:]+)m'
     52 function ble/init:term/define-sgr-param {
     53   local name=$1 seq=$2 ansi=$3
     54   if [[ $seq =~ $_ble_term_rex_sgr ]]; then
     55     local rematch1=${BASH_REMATCH[1]}
     56     builtin eval "$name=\$rematch1"
     57 
     58     # term2ansi
     59     if [[ $ansi ]]; then
     60       local rex='^[0-9]+$'
     61       [[ $rematch1 =~ $rex ]] &&
     62         [[ ! ${_ble_term_sgr_term2ansi[rematch1]} ]] &&
     63         _ble_term_sgr_term2ansi[rematch1]=$ansi
     64     fi
     65   else
     66     builtin eval "$name="
     67   fi
     68 
     69   if [[ $name =~ ^[_a-zA-Z][_a-zA-Z0-9]*$ ]]; then
     70     ble/init:term/register-varname "$name"
     71   fi
     72 }
     73 
     74 function ble/init:term/initialize {
     75   local -a varnames=()
     76   ble/init:term/register-varname _ble_term_sgr_term2ansi
     77 
     78   # xenl (end of line behavior)
     79   _ble_term_xenl=1
     80   [[ $_ble_term_tput ]] &&
     81     ! ble/init:term/tput xenl:xn &>/dev/null &&
     82     _ble_term_xenl=
     83   [[ $TERM == sun* ]] && _ble_term_xenl=
     84   ble/init:term/register-varname _ble_term_xenl
     85 
     86   # bce (background color erase)
     87   _ble_term_bce=
     88   [[ $_ble_term_tput ]] &&
     89     ble/init:term/tput bce:ut &>/dev/null &&
     90     _ble_term_bce=1
     91   ble/init:term/register-varname _ble_term_bce
     92 
     93   # tab width
     94   _ble_term_it=8
     95   if [[ $_ble_term_tput ]]; then
     96     ble/util/assign _ble_term_it 'ble/init:term/tput it:it'
     97     _ble_term_it=${_ble_term_it:-8}
     98   fi
     99   ble/init:term/register-varname _ble_term_it
    100 
    101   # IND/RI, CR, LF, FS
    102   ble/init:term/define-cap.2 _ble_term_ind $'\n' ind:sf # $'\eD'
    103   ble/init:term/define-cap   _ble_term_ri  ''    ri:sr  # $'\eM'
    104   ble/init:term/define-cap   _ble_term_cr  $'\r' cr:cr
    105   if [[ $OSTYPE == msys && ! $_ble_term_CR ]]; then # msys-1.0
    106     [[ $_ble_term_cr ]] || _ble_term_cr=$'\e[G'
    107     if [[ $TERM == cygwin ]]; then
    108       [[ $_ble_term_ind == $'\eD' ]] && _ble_term_ind=$'\n'
    109       _ble_term_xenl=
    110     fi
    111   fi
    112 
    113   # CUU/CUD/CUF/CUB
    114   ble/init:term/define-cap _ble_term_cuu $'\e[%dA' cuu:UP 123
    115   ble/init:term/define-cap _ble_term_cud $'\e[%dB' cud:DO 123
    116   ble/init:term/define-cap _ble_term_cuf $'\e[%dC' cuf:RI 123
    117   ble/init:term/define-cap _ble_term_cub $'\e[%dD' cub:LE 123
    118   _ble_term_cuu=${_ble_term_cuu//123/%d}
    119   _ble_term_cud=${_ble_term_cud//123/%d}
    120   _ble_term_cuf=${_ble_term_cuf//123/%d}
    121   _ble_term_cub=${_ble_term_cub//123/%d}
    122   # ※もし 122 だとか 124 だとかになると上記では駄目
    123 
    124   _ble_term_ri_or_cuu1=${_ble_term_ri:-${_ble_term_cuu//'%d'/1}}
    125   ble/init:term/register-varname _ble_term_ri_or_cuu1
    126 
    127   # CUP
    128   ble/init:term/define-cap _ble_term_cup $'\e[13;35H' cup:cm 12 34
    129   _ble_term_cup=${_ble_term_cup//13/%l}
    130   _ble_term_cup=${_ble_term_cup//35/%c}
    131   _ble_term_cup=${_ble_term_cup//12/%y}
    132   _ble_term_cup=${_ble_term_cup//34/%x}
    133 
    134   # CHA HPA VPA
    135   ble/init:term/define-cap _ble_term_hpa "$_ble_term_cr${_ble_term_cuf//'%d'/123}" hpa:ch 123
    136   _ble_term_hpa=${_ble_term_hpa//123/%x}
    137   _ble_term_hpa=${_ble_term_hpa//124/%c}
    138   ble/init:term/define-cap _ble_term_vpa "${_ble_term_cuu//'%d'/199}${_ble_term_cud//'%d'/123}" vpa:cv 123
    139   _ble_term_vpa=${_ble_term_vpa//123/%y}
    140   _ble_term_vpa=${_ble_term_vpa//124/%l}
    141 
    142   # CUP+ED (clear_screen)
    143   ble/init:term/define-cap _ble_term_clear $'\e[H\e[2J' clear:cl
    144 
    145   # IL/DL
    146   ble/init:term/define-cap _ble_term_il $'\e[%dL' il:AL 123
    147   ble/init:term/define-cap _ble_term_dl $'\e[%dM' -c dl:DL 123
    148   _ble_term_il=${_ble_term_il//123/%d}
    149   _ble_term_dl=${_ble_term_dl//123/%d}
    150 
    151   # EL
    152   ble/init:term/define-cap _ble_term_el  $'\e[K'  el:ce
    153   ble/init:term/define-cap _ble_term_el1 $'\e[1K' el1:cb
    154   if [[ $_ble_term_el == $'\e[K' && $_ble_term_el1 == $'\e[1K' ]]; then
    155     _ble_term_el2=$'\e[2K'
    156   else
    157     _ble_term_el2=$_ble_term_el1$_ble_term_el
    158   fi
    159   ble/init:term/register-varname _ble_term_el2
    160 
    161   # ED
    162   ble/init:term/define-cap _ble_term_ed  $'\e[J' -c ed:cd
    163 
    164   # ICH/DCH/ECH
    165   #   Note: 必ずしも対応しているか分からないので terminfo に載っている時のみ使う。
    166   ble/init:term/define-cap _ble_term_ich '' ich:IC 123 # CSI @
    167   ble/init:term/define-cap _ble_term_dch '' dch:DC 123 # CSI P
    168   ble/init:term/define-cap _ble_term_ech '' ech:ec 123 # CSI X
    169   _ble_term_ich=${_ble_term_ich//123/%d}
    170   _ble_term_dch=${_ble_term_dch//123/%d}
    171   _ble_term_ech=${_ble_term_ech//123/%d}
    172 
    173   # DECSC/DECRC or SCOSC/SCORC
    174   ble/init:term/define-cap _ble_term_sc $'\e7' sc:sc # \e[s
    175   ble/init:term/define-cap _ble_term_rc $'\e8' rc:rc # \e[u
    176   [[ $TERM == minix ]] && _ble_term_sc= _ble_term_rc=
    177   # Note: TERM=sun{,-color}: terminfo にはないが \e7 \e8 が使える。
    178 
    179   # Cursors
    180   ble/init:term/define-cap _ble_term_Ss '' Ss:Ss 123 # DECSCUSR
    181   _ble_term_Ss=${_ble_term_Ss//123/@1}
    182   ble/init:term/define-cap _ble_term_cvvis $'\e[?25h' cvvis:vs
    183   ble/init:term/define-cap _ble_term_civis $'\e[?25l' civis:vi
    184   [[ $TERM == minix ]] && _ble_term_cvvis= _ble_term_civis=
    185   # xterm の terminfo が点滅まで勝手に変更するので消す。
    186   [[ $_ble_term_cvvis == $'\e[?12;25h' || $_ble_term_cvvis == $'\e[?25;12h' ]] &&
    187     _ble_term_cvvis=$'\e[?25h'
    188   # 何故か screen の terminfo が壊れている(非対称になっている)ので対称化する。
    189   [[ $_ble_term_cvvis == $'\e[34l'* && $_ble_term_civis != *$'\e[34h'* ]] &&
    190     _ble_term_civis=$_ble_term_civis$'\e[34h'
    191   [[ $_ble_term_civis == $'\e[?25l'* && $_ble_term_cvvis != *$'\e[?25h'* ]] &&
    192     _ble_term_cvvis=$_ble_term_cvvis$'\e[?25h'
    193 
    194   # Alternate Screen Buffer
    195   ble/init:term/define-cap _ble_term_smcup '' smcup:ti # \e[?1049h
    196   ble/init:term/define-cap _ble_term_rmcup '' rmcup:te # \e[?1049l
    197 
    198   # status lines
    199   ble/init:term/define-cap _ble_term_tsl '' tsl:ts
    200   ble/init:term/define-cap _ble_term_fsl '' fsl:fs
    201   ble/init:term/define-cap _ble_term_dsl '' dsl:ds
    202   [[ ! $_ble_term_dsl && $_ble_term_fsl ]] &&
    203     _ble_term_dsl=$_ble_term_tsl$_ble_term_fsl
    204 
    205   # SGR clear
    206   ble/init:term/define-cap _ble_term_sgr0 $'\e[m' sgr0:me
    207 
    208   # SGR misc
    209   ble/init:term/define-cap _ble_term_bold  $'\e[1m' bold:md
    210   ble/init:term/define-cap _ble_term_blink $'\e[5m' blink:mb
    211   ble/init:term/define-cap _ble_term_rev   $'\e[7m' rev:mr
    212   ble/init:term/define-cap _ble_term_invis $'\e[8m' invis:mk
    213   ble/init:term/define-sgr-param _ble_term_sgr_bold  "$_ble_term_bold"  1
    214   ble/init:term/define-sgr-param _ble_term_sgr_blink "$_ble_term_blink" 5
    215   ble/init:term/define-sgr-param _ble_term_sgr_rev   "$_ble_term_rev"   7
    216   ble/init:term/define-sgr-param _ble_term_sgr_invis "$_ble_term_invis" 8
    217   ble/init:term/define-cap _ble_term_sitm $'\e[3m'  sitm:ZH
    218   ble/init:term/define-cap _ble_term_ritm $'\e[23m' ritm:ZR
    219   ble/init:term/define-cap _ble_term_smul $'\e[4m'  smul:us
    220   ble/init:term/define-cap _ble_term_rmul $'\e[24m' rmul:ue
    221   ble/init:term/define-cap _ble_term_smso $'\e[7m'  smso:so
    222   ble/init:term/define-cap _ble_term_rmso $'\e[27m' rmso:se
    223   ble/init:term/define-sgr-param _ble_term_sgr_sitm "$_ble_term_sitm" 3
    224   ble/init:term/define-sgr-param _ble_term_sgr_ritm "$_ble_term_ritm" 23
    225   ble/init:term/define-sgr-param _ble_term_sgr_smul "$_ble_term_smul" 4
    226   ble/init:term/define-sgr-param _ble_term_sgr_rmul "$_ble_term_rmul" 24
    227   ble/init:term/define-sgr-param _ble_term_sgr_smso "$_ble_term_smso" 7
    228   ble/init:term/define-sgr-param _ble_term_sgr_rmso "$_ble_term_rmso" 27
    229 
    230   # Note: rev と smso が同じ場合は、rev の reset に rmso を使用する。
    231   ble/init:term/register-varname _ble_term_sgr_rev_reset
    232   if [[ $_ble_term_sgr_smso && $_ble_term_sgr_smso == "$_ble_term_sgr_rev" ]]; then
    233     _ble_term_sgr_rev_reset=$_ble_term_sgr_rmso
    234   else
    235     _ble_term_sgr_rev_reset=
    236   fi
    237 
    238   # SGR colors
    239   ble/init:term/define-cap _ble_term_colors 256 colors:Co
    240   local i
    241   _ble_term_setaf=()
    242   _ble_term_setab=()
    243   _ble_term_sgr_af=()
    244   _ble_term_sgr_ab=()
    245   for ((i=0;i<16;i++)); do
    246     local i1=$((i%8)) af= ab=
    247 
    248     if [[ $TERM == *-direct ]]; then
    249       # Note: direct の時には terminfo 経由では16 色に対応する
    250       #   シーケンスを得られない。direct に対応している場合は
    251       #   当然 index color にも対応していると期待されるので、
    252       #   16 色にはそれを用いる。
    253       if ((i<8)); then
    254         af=$'\e[3'$i'm'
    255         ab=$'\e[4'$i'm'
    256       else
    257         af=$'\e[38;5;'$i'm'
    258         ab=$'\e[48;5;'$i'm'
    259       fi
    260     else
    261       # from terminfo
    262       if ((i<_ble_term_colors)); then
    263         local j1
    264         ((j1=(i1==3?6:
    265               (i1==6?3:
    266                (i1==1?4:
    267                 (i1==4?1:i1))))))
    268         local j=$((i-i1+j1))
    269 
    270         ble/util/assign af 'ble/init:term/tput setaf:AF "$i" 2>/dev/null'
    271         [[ $af ]] || ble/util/assign af 'ble/init:term/tput setf:Sf "$j" 2>/dev/null'
    272 
    273         ble/util/assign ab 'ble/init:term/tput setab:AB "$i" 2>/dev/null'
    274         [[ $ab ]] || ble/util/assign ab 'ble/init:term/tput setb:Sb "$j" 2>/dev/null'
    275       fi
    276     fi
    277 
    278     # default value
    279     [[ $af ]] || af=$'\e[3'$i1'm'
    280     [[ $ab ]] || ab=$'\e[4'$i1'm'
    281 
    282     # register
    283     _ble_term_setaf[i]=$af
    284     _ble_term_setab[i]=$ab
    285     local ansi_sgr_af=3$i1 ansi_sgr_ab=4$i1
    286     ((i>=8)) && ansi_sgr_af=9$i1 ansi_sgr_ab=10$i1
    287     ble/init:term/define-sgr-param "_ble_term_sgr_af[i]" "$af" "$ansi_sgr_af"
    288     ble/init:term/define-sgr-param "_ble_term_sgr_ab[i]" "$ab" "$ansi_sgr_ab"
    289   done
    290   ble/init:term/register-varname "_ble_term_setaf"
    291   ble/init:term/register-varname "_ble_term_setab"
    292   ble/init:term/register-varname "_ble_term_sgr_af"
    293   ble/init:term/register-varname "_ble_term_sgr_ab"
    294 
    295   # save
    296   ble/util/declare-print-definitions "${varnames[@]}" >| "$_ble_base_cache/term.$TERM"
    297 }
    298 
    299 ble/util/put "ble/term.sh: updating tput cache for TERM=$TERM... " >&2
    300 ble/init:term/initialize
    301 ble/util/print $'\r'"ble/term.sh: updating tput cache for TERM=$TERM... done" >&2
    302 
    303 return 0