Skip to content

Commit

Permalink
add zsh support
Browse files Browse the repository at this point in the history
  • Loading branch information
seveniruby committed May 18, 2021
1 parent abc4edd commit fbc7aa8
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 52 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@


## BashGems
这是TesterHome TTF旗下的一个开源项目,用于增强Bash功能,BashGems通过加载各类有用的函数实现对系统命令的扩展

用于增强 Bash 功能,BashGems 通过加载各类有用的函数实现对系统命令的扩展,对标 python 中的 pip

## Get Start

quick install

```bash
eval "$(curl https://raw.githubusercontent.com/seveniruby/bashgems/master/bin/bashgems.sh);bashgems_install"
```

## Use

```bash
# 霍格沃兹测试工具包,显示所有的学院提供的帮助命令
hogwarts help
Expand All @@ -18,13 +20,10 @@ proxy npm install -g appium --verbose
# 显示chromedriver与chrome的版本对应关系和下载地址
chromedriver_list
```
为了让测试工程师安装自动化测试工具更方便,我们提供了proxy命令来确保安装顺利。
更多功能详见代码。

## More
- TesterHome: https://testerhome.com
- TTF: https://testerhome.com/topics/15522
- BashGems: https://github.com/seveniruby/bashgems.git
为了让测试工程师安装自动化测试工具更方便,我们提供了 proxy 命令来确保安装顺利。
更多功能详见代码。

## Maintainers

霍格沃兹测试学院
30 changes: 13 additions & 17 deletions bin/bashgems.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bashgems_install() {
if [ -d ~/.bashgems ]; then
cd ~/.bashgems
git pull
cd $OLDPWD
cd $OLDPWD
else
git clone https://github.com/seveniruby/bashgems.git ~/.bashgems
fi
Expand All @@ -56,26 +56,22 @@ bashgems_install() {
}

logo() {
echo '
# __ __ __
# / /____ _____/ /____ _____/ /_ ____ ____ ___ ___
# / __/ _ \/ ___/ __/ _ \/ ___/ __ \/ __ \/ __ `__ \/ _ \
# / /_/ __(__ ) /_/ __/ / / / / / /_/ / / / / / / __/
# \__/\___/____/\__/\___/_/ /_/ /_/\____/_/ /_/ /_/\___/
'
:
}

#default library
bashgems_init() {
logo
echo "TesterHome: https://testerhome.com"
echo "TTF: https://testerhome.com/topics/15522"
echo "BashGems: https://github.com/seveniruby/bashgems.git"
echo
[ -f $BASHGEMS_HOME/lib/shellex.sh ] && . $BASHGEMS_HOME/lib/shellex.sh
[ -f $BASHGEMS_HOME/lib/hogwarts.sh ] && . $BASHGEMS_HOME/lib/hogwarts.sh
# mo only for bash
# [ -f $BASHGEMS_HOME/lib/cgi/mo ] && . $BASHGEMS_HOME/lib/cgi/mo
[ -f $BASHGEMS_HOME/lib/hogwarts/hogwarts.sh ] && . $BASHGEMS_HOME/lib/hogwarts/hogwarts.sh
:
}

#########################
[ -z "$BASHGEMS_HOME" ] && export BASHGEMS_HOME=~/.bashgems
bashgems_init "$@"
:
export BASHGEMS_HOME=$(cd $( [ -n "$BASH" ] && dirname $BASH_SOURCE; [ -n "$ZSH_NAME" ] && dirname $0 )/../ && echo $PWD)
bashgems_init

for f in "$@"; do
$f
done
229 changes: 203 additions & 26 deletions lib/shellex.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tmp=/tmp/${USER}_tmp

defineY() {
set | grep "^$1="
set | grep "^$1="
}

: <<sss_help
Expand All @@ -11,10 +9,10 @@ sss -u dinglh
sss -s session_name
sss_help
exscreen() {
local s
ARGS="tmp s" pp "$@"
local s
ARGS="tmp s" pp "$@"

echo -e '
echo -e '
multiuser on
hardstatus on
hardstatus alwayslastline
Expand All @@ -23,31 +21,210 @@ exscreen() {
hardstatus string "%{= kG}%-w%{.cW}%n %t%{-}%+w %=%{= kG} %H %{..Y} %Y/%m/%d %c"
logfile '"/tmp/${USER}_${s:=$USER}.log"'
' >$tmp
echo session_name=$s
screen -ArxRL ${s:=$USER} -c $tmp -t ${s:=$USER}
echo session_name=$s
screen -ArxRL ${s:=$USER} -c $tmp -t ${s:=$USER}
}
screen_ex() {
exscreen "$@"
exscreen "$@"
}
ps_ex() {
ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command "$@"
ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command "$@"
}

parallel() {
[ $# -lt 2 ] && ls | echo parallel 10 "echo $index" && return
local p=$1
shift
local cmd="$@"
echo cmd="$cmd"
echo parallel=$p
while true; do
local count=$(jobs -l | grep Running | wc -l)
echo running count=$count
if ((count >= p)); then
sleep 1
else
echo "run $cmd"
eval $cmd &
fi
done
[ $# -lt 2 ] && ls | echo parallel 10 "echo $index" && return
local p=$1
shift
local cmd="$@"
echo cmd="$cmd"
echo parallel=$p
while true; do
local count=$(jobs -l | grep Running | wc -l)
echo running count=$count
if ((count >= p)); then
sleep 1
else
echo "run $cmd"
eval $cmd &
fi
done
}

#key=2 value=1 include="a c" exclude="b" include_and_exclude
include_and_exclude() {
local sep
local key
local value
local include
local exclude
awk -v sep="$sep" \
-v key="$key" \
-v value="$value" \
-v include_keys="$include" \
-v exclude_keys="$exclude" \
'
BEGIN{
if(sep!="") FS=sep
if(key=="") key=0
if(value=="") value=key
include_count=split(include_keys,include_list," ");
exclude_count=split(exclude_keys,exclude_list," ");
}
{
if(include_count==0){
if( $0~include_list[i] && black[$key]!=1 )
{s[$key]=$value}
}else{
for(i=1;i<=include_count;i++)
if( $0~include_list[i] && black[$key]!=1 )
{s[$key]=$value}
}
}
{
for(i=1;i<=exclude_count;i++) if( $0~exclude_list[i]) {black[$key]=1; delete s[$key]}
}
END{
for(k in s) print k, s[k]
}
'
}

#f(){ eval local x=$(get_temp_file 2 m 3 ); echo $x; }
get_temp_file() {
local t

local r
for p in "$@"; do
[ "$p" = "%S" ] && p=$(date +%Y-%m-%dT%H:%M:%S)
[ "$p" = "%M" ] && p=$(date +%Y-%m-%dT%H:%M)
[ "$p" = "%H" ] && p=$(date +%Y-%m-%dT%H)
[ "$p" = "%d" ] && p=$(date +%Y-%m-%d)

r=$r.$p
done
echo "/tmp/\$FUNCNAME$r"
}


#f(){ local log=$(log_file %M) }
log_file() {
local r
for p in "$@"; do
[ "$p" = "%S" ] && p=$(date +%Y-%m-%dT%H:%M:%S)
[ "$p" = "%M" ] && p=$(date +%Y-%m-%dT%H:%M)
[ "$p" = "%H" ] && p=$(date +%Y-%m-%dT%H)
[ "$p" = "%d" ] && p=$(date +%Y-%m-%d)

r=$r.$p
done
caller 0 | awk -v t="$r" '{print $NF"."$(NF-1)t".log"}'
}

test_log_file(){
local log=$(log_file %M)
echo log=$log
}




#python dict for shell
#dict a 1
#dict a
#key=a dict
#key=a value=1 dict
dict() {
local key k
local value v
[ -z "$key" ] && key="$k"
[ -z "$value" ] && value="$v"
[ -z "$key" ] && key="$1"
[ -z "$value" ] && value="$2"

for i in ${!_ArrayKeyGlobal[@]}; do
if [ "${_ArrayKeyGlobal[i]}" = "$key" ]; then
if [ -n "$value" ]; then
#overwrite
_ArrayValueGlobal[i]="$value"
elif [ -z "$value" ]; then
#get
echo "${_ArrayValueGlobal[i]}"
fi
return
fi
done
#new
if [ -n "$value" ]; then
#put
_ArrayKeyGlobal+=("$key")
_ArrayValueGlobal+=("$value")
return
elif [ -z "$value" ]; then
#not exist
return 1
fi

}

test_dict() {
unset _ArrayKeyGlobal _ArrayValueGlobal
dict a 1
dict b "2 3"
[ "$(dict a)" = 1 ] || echo error
[ "$(dict b)" = "2 3" ] || echo error

k=a v=1 dict
k=b v="2 3" dict
[ "$(dict a)" = 1 ] || echo error
[ "$(dict b)" = "2 3" ] || echo error

key=a value=1 dict
key=b value="2 3" dict
[ "$(dict a)" = 1 ] || echo error
[ "$(dict b)" = "2 3" ] || echo error

dict c && echo error

}

#set and get return value
# seveniruby:~ seveniruby$ f(){
# > echo 1 2
# > return_value 3 4
# > }
# seveniruby:~ seveniruby$ f
# 1 2
# seveniruby:~ seveniruby$ return_value
# 3 4
return_value(){
[ $# -ge 1 ] && _RETURN_VALUE="$@" || echo $_RETURN_VALUE
}


#timestamp 2020-04-16
#format="%Y-%m-%d" timestamp -1d
timestamp() {
local format
[ -z "$format" ] && format="%s"

local param="${@:-+0}"
local result

#depend gawk
if echo "$param" | grep '^[+-]' >/dev/null; then
result=$(awk -v diff=$(echo "0$param" | sed 's#d#*3600*24#' | bc) 'BEGIN{print systime()+diff}')
else
result=$(echo "$param" | awk -F ' |-|:' '{for(i=1;i<=6;i++) {if($i=="") $i="00";s=s$i" "};print mktime(s)}')
fi
if [ -n "$format" ]; then
awk -v result="$result" -v format="$format" 'BEGIN{print strftime(format, result)}'
else
echo "$result"
fi
}


#echo '{"a": 1, "b": [3,4]}' | json_parse "j=json.loads(sys.stdin.read());print(j['b'])"
json_parse(){
python -c 'import sys; import os; import json;'"$@"
}

0 comments on commit fbc7aa8

Please sign in to comment.