sistema_progs

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

execmark.bash (4605B)


      1 # blesh/contrib/config/execmark.bash (C) 2022, Koichi Murase <myoga.murase@gmail.com>
      2 #
      3 # Example setup
      4 #
      5 # ```bash
      6 # # blerc
      7 # ble-import -d contrib/config/execmark
      8 # bleopt exec_elapsed_enabled='_ble_exec_time_tot>=1000'
      9 # ```
     10 #
     11 
     12 _ble_contrib_config_execmark_status_name=(
     13   [0]=EXIT_SUCCESS
     14   [1]=EXIT_FAILURE
     15   [2]=usage_error
     16 
     17   # Used by bash
     18   [124]=progcomp_restart
     19   [126]=permisson_denied
     20   [127]=command_not_found
     21 
     22   # From /usr/include/sysexits.h
     23   [64]=EX_USAGE
     24   [65]=EX_DATAERR
     25   [66]=EX_NOINPUT
     26   [67]=EX_NOUSER
     27   [68]=EX_NOHOST
     28   [69]=EX_UNAVAILABLE
     29   [70]=EX_SOFTWARE
     30   [71]=EX_OSERR
     31   [72]=EX_OSFILE
     32   [73]=EX_CANTCREAT
     33   [74]=EX_IOERR
     34   [75]=EX_TEMPFAIL
     35   [76]=EX_PROTOCOL
     36   [77]=EX_NOPERM
     37   [78]=EX_CONFIG
     38 )
     39 function ble/contrib/config:execmark/.statusname {
     40   name=
     41   if [[ ${name:=${_ble_contrib_config_execmark_status_name[exit]}} ]]; then
     42     return 0
     43   elif ((128<=exit&&exit<=255)) && [[ ${name:=${_ble_builtin_trap_sig_name[exit&127]}} ]]; then
     44     return 0
     45   else
     46     return 1
     47   fi
     48 }
     49 
     50 function ble/contrib/config:execmark/.getsec {
     51   local msec=$1 sec
     52   if ((msec==0)); then
     53     ret=0
     54   elif ((msec<1000)); then
     55     ret=${msec}ms
     56   else
     57     ((sec=msec/1000,msec%=1000))
     58     msec=000$msec msec=${msec:${#msec}-3}
     59     ret=$sec.$msec
     60   fi
     61 }
     62 
     63 function ble/contrib/config:execmark/postexec.hook {
     64   local command=$_ble_edit_exec_BASH_COMMAND
     65 
     66   if ((_ble_edit_exec_lastexit)) || ble/exec/time#mark-enabled; then
     67     local ret
     68     local sgr=$'\e[;94m' sgrC=$'\e[38:5:244m' sgr0=$'\e[m'
     69     ble/color/face2sgr-ansi syntax_varname; local sgrV=$ret
     70     ble/color/face2sgr-ansi command_function; local sgrS=$ret
     71     ble/color/face2sgr-ansi varname_number; local sgrN=$ret
     72 
     73     # exit
     74     if ((_ble_edit_exec_lastexit)); then
     75       local sgrE=$'\e[;91m'
     76       local exit=$_ble_edit_exec_lastexit name=
     77       if ble/contrib/config:execmark/.statusname; then
     78         exit=$'\e[1m'"$name"$'\e[22m'" ($sgrN$exit$sgrE)"
     79       else
     80         exit="exit $sgrN$exit$sgrE"
     81       fi
     82       local mark=$sgrE'[ble: '$exit']'$sgr0
     83       ble/util/buffer.print "$mark"
     84     fi
     85 
     86     # ata
     87     local ata=$_ble_exec_time_ata
     88     local d=${#ata} sec min hour
     89     if ((d<=3)); then
     90       ata=${ata}us
     91     elif ((d<=5)); then
     92       ata=${ata::d-3}.${ata:d-3:6-d}ms
     93     elif ((d<=6)); then
     94       ata=${ata::3}ms
     95     elif ((d<=9)); then
     96       ata=${ata::${#ata}-6}.${ata:${#ata}-6:3}s
     97     elif ((sec=ata/(1000*1000),min=sec/60,sec%=60,min<100)); then
     98       ata="${min}m${sec}s"
     99     elif ((hour=min/60,min%=60,hour<100)); then
    100       ata="${hour}h${min}m${sec}s"
    101     else
    102       ata="$((hour/24))d$((hour%24))h${min}m"
    103     fi
    104     ata="${sgrV}elapsed$sgr $sgrN$ata$sgr"
    105 
    106     # cpu
    107     local cpu='--.-%'
    108     if ((_ble_exec_time_tot)); then
    109       cpu=$(((_ble_exec_time_usr+_ble_exec_time_sys)*1000/_ble_exec_time_tot))
    110       cpu=$((cpu/10)).$((cpu%10))%
    111     fi
    112     cpu="${sgrV}CPU$sgr $sgrN$cpu$sgr"
    113     if [[ $_ble_exec_time_usr_self && $_ble_exec_time_sys_self ]]; then
    114       local usr0=$_ble_exec_time_usr_self
    115       local sys0=$_ble_exec_time_sys_self
    116       local usr1=$((_ble_exec_time_usr-_ble_exec_time_usr_self))
    117       local sys1=$((_ble_exec_time_sys-_ble_exec_time_sys_self))
    118 
    119       local ret max=0
    120       ble/contrib/config:execmark/.getsec "$usr0"; local usr0s=$ret
    121       ble/contrib/config:execmark/.getsec "$sys0"; local sys0s=$ret
    122       ((usr0>max)) && max=$usr0
    123       ((sys0>max)) && max=$sys0
    124       if ((usr1||sys1)); then
    125         ble/contrib/config:execmark/.getsec "$usr1"; local usr1s=$ret
    126         ble/contrib/config:execmark/.getsec "$sys1"; local sys1s=$ret
    127         ((usr1>max)) && max=$usr1
    128         ((sys1>max)) && max=$sys1
    129       fi
    130 
    131       ((usr0==max)) && usr0s=$'\e[1m'$usr0s$'\e[22m'
    132       ((sys0==max)) && sys0s=$'\e[1m'$sys0s$'\e[22m'
    133       cpu="$cpu $sgrS(self)${sgrV}usr$sgrN$usr0s$sgr/${sgrV}sys$sgrN$sys0s$sgr"
    134       if ((usr1||sys1)); then
    135         ((usr1==max)) && usr1s=$'\e[1m'$usr1s$'\e[22m'
    136         ((sys1==max)) && sys1s=$'\e[1m'$sys1s$'\e[22m'
    137         cpu="$cpu+$sgrS(child)${sgrV}usr$sgrN${usr1s}$sgr/${sgrV}sys$sgrN${sys1s}$sgr"
    138       fi
    139     else
    140       local ret
    141       ble/contrib/config:execmark/.getsec "$_ble_exec_time_usr"; local usr=$ret
    142       ble/contrib/config:execmark/.getsec "$_ble_exec_time_sys"; local sys=$ret
    143       cpu="$cpu ${usr}usr/${sys}sys"
    144     fi
    145 
    146     local msg=$sgr'[ble: '$ata', '$cpu']'$sgr0
    147     local ret; ble/string#ltrim "$command"; msg="$msg${ret:+ $sgrC$ret$sgr0}"
    148     x=0 y=0 g=0 LINES=1 ble/canvas/trace "$msg" confine:truncate
    149     ble/util/buffer.print "$ret"
    150   fi
    151 }
    152 bleopt exec_@_mark=
    153 blehook POSTEXEC!=ble/contrib/config:execmark/postexec.hook