addet blesh

This commit is contained in:
pika 2024-08-18 03:40:38 +02:00
parent 58205a50b9
commit 8c8d8e9962
302 changed files with 74275 additions and 0 deletions

View file

@ -0,0 +1,155 @@
# blesh/contrib/config/execmark.bash (C) 2022, Koichi Murase <myoga.murase@gmail.com>
#
# Example setup
#
# ```bash
# # blerc
# ble-import -d contrib/config/execmark
# bleopt exec_elapsed_enabled='_ble_exec_time_tot>=1000'
# ```
#
_ble_contrib_config_execmark_status_name=(
[0]=EXIT_SUCCESS
[1]=EXIT_FAILURE
[2]=usage_error
# Used by bash
[124]=progcomp_restart
[126]=permisson_denied
[127]=command_not_found
# From /usr/include/sysexits.h
[64]=EX_USAGE
[65]=EX_DATAERR
[66]=EX_NOINPUT
[67]=EX_NOUSER
[68]=EX_NOHOST
[69]=EX_UNAVAILABLE
[70]=EX_SOFTWARE
[71]=EX_OSERR
[72]=EX_OSFILE
[73]=EX_CANTCREAT
[74]=EX_IOERR
[75]=EX_TEMPFAIL
[76]=EX_PROTOCOL
[77]=EX_NOPERM
[78]=EX_CONFIG
)
function ble/contrib/config:execmark/.statusname {
name=
if [[ ${name:=${_ble_contrib_config_execmark_status_name[exit]}} ]]; then
return 0
elif ((128<=exit&&exit<=255)) && [[ ${name:=${_ble_builtin_trap_sig_name[exit&127]}} ]]; then
return 0
else
return 1
fi
}
function ble/contrib/config:execmark/.getsec {
local msec=$1 sec
if ((msec==0)); then
ret=0
elif ((msec<1000)); then
ret=${msec}ms
else
((sec=msec/1000,msec%=1000))
msec=000$msec msec=${msec:${#msec}-3}
ret=$sec.$msec
fi
}
function ble/contrib/config:execmark/postexec.hook {
local command=$_ble_edit_exec_BASH_COMMAND
if ((_ble_edit_exec_lastexit)) || ble/exec/time#mark-enabled; then
local ret
local sgr=$'\e[;94m' sgrC=$'\e[38:5:244m' sgr0=$'\e[m'
ble/color/face2sgr-ansi syntax_varname; local sgrV=$ret
ble/color/face2sgr-ansi command_function; local sgrS=$ret
ble/color/face2sgr-ansi varname_number; local sgrN=$ret
# exit
if ((_ble_edit_exec_lastexit)); then
local sgrE=$'\e[;91m'
local exit=$_ble_edit_exec_lastexit name=
if ble/contrib/config:execmark/.statusname; then
exit=$'\e[1m'"$name"$'\e[22m'" ($sgrN$exit$sgrE)"
else
exit="exit $sgrN$exit$sgrE"
fi
local mark=$sgrE'[ble: '$exit']'$sgr0
x=0 y=0 g=0 LINES=1 ble/canvas/trace "$mark" confine:truncate
ble/util/buffer.print "$ret"
fi
# ata
local ata=$_ble_exec_time_ata
local d=${#ata} sec min hour
if ((d<=3)); then
ata=${ata}us
elif ((d<=5)); then
ata=${ata::d-3}.${ata:d-3:6-d}ms
elif ((d<=6)); then
ata=${ata::3}ms
elif ((d<=9)); then
ata=${ata::${#ata}-6}.${ata:${#ata}-6:3}s
elif ((sec=ata/(1000*1000),min=sec/60,sec%=60,min<100)); then
ata="${min}m${sec}s"
elif ((hour=min/60,min%=60,hour<100)); then
ata="${hour}h${min}m${sec}s"
else
ata="$((hour/24))d$((hour%24))h${min}m"
fi
ata="${sgrV}elapsed$sgr $sgrN$ata$sgr"
# cpu
local cpu='--.-%'
if ((_ble_exec_time_tot)); then
cpu=$(((_ble_exec_time_usr+_ble_exec_time_sys)*1000/_ble_exec_time_tot))
cpu=$((cpu/10)).$((cpu%10))%
fi
cpu="${sgrV}CPU$sgr $sgrN$cpu$sgr"
if [[ $_ble_exec_time_usr_self && $_ble_exec_time_sys_self ]]; then
local usr0=$_ble_exec_time_usr_self
local sys0=$_ble_exec_time_sys_self
local usr1=$((_ble_exec_time_usr-_ble_exec_time_usr_self))
local sys1=$((_ble_exec_time_sys-_ble_exec_time_sys_self))
local ret max=0
ble/contrib/config:execmark/.getsec "$usr0"; local usr0s=$ret
ble/contrib/config:execmark/.getsec "$sys0"; local sys0s=$ret
((usr0>max)) && max=$usr0
((sys0>max)) && max=$sys0
if ((usr1||sys1)); then
ble/contrib/config:execmark/.getsec "$usr1"; local usr1s=$ret
ble/contrib/config:execmark/.getsec "$sys1"; local sys1s=$ret
((usr1>max)) && max=$usr1
((sys1>max)) && max=$sys1
fi
((usr0==max)) && usr0s=$'\e[1m'$usr0s$'\e[22m'
((sys0==max)) && sys0s=$'\e[1m'$sys0s$'\e[22m'
cpu="$cpu $sgrS(self)${sgrV}usr$sgrN$usr0s$sgr/${sgrV}sys$sgrN$sys0s$sgr"
if ((usr1||sys1)); then
((usr1==max)) && usr1s=$'\e[1m'$usr1s$'\e[22m'
((sys1==max)) && sys1s=$'\e[1m'$sys1s$'\e[22m'
cpu="$cpu+$sgrS(child)${sgrV}usr$sgrN${usr1s}$sgr/${sgrV}sys$sgrN${sys1s}$sgr"
fi
else
local ret
ble/contrib/config:execmark/.getsec "$_ble_exec_time_usr"; local usr=$ret
ble/contrib/config:execmark/.getsec "$_ble_exec_time_sys"; local sys=$ret
cpu="$cpu ${usr}usr/${sys}sys"
fi
local msg=$sgr'[ble: '$ata', '$cpu']'$sgr0
local ret; ble/string#ltrim "$command"; msg="$msg${ret:+ $sgrC$ret$sgr0}"
x=0 y=0 g=0 LINES=1 ble/canvas/trace "$msg" confine:truncate
ble/util/buffer.print "$ret"
fi
}
bleopt exec_errexit_mark=
bleopt exec_elapsed_mark=
blehook POSTEXEC!=ble/contrib/config:execmark/postexec.hook

View file

@ -0,0 +1,31 @@
# blesh-contrib/config/github265-prompt-path-level-colors.bash (C) 2022, Koichi Murase <myoga.murase@gmail.com>
#
# Example:
#
# ble-import /config/github265-prompt-path-level-colors
# PS1='[\u@\h \q{github265/pwd}]\$ '
#
_ble_contrib_config_github265_pwd_gspecs=(fg=27 fg=57 fg=141 fg=63)
function ble/prompt/backslash:github265/pwd {
ble/prompt/unit/add-hash '$PWD'
ble/prompt/.update-working-directory
local str=$prompt_cache_wd
local -a dirs=()
while ble/string#match "$str" '^([^/]*/|[^/]+$)'; do
ble/array#push dirs "$BASH_REMATCH"
str=${str:${#BASH_REMATCH}}
done
local count=${#dirs[@]}
local n=${#_ble_contrib_config_github265_pwd_gspecs[@]} index ret
for dir in "${dirs[@]}"; do
((index=--count,index<n||(index=n-1)))
ble/prompt/process-prompt-string "\g{${_ble_contrib_config_github265_pwd_gspecs[index]}}"
ble/prompt/.escape-control-characters "$dir"
ble/prompt/print "$ret"
done
ble/prompt/print "$_ble_term_sgr0"
return 0
}

View file

@ -0,0 +1,54 @@
# blesh/contrib/config/github288-filter-sabbrev-completion.bash (C) 2023, Koichi Murase <myoga.murase@gmail.com>
# The option "bleopt complete_source_sabbrev_command_ignore" specifies a
# colon-separated list of glob patterns of command names for which the sabbrev
# completion should be suppressed.
bleopt/declare -v complete_source_sabbrev_command_ignore ''
_ble_contrib_config_github288_command_ignore_patterns=()
function bleopt/check:complete_source_sabbrev_command_ignore {
if [[ $value ]]; then
ble/string#split _ble_contrib_config_github288_command_ignore_patterns : "$value"
else
_ble_contrib_config_github288_command_ignore_patterns=()
fi
}
bleopt -I complete_source_sabbrev_command_ignore
# This is an "advice" function for the exiting function
# "ble/complete/source:sabbrev", i.e., the function to modify the behavior of
# the original function. We insert some codes before starting the original
# processing.
function ble/contrib/config:github288/filter-sabbrev-completion.advice {
# If we are generating sabbrevs as a part of argument completions and if
# there are any ignore patterns for the command names in
# "complete_source_sabbrev_command_ignore", we make the sabbrev completions
# inactive for the commands matching an ignore pattern.
if [[ ${ADVICE_FUNCNAME[1]} == ble/complete/source:argument ]]; then
if ((${#_ble_contrib_config_github288_command_ignore_patterns[@]})); then
# Extract the command name and arguments on the current caret position.
local comp_words comp_line comp_point comp_cword
ble/syntax:bash/extract-command "$_ble_edit_ind" || return 1
# If the command word is simple enough, we evaluate the command word to
# resolve quoting and parameter expansions.
local cmd=${comp_words[0]} ret
ble/syntax:bash/simple-word/safe-eval "$cmd" nonull && cmd=$ret
# If the command name matches any pattern of the ignored list, we return
# the function without calling the original function.
ble/complete/string#match-patterns "$cmd" "${_ble_contrib_config_github288_command_ignore_patterns[@]}" &&
return 1
fi
fi
# We call the original "ble/complete/source:sabbrev" here. The original
# function, i.e., the advice target, can be called by ble/function#advice/do.
ble/function#advice/do
}
function ble/contrib/config:github288/initialize.hook {
# We hook the above "advice" function to "ble/complete/source:sabbrev".
ble/function#advice around ble/complete/source:sabbrev ble/contrib/config:github288/filter-sabbrev-completion.advice
}
blehook/eval-after-load complete ble/contrib/config:github288/initialize.hook

View file

@ -0,0 +1,21 @@
# blesh/contrib/config/github296-named-execmark.bash (C) 2023, Koichi Murase <myoga.murase@gmail.com>
function ble/contrib/config:github296/named-execmark.hook {
local lastexit=$? command=$1
if ((lastexit)) && ! ble/string#match "$command" $'^[[:space:]]*#[^\n]+$'; then
local sgr=$'\e[;94m' sgr0=$'\e[m' sgrE=$'\e[;91m' ret
ble/color/face2sgr-ansi varname_number; local sgrN=$ret
local exit=$lastexit name=
if ((exit>=128)) && [[ ${name:=${_ble_builtin_trap_sig_name[exit&127]}} ]]; then
exit=$'\e[1m'"$name"$'\e[22m'" ($sgrN$exit$sgrE)"
else
exit="exit $sgrN$exit$sgrE"
fi
local mark=$sgrE'[ble: '$exit']'$sgr0
ble/util/buffer.print "$mark"
fi
}
bleopt exec_errexit_mark=
blehook POSTEXEC!=ble/contrib/config:github296/named-execmark.hook

View file

@ -0,0 +1,76 @@
# ble.sh contrib/config/github302-perlre-server.bash
# Copyright 2023 Britton Kerin <https://lists.gnu.org/archive/html/help-bash/2023-03/msg00068.html>
# Copyright 2023 Koichi Murase <myoga.murase@gmail.com>
# This file illustrates an example usage of "ble/util/bgproc#open" from module
# "util.bgproc". The server code in the shell function
# "ble/contrib/config:github302/perlre-server" was taken from the report by
# Britton Kerin at Ref. [1].
#
# [1] https://lists.gnu.org/archive/html/help-bash/2023-03/msg00068.html
#
# Note: This example does not consider the case that the background process
# terminates by an error, crashes, etc.
ble-import util.bgproc
function ble/contrib/config:github302/perlre-server {
exec perl <(cat <<' END_PERL'
use strict; use warnings;
$| = 1;
while ( 1 ) {
my $rex = <>;
chomp($rex);
my $str = <>;
if ( $str =~ m/$rex/ ) {
# FIXME: Sadly this die argument doesn't show anywhere:
defined($1) or die 'group $1 unexpectedly undefined';
print length($1);
}
print "\n";
}
END_PERL
) 2>/dev/tty
}
if ble/util/bgproc#open perlre_server ble/contrib/config:github302/perlre-server; then
# The main shell can send a request to fd ${perlre_server_bgproc[1]} and can
# read from fd ${perlre_server_bgproc[0]}.
ble/util/print "ble/contrib/config:github30: perlre-server (${perlre_server_bgproc[4]}) has started." >&2
else
ble/util/print 'ble/contrib/config:github30: failed to start perlre-server' >&2
return 1
fi
## @fn ble/contrib/config:github302/perlre-match rex str
## Matches the regex REX against the string STR and returns the length of the
## string captured by the first matching group ($1).
##
## @param[in] rex
## The regex to match in "perlre" syntax.
## @param[in] str
## The string to be matched by REX.
## @var[out] ret
## The resulting length of the string matched by the first capture group is
## stored in this variable. When REX does not match STR, an empty value is
## stored in this variable.
## @exit 0 if REX successfully matches STR, or otherwise 1.
##
function ble/contrib/config:github302/perlre-match {
local IFS=$' \t\n'
# Only takes the first lines of both because newlines in requests confuse the
# server.
local rex=${1%%$'\n'*} str=${2%%$'\n'*}
# We send the requests to fd ${perlre_server_bgproc[1]}.
ble/util/print-lines "$rex" "$str" >&"${perlre_server_bgproc[1]}"
# We can read the resposnes from fd ${perlre_server_bgproc[0]}. We set a
# timeout to `read' so that it doesn't lock forever in case that the expected
# output is not obtained by accident (e.g., for the reason that REX did not
# contain any capturing group accessible through $1).
ble/bash/read-timeout 1 -r -u "${perlre_server_bgproc[0]}" ret
[[ $ret ]]
}