文章

zsh

zsh

zsh

什么是 zsh?

Zsh 也被称为 z Shell,它扩展了 Bourne Shell (sh) 的功能,提供了更新的特性以及对插件和主题的更多支持。从 2019 年的 MacOS Catalina 开始,Zsh 成为 Mac 电脑中默认的登录和交互式 shell。
Zsh 本身既不美观也不智能,但它具有可扩展性,因此可以应用社区开发的插件使其美观而强大。
Zsh 非常受欢迎,它被发布到几乎所有 Unix 发行版 (Ubuntu、Centos、macOS 等) 的包存储库中,因此您可以使用包管理器轻松安装 Zsh

antigen 用于 zsh 的插件管理器

什么是 antigen?

https://github.com/zsh-users/antigen

Zsh 支持可插拔性,以便用户可以安装插件并扩展其功能。但是 Zsh 本身并没有提供很好的插件管理机制,包括获取、安装、更新、移除插件等。
Antigen 很好地承担了这个责任。大多数 Zsh 插件都以 git 存储库的形式发布。 Antigen 允许我们简单地指定远程存储库的路径,然后它会在第一次运行时自动获取和安装。 Antigen 还提供了轻松更新和删除插件的命令。

安装 antigen

Installation · zsh-users/antigen Wiki · GitHub

  • curl
1
curl -L git.io/antigen > antigen.zsh

配置

https://github.com/zsh-users/antigen#usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## 导入antigen.zsh文件
source $HOME/antigen.zsh

## Load the oh-my-zsh's library. # 使用ohmyzsh
antigen use oh-my-zsh

## 添加要使用的ohmyzsh插件,可以在ohmyzsh项目plugins文件夹下找到
antigen bundle zsh-users/zsh-syntax-highlighting # 终端命令语法高亮
antigen bundle zsh-users/zsh-autosuggestions
antigen bundle zsh-users/zsh-completions # 命令自动补全 键盘命令自动补全 键盘 https://github.com/zsh-users/zsh-completions

antigen bundle extract
antigen bundle sublime
antigen bundle autojump
antigen bundle git

antigen bundle node
antigen bundle npm
antigen bundle yarn

antigen bundle pip
antigen bundle python
antigen bundle pip3
antigen bundle python3

antigen bundle fastlane
antigen bundle mac

antigen bundle command-not-found

# ZSH Config
DEFAULT_USER="hacket"
ZSH_DISABLE_COMPFIX=true
# ZSH_THEME="agnoster"

# Load the theme.
# antigen theme robbyrussell
# fixApplyTheme
[[ ! -z "$HasApplyThemeTag" ]] && echo "pass apply theme" || antigen theme robbyrussell/oh-my-zsh themes/agnoster
export HasApplyThemeTag=true

# Tell Antigen that you're done.
antigen apply

oh my zsh

见下面

Oh My Zsh

Oh my zsh 是什么?

Oh my Zsh是一个 Zsh 配置框架,它嵌入了一堆插件和主题;zsh 没人用的原因就是过于复杂。

Oh my zsh 安装?

  1. curl 安装

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

安装好 oh my zsh 后,在 Mac 的用户根目录下会多了 .zshrc 配置文件和 .oh-my-zsh 目录。均为隐藏文件,通过快捷键 ` command + shift + . ` 可以快速显示/隐藏

1
2
3
4
5
6
.oh-my-zsh文件
├───plugins       # 自带插件目录,每个插件目录下都有一个 README,有详细使用说明
├───themes        # 自带主题目录
└───custom        # 用户自定义目录
    ├───plugins   # 第三方插件目录
    └───themes    # 第三方主题目录

zsh 字体

powerline fonts

1
2
3
4
5
6
7
8
# clone
git clone https://github.com/powerline/fonts.git --depth=1
# install
cd fonts
./install.sh
# clean-up a bit
cd ..
rm -rf fonts

nerd-fonts

nerd font 很多安装方式,推荐用 homebrew
homebrew 安装 nerd fonts

1
2
brew tap homebrew/cask-fonts
brew install font-hack-nerd-font

zsh 插件

推荐插件:

  • git:如效果图所见,显示 git 项目信息,还有各种 git 命令快捷方式;
  • z:快速目录跳转, cd 进入过某个目录后,可直接通过 z 目录名 快速跳转,而不需要输入完整路径;
  • zsh-syntax-highlighting:常见命令高亮,输入错误为红色,正确为绿色,比如 cd,ls,clear 等;
  • zsh-autosuggestions:命令提示,输入时会灰字提示推荐命令,直接键盘➡️补全,并不是 tab 键;

注意点:其中 git 和 z 插件是 oh my zsh 自带的,可直接配置.zshrc 文件 使用。另外两个需要安装

git

查看所有 git 插件的配置:~/.oh-my-zsh/plugins/git/git.plugin.zsh

设置前的 git 命令设置后的 git 命令
git add –allgaa
git branch -DgbD
git commit -a -mgcam
git checkout -bgcb
git checkout mastergcm
git clone –recurse-submodulesgcl

zsh-syntax-highlighting

在 oh my zsh 中安装:

1
2
3
4
5
6
# 下载插件
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 命令会将插件git clone 到 ~/.oh-my-zsh/custom/plugins 用户自定义插件目录下

# 在~/.zshrc激活插件
plugins=( [plugins...] zsh-syntax-highlighting)

autosuggestions 输入命令时会显示自动补全提示

安装教程:zsh-autosuggestions/INSTALL.md at master · zsh-users/zsh-autosuggestions · GitHub

在 oh my zsh 中安装:

1
2
3
4
5
6
7
8
# 下载插件
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

# 在~/.zshrc中激活
plugins=(
    # other plugins...
    zsh-autosuggestions
)

homebrew 安装:

1
2
3
4
5
6
7
8
# 通过homebrew安装zsh-autosuggestions
brew install zsh-autosuggestions

# 在.zshrc文件配置插件
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

# .zshrc文件 source zsh-autosuggestions.zsh
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh

配置 zsh 主题

~/.zshrc 中配置 ZSH_THEME

1
2
3
4
5
6
# theme
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="agnoster"
# ZSH_THEME="powerlevel9k/powerlevel9k"

Oh my zsh 主题查看:在 ~/.oh-my-zsh/themes
c3ny9

系统主题

第三方主题

powerlevel9k(p9k) zsh 下最棒的 Powerline 主题

Powerlevel9k 主题可以用于 vanilla ZSH 或 ZSH 框架,如 oh-my-ZSH、 Prezto、 Antigen 等。
Powerlevel9k 的四个主要目标:

  • 开箱即用:让用户无需过多配置即可拥有一个让人眼前一亮的主题
  • 极易定制:为那些想调整提示符的用户,提供更加语义化和模块化的方式直接在 ~/.zshrc 中进行配置。Powerlevel9k 将提示符分割成不同的分段,用户可以使用这些内置的分段来自由组合最终的提示符。
  • 提供丰富的配置分段 (Segments):内置分段涵盖使用场景非常多;从表示计算机状态的分段到单元测试覆盖率的分段再到 AWS 实例的分段,都有提供。
  • 保证主题渲染的速度:Powerlevel9k 尽可能优化代码以提高执行速度

安装 Powerlevel9k
涉及两个步骤:

powerlevel10k

提供漂亮的提示符,可以显示当前路径、时间、命令执行成功与否,还能够支持 git 分支显示等等

  • 安装 powerline 字体/nerd 字体
  • 安装 powerlevel10k 主题

主题 powerlevel 10k,克隆到 Oh My Zsh 的主题目录,方式如下:

git clone –depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

然后去 ~/.oh-my-zsh/custom/themes/ 目录下看看有没有 powerlevel10k 就知道有没有安装成功了。

  • 配置 Oh My Zsh

    在 ~/.zshrc 中设置 ZSH_THEME="powerlevel10k/powerlevel10k" 即可,

  • 最后 source ~/.zshrc,然后会引导你做一些配置,按需选择即可

bp10l

  • 如果想重新配置,执行 p10k configure,并在最后一步选择覆盖原有的配置文件。

配色

solarized

solarized 是目前最完整的配色项目了,几乎覆盖主流的操作系统(Mac OS X,Linux,Windows)、编辑器和 IDE(Vim,Emacs,Xcode 等)、终端(Iterm2, Terminal.app 等)
下载链接
在 solarized/iterm2-colors-solarized 下双击 Solarized Dark.itermcolorsSolarized Light.itermcolors 两个文件就可以把配置文件导入到 iTerm2 里
iTerm→Preferences→Colors→color Presets→选择solarized Dark或Light

其他配置

支持中文,防止乱码

1
2
3
4
# encode
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

大佬完整的.zshrc 配置

大圣.zshrc单独完整配置

zsh终端环境配置.zshrc 截止2023年08月19日.zip
easyproxy_proxyman.sh
easyproxy.sh
script_of_justice.sh
work.sh
click.sh
git-batch.py

大圣 zshrc 和 shell 脚本分开

  • .zshrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
source /etc/profile

#################################################################################################
############################################ antigen ############################################
#################################################################################################

## 导入antigen.zsh文件
source $HOME/antigen.zsh

## Load the oh-my-zsh's library. # 使用ohmyzsh
antigen use oh-my-zsh

## 添加要使用的ohmyzsh插件,可以在ohmyzsh项目plugins文件夹下找到
antigen bundle zsh-users/zsh-syntax-highlighting # 终端命令语法高亮
antigen bundle zsh-users/zsh-autosuggestions # 支持Zsh终端输入代码补全建议
# antigen bundle zsh-users/zsh-completions # 命令自动补全 键盘命令自动补全 键盘 https://github.com/zsh-users/zsh-completions
# antigen bundle zsh-users/zsh-history-substring-search # 支持方向键上下移动按关键字搜索历史命令 https://github.com/zsh-users/zsh-history-substring-search/issues/113
# antigen bundle zsh-users/zsh-apple-touchbar

antigen bundle extract
antigen bundle sublime
antigen bundle autojump
antigen bundle git

antigen bundle node
antigen bundle npm
antigen bundle yarn

antigen bundle pip
antigen bundle python
antigen bundle pip3
antigen bundle python3

antigen bundle fastlane
antigen bundle mac

antigen bundle command-not-found

# ZSH Config
DEFAULT_USER="hacket"
ZSH_DISABLE_COMPFIX=true
# ZSH_THEME="agnoster"

# Load the theme.
# antigen theme robbyrussell
# fixApplyTheme
[[ ! -z "$HasApplyThemeTag" ]] && echo "pass apply theme robbyrussell" || antigen theme robbyrussell/oh-my-zsh themes/agnoster
export HasApplyThemeTag=true

# Tell Antigen that you're done.
antigen apply


# # # oh my zsh
# if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
#   source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
# fi

# # 指定了命令行终端的主题,这里使用的是 Powerlevel10k 主题
# ZSH_THEME="powerlevel10k/powerlevel10k"
# [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh



# ## default EDITOR
# export EDITOR="vi"
# ## source ohmyzsh
# source $ZSH/oh-my-zsh.sh

# ## encode
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

# ## To customize prompt, run `p10k configure`


#################################################################################################
############################################# PATH ##############################################
#################################################################################################

# PATH
## Gradle
GRADLE_HOME=$HOME/Library/gradle-4.6
export GRADLE_HOME
GRADLE_USER_HOME=$HOME/.gradle
export GRADLE_USER_HOME
## Android
ANDROID_HOME=$HOME/Library/Android/sdk
export ANDROID_HOME
## Flipper use
ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
export ANDROID_SDK_ROOT
## JAVA
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
export JAVA_HOME

## Flutter

### Flutter镜像
#### 社区镜像
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

#### 腾讯开源的镜像站(与TUNA同步,定时更新)
# export PUB_HOSTED_URL=https://mirrors.cloud.tencent.com/dart-pub
# export FLUTTER_STORAGE_BASE_URL=https://mirrors.cloud.tencent.com/flutter

#### 上海交通大学 Linux 用户组的镜像站(与原站实时更新)
# export PUB_HOSTED_URL=https://dart-pub.mirrors.sjtug.sjtu.edu.cn
# export FLUTTER_STORAGE_BASE_URL=https://mirrors.sjtug.sjtu.edu.cn

#### CNNIC的镜像站 (与TUNA同步)
# export PUB_HOSTED_URL=http://mirrors.cnnic.cn/dart-pub
# export FLUTTER_STORAGE_BASE_URL=http://mirrors.cnnic.cn/flutter

#### 清华大学 TUNA 协会的镜像站(与Flutter社区同步,推荐)
# export PUB_HOSTED_URL=https://mirrors.tuna.tsinghua.edu.cn/dart-pub
# export FLUTTER_STORAGE_BASE_URL=https://mirrors.tuna.tsinghua.edu.cn/flutter

### 单个Flutter版本
# export FLUTTER3_HOME=~/flutter/flutter3
# export FLUTTER2_HOME=~/flutter/flutter_macos_2.10.5-stable
# export FLUTTER_HOME=/Users/xxx/WorkSpace/xxx/yyy_flutter_module/.flutter
# export PATH=$FLUTTER3_HOME/bin:$FLUTTER2_HOME/bin:$FLUTTER_HOME/bin:$PATH

### fvm管理多个Flutter版本
export FVM_HOME=$HOME/fvm
export FLUTTER_ROOT=$FVM_HOME/default
export PATH=~/fvm/default/bin:$PATH


export PATH=$HOME/.pub-cache/bin:$PATH

export PATH=/bin:/sbin:/usr/bin/:$HOME/bin:/usr/local/bin:$PATH

## Android
export PATH=$GRADLE_HOME/bin:$$ANDROID_SDK_ROOT:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools/34.0.0:$PATH

## HomeBrew
export PATH=/opt/homebrew/bin:$PATH

## crontab -e用vscode打开编辑
export VISUAL=~/code-wait.sh
export EDITOR=~/code-wait.sh
function crontab:vscode() {
  EDITOR='code' crontab -e
}


#################################################################################################
############################################ zshrc ##############################################
#################################################################################################
export ZSHRC_DIR=${${(%):-%x}:A:h} # 获取当前脚本的绝对路径
export ZSHRC=$ZSHRC_DIR/.zshrc
export COMMON_PROFILE=$ZSHRC_DIR/.common_profile.sh

[ -f $COMMON_PROFILE ] && source $COMMON_PROFILE # 存在COMMON_PROFILE文件就加载

  • .common_profile.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
#!/bin/zsh

echo ".common_profile.sh start work"


#################################################################################################
############################################ 一些变量 ############################################
#################################################################################################

MAIN_APP_ID=com.zzz # 主要开发的APP xxx
MAIN_APP_SPLASH=$MAIN_APP_ID/com.xxx.welcome.WelcomeActivity
SECOND_APP_ID=com.yyy # 次要开发的APP yyy
SECOND_APP_SPLASH=$SECOND_APP_ID/com.yyy.work.home.SplashUI

WEWORK_APP=com.tencent.wework # 企业微信
WX_APP=com.tencent.mm # 微信
KING_APP=me.hacket.assistant.samples # 大圣助手king
DEVKIT_APP=me.hacket.assistant.devkit # devkit辅助工具

WORKSPACE_MAIN=~/Workspace/xxx # 主工程
WORKSPACE_BUG=~/Workspace/bugfix/xxx # bug工程

GITS=~/py/git-batch/git-batch.py # git多仓库拉取的py脚本

HACK_HOME=${HOME}/software/decompile # 存放反编译脚本的目录
CHARLES_HOME=${HOME}/charles

CHARLES_PROXY_PORT=8888
PROXY_PORT_PROXYMAN=9091

ANDROID_HOME=$HOME/Library/Android/sdk
GRADLE_HOME=$HOME/Library/gradle-4.6
GRADLE_USER_HOME=$HOME/.gradle
ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
# FLUTTER_HOME=~/flutter

VISUAL=~/code-wait.sh
EDITOR=~/code-wait.sh

SHELL_HOME=~/.sh # 存放shell脚本主目录
USER_HOME=/Users/xxx # 用户目录

alias adb='~/Library/Android/sdk/platform-tools/adb'



#################################################################################################
############################################# PATH ##############################################
#################################################################################################

# PATH
## Gradle
# export GRADLE_HOME
# export GRADLE_USER_HOME
# ## Android
# export ANDROID_HOME
# export ANDROID_SDK_ROOT ## Flipper use
# ## JAVA
# export JAVA_HOME
# ## Flutter
# export FLUTTER_HOME
# export PUB_HOSTED_URL=https://pub.flutter-io.cn
# export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
# export PATH="$PATH":"$HOME/.pub-cache/bin"

# export PATH=/bin:/sbin:/usr/bin/:$HOME/bin:/usr/local/bin

# ### Android
# export PATH=$GRADLE_HOME/bin:$ANDROID_HOME:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$FLUTTER_HOME/bin:$PATH

# ### HomeBrew
# export PATH=/opt/homebrew/bin:$PATH

# ## crontab -e用vscode打开编辑
# export VISUAL
# export EDITOR
# function crontab:vscode() {
#   EDITOR='code' crontab -e
# }

#################################################################################################
############################################ alias #############################################
#################################################################################################

# alias
## common
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim'

## md5/sha1
alias check:md5='md5'
alias check:sha1='openssl dgst -sha1'
alias check:sha256='openssl dgst -sha256's
alias check:sha512='openssl dgst -sha512'


#################################################################################################
############################################ common #############################################
#################################################################################################

function checkOpts() {
    local key=$1
    if [[ -z "${key}" ]] ; then
        echo -e "\033[31mFATAL: $2 should not be empty! \033[0m"
        return 0
    fi
}

##############################################
################## honebrew ##################
##############################################
alias brew:info='brew info'
alias brew:info:cask='brew cask info'
alias brew:install='brew install'
alias brew:install:cask='brew cask install'
alias brew:uninstall='brew uninstall'
alias brew:uninstall:cask='brew cask uninstall'
alias brew:reinstall='brew reinstall --force'
alias brew:reinstall:cask='brew cask reinstall --force'
alias brew:search='brew search'
alias brew:doctor='brew doctor'
alias brew:update='brew update'
alias brew:upgrade='brew upgrade; brew upgrade --cask;'
alias brew:cleanup='brew cleanup;brew cask cleanup;'
alias brew:repair='sudo chown -R $(whoami) $(brew --prefix)/*'
alias brew:ls='brew:list'
### 列举brew已经安装的软件
function brew:list() {
    echo "\n\033[42;30m brew list: \033[m"
    brew list
    # echo "\n\033[42;30m brew list --cask: \033[m"
    # brew list --cask
    echo ""
}

### brew 安装必要QuickLook插件
function brew:quicklook:init() {
    brew install qlcolorcode # 高亮代码
    brew install qlstephen # 无后缀名的文本文件
    brew install qlmarkdown # markdown
    brew install quicklook-json # json
    brew install quicklook-csv # 更好查看csv文件
    brew install quicklook-pat
    brew install qlprettypatch
    brew install qlimagesize
    brew install webpquicklook
    brew install quicklookapk
    brew install betterzip # 预览zip文件
    brew install suspicious-package
    brew install apparency
    brew install quicklookase
    brew install qlvideo # 预览视频
    brew install provisionql
}

### brew必要的Tools:
function brew:tool:init() {
    brew install tree
    brew install exa
    brew install bat
    brew install fzf
    brew install dust
    brew install delta
    brew install glances
    brew install neofetch
    brew install bandwhich
    brew install grex
    brew install tealdeer
    brew install tokei
    brew install ripgrep
    brew install procs
    brew install jadx # https://github.com/skylot/jadx#install
    brew install apktool # https://apktool.org/docs/install/#brew
    brew install scrcpy # scrcpy:https://github.com/Genymobile/scrcpy/blob/master/doc/macos.md
    brew install android-platform-tools # scrcpy依赖的adb
    brew install shellcheck # shell脚本语法检查 https://github.com/koalaman/shellcheck
    brew install launchcontrol
    # brew tap dart-lang/dart # dart https://dart.dev/get-dart#install
    # brew install dart
}

##############################################
#################### git #####################
##############################################
## git
# Git https://blog.csdn.net/dwarven/article/details/46550117
function git:author:lines() {
    userName=""
    if [ -z $1 ]; then
        userName=`git config --get user.name`
    else
        userName="$1"
    fi
    echo " $userName :"
    git log --author="$userName" --pretty=tformat: --numstat | awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf " Added   Lines: %s \n Removed Lines: %s \n Total   Lines: %s\n",add,subs,loc }'
}
function git:majority:author:name() {
    topCount=10
    if [ ! -z $1 ]; then
        topCount=$1
    fi
    git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n $topCount
}
function git:majority:author:email() {
    topCount=10
    if [ ! -z $1 ]; then
        topCount=$1
    fi
    git log --pretty=format:%ae | awk -- '{ ++c[$0]; } END { for(cc in c) printf "%5d %s\n",c[cc],cc; }' | sort -u -n -r | head -n $topCount
}
function git:count:author() {
    git log --pretty='%aN' | sort -u | wc -l
}
function git:count:commit() {
    git log --oneline | wc -l
}
function git:tag:cleanup() {
    git tag | xargs git tag -d
}

function git:submodule() {
  git submodule # 查看当前repo的submodule
}
function git:submodule:add() {
  git submodule add $1 $2 # 在当前路径add submodule
}
function git:submodule:update() {
  git submodule update --remote # 以checkout的方式更新
}
function git:submodule:delete() {
  git rm --cached $1
  # git rm --cached <本地路径>
  rm -rf $1
  rm .gitmodules
}

##############################################
################## gradle ####################
##############################################
## Gradle
alias gw='./gradlew' # gradlew命令
function g:clean() {
  ./gradlew clean
}
function g:aDebug() {
  ./gradlew assembleDebug
}
function g:aRelease() {
  ./gradlew assembleRelease
}


##############################################
#################### proxy ###################
##############################################
function proxyhelper() {
    local operator=$1 #=号两边不能有空格
    if [[ -z "${operator}" ]] ; then
        echo -e "\033[31mFATAL: 请输入合法的proxyhelper命令: \033[0m"
        echo "设置代理:本机IP:8888       proxyhelper set"
        echo "设置代理:本机IP:port       proxyhelper set port"
        echo "设置代理: ip:port         proxyhelper set ip port"
        echo "获取当前代理:              proxyhelper get"
        echo "删除代理:                 proxyhelper clear"
        return 0
    fi
    if [[ "$operator" == "set" ]];then
        if [ $# -eq 3 ]; then # set后2个参数的是ip:port
            IP=$2
            PROT=$3
            echo "设置自定义代理 $IP:$PROT"
            adb shell settings put global http_proxy $IP:$PROT
        elif [ $# -eq 2 ]; then # set后1个参数的是port
            PROT=$2
            # 获取 IP
            ip=$(/sbin/ifconfig | /usr/bin/sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
            # echo $ip
            count=$(echo $ip | /usr/bin/tr ' ' '\n' | /usr/bin/wc -l )
            if [ $count -gt 1 ];then
                echo "多个ip, 请手动选择一个"
                exit
            fi
            default_proxy=${ip}":$PROT"
            echo "设置代理为本机IP: $default_proxy"
            adb shell settings put global http_proxy $default_proxy
        else # 0个参数的是默认端口:8888
            # 获取 IP
            ip=$(/sbin/ifconfig | /usr/bin/sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
            # echo $ip
            count=$(echo $ip | /usr/bin/tr ' ' '\n' | /usr/bin/wc -l )
            if [ $count -gt 1 ];then
                echo "多个ip, 请手动选择一个"
                exit
            fi
            default_proxy=${ip}":$CHARLES_PROXY_PORT"
            echo "设置代理为本机IP(默认port:8888): $default_proxy"
            adb shell settings put global http_proxy $default_proxy
        fi
    elif [[ $operator == "get" ]];then
        echo "当前代理:"
        adb shell settings get global http_proxy
    elif [[ $operator == "clear" ]];then
        echo "清除代理成功!"
        adb shell settings put global http_proxy :0
        # 下面的方式需要重启手机
        # adb shell settings delete global http_proxy
        # adb shell settings delete global global_http_proxy_host
        # adb shell settings delete global global_http_proxy_port
    else
        echo -e "\033[31mFATAL: 请输入合法的proxyhelper命令: \033[0m"
        echo "设置代理:本机IP:8888       proxyhelper set"
        echo "设置代理:本机IP:port       proxyhelper set port"
        echo "设置代理: ip:port         proxyhelper set ip port"
        echo "获取当前代理:              proxyhelper get"
        echo "删除代理:                 proxyhelper clear"
    fi
}
## charles proxy抓包
function proxy:on() { # 设置charles全局代理
  proxyhelper set $CHARLES_PROXY_PORT
  # ~/.sh/easyproxy.sh set
}
function proxyman:on() { # 设置proxyman全局代理
  proxyhelper set $PROXY_PORT_PROXYMAN
}
function proxy:get() { # 获取当前代理
  proxyhelper get
}
function proxy:off() { # 清除charles全局代理
  proxyhelper clear
}


##############################################
#################### adb #####################
##############################################

function adb:focus() { # 查看当前焦点Activity并复制到剪切板
  # result=`adb shell dumpsys window |grep mCurrentFocus`
  result=$(adb shell dumpsys window |grep mCurrentFocus)
  echo $result
  echo $result | pbcopy
}

function adb:input() { # 模拟输入
  adb shell input text "$1"
}
function adb:install() {
  adb install -r -t "$1"
}
function adb:uninstall() {
  echo 'uninstall '$MAIN_APP_ID
  adb uninstall $MAIN_APP_ID
}
function adb:uninstall:yyy() {
  echo 'uninstall '$SECOND_APP_ID
  adb uninstall $SECOND_APP_ID
}
function adb:5037() {
  lsof -i tcp:5037 ##查看占用5037
}
function adb:kill() {
  adb kill-server
}

#### 重启某个启动,参数应用包名
function adb:stop() { # 停止一个组件
  adb shell am force-stop $1
}
function adb:stop:xxx() {
  adb:stop $MAIN_APP_ID
}
function adb:stop:yyy() {
  adb:stop $SECOND_APP_ID
}

function adb:start() { # 显示启动一个组件
  adb shell am start -n $1
}
function adb:start:intent() { # 显示启动一个组件
  adb shell am start -a android.intent.action.VIEW # -d  http://www.baidu.cn/
}
function adb:start:browser() {
  # 启动浏览器
  # 显示:adb shell am start -n com.android.browser/com.android.browser.BrowserActivity
  # adb:start com.android.browser/com.android.browser.BrowserActivity

  # 隐式:adb shell am start -a android.intent.action.VIEW -d  http://www.baidu.cn/
  adb shell am start -a android.intent.action.VIEW -d  http://www.baidu.cn/
}
function adb:start:via() {
  adb:stop $1
  # adb shell monkey -p $1 -c android.intent.category.LAUNCHER 1
  adb:start mark.via.gp/mark.via.Shell # 启动Via
}
function adb:start:xxx() {
  adb:stop $MAIN_APP_ID
  adb:start $MAIN_APP_SPLASH
}
function adb:start:yyy() {
  adb:stop $SECOND_APP_ID
  adb:start $SECOND_APP_SPLASH
}

#### 重启企业微信
function adb:start:wework() {
  adb shell am force-stop $WEWORK_APP
  adb shell monkey -p $WEWORK_APP -c android.intent.category.LAUNCHER 1
}

### adb debug 调试
function adb:debug:xxx() { # 启动时调试模式
  adb shell am set-debug-app -w --persistent $MAIN_APP_ID
}
function adb:debug:yyy() { # 启动时调试模式
  adb shell am set-debug-app -w --persistent $SECOND_APP_ID
}
function adb:debug:clear() { # 清除启动时的调试模式
  adb shell am clear-debug-app
}
function adb:debug:king() {
  adb shell am set-debug-app -w --persistent $KING_APP
}

### adb applink
function adb:applink() { # 查看applink情况,参数应用包名;不带参数就是查看手机上的所有包
  adb shell pm get-app-links $1
}
function adb:applink:xxx() {
  adb shell pm get-app-links $MAIN_APP_ID
}
function adb:applink:yyy() {
  adb shell pm get-app-links $SECOND_APP_ID
}
function adb:applink:reverify:xxx() {
  adb shell pm verify-app-links --re-verify $MAIN_APP_ID
}
function adb:applink:reverify:yyy() {
  adb shell pm verify-app-links --re-verify $SECOND_APP_ID
}

### others
function adb:debugtools() {
  # 打开辅助开发工具
  adb shell am start -n $DEVKIT_APP/.SettingsToolsActivity
}
function adb:debugtools:test() {
  # 打开辅助开发工具
  adb shell am start -n $DEVKIT_APP/.SettingsToolsActivity
  # App的WebSocket服务端地址
  echo 'ws://10.102.177.66:8800/mock' | pbcopy
  # PC浏览器打开 http://www.websocket-test.com/
  open 'http://www.websocket-test.com/'
}

#### 启动via app mark.via.gp/mark.via.Shell,然后输入文本,最后按回车键
#### 可用来测试deeplink跳转
function adb:via() {
  echo "stop Via app."
  adb shell am force-stop mark.via.gp

  echo "start Via app."
  adb shell am start -n mark.via.gp/mark.via.Shell

  sleep 2
  echo "input tap 500 1033"
  # 模拟按2次tab键盘到tab,才能定位到EditText,好像经常找不到焦点
  # adb shell input keyevent KEYCODE_TAB
  # adb shell input keyevent KEYCODE_TAB
  adb shell input tap 500 1033

  echo "input text $1"
  adb shell input text "$1"

  echo "send keyevent KEYCODE_ENTER"
  adb shell input keyevent KEYCODE_ENTER
}

function adb:leakcanary:xxx() {
  adb:leakcanary $MAIN_APP_ID
}
function adb:leakcanary:yyy() {
  adb:leakcanary $SECOND_APP_ID
}
function adb:leakcanary:king() {
  adb:leakcanary $KING_APP
}
#### 启动/关闭 LeakCanary的Dump heap automatically
function adb:leakcanary() {
  applicationId=$1
  if [[ -z "${applicationId}" ]] ; then
    echo -e "\033[31mFATAL: 请输入合法的applicationId \033[0m"
    return 0
  fi

  echo "start $applicationId App LeakLauncherActivity page"
  adb shell am start -n $applicationId/leakcanary.internal.activity.LeakLauncherActivity

  # 休眠2秒,启动Activity需要一定的时间
  sleep 2

  echo "Simulated click \"About\" tab"
  adb shell input tap 915 2190

  sleep 1

  echo "Simulated click \"Dump heap automatically\""
  adb shell input tap 500 2000

  adb shell input keyevent 4 # 模拟返回键
}

#### deeplink测试,value中如果带了” & 等符合要转义
#### adb:deeplink 'xxxlink://applink/pushtoweb2?data={\"url\":\"https://www.baidu.com?aaa=xx\&bbb=yyy\"}'
function adb:deeplink() {
  # 快速测试deeplink跳转
  echo $1
  #adb shell am start -a android.intent.action.VIEW -d $1
  adb shell am start -a android.intent.action.VIEW -d $1
}
function adb:deeplink:sleep() {
  # 模拟sleep 8秒后跳转
  sleep 8
  # 快速测试deeplink跳转
  adb shell am start -a android.intent.action.VIEW -d $1
}

# 模拟测试fb ddl的流程: xxx
function adb:fbddl:xxx() {
    APK_BUILD_PATH=$WORKSPACE_MAIN/xxx_android/xxx/build/outputs/apk/
    test_link_splash=com.facebook.katana/com.facebook.katana.LoginActivity
    adb:deferredlink $MAIN_APP_ID $APK_BUILD_PATH $test_link_splash
}
# 模拟测试fb ddl的流程: yyy
function adb:fbddl:yyy() {
    APK_BUILD_PATH=$WORKSPACE_MAIN/xxx_android/xxx/build/outputs/apk/
    test_link_splash=com.facebook.katana/com.facebook.katana.LoginActivity
    adb:deferredlink $SECOND_APP_ID $APK_BUILD_PATH $test_link_splash
}

# 模拟测试gpir的流程: xxx
function adb:gpir:xxx() {
    APK_BUILD_PATH=$WORKSPACE_MAIN/xxx_android/xxx/build/outputs/apk/
    test_link_splash=com.pushbullet.android/com.pushbullet.android.ui.LaunchActivity
    adb:deferredlink $MAIN_APP_ID $APK_BUILD_PATH $test_link_splash
}
# 模拟测试gpir的流程: yyy
function adb:gpir:yyy() {
    APK_BUILD_PATH=$WORKSPACE_MAIN/yyy_android/app/build/outputs/apk/
    test_link_splash=com.pushbullet.android/com.pushbullet.android.ui.LaunchActivity
    adb:deferredlink $SECOND_APP_ID $APK_BUILD_PATH $test_link_splash
}

# 模拟测试deferredlink的流程
function adb:deferredlink() {
    echo '参数:' $@
    # 校验输入参数的正确性:
    ## 参数1:应用包名,如:me.hacket.assistant
    ## 参数2:存放apk包的路径,如:相对路径也可以
    ## 参数3:splas页,通过什么APP的启动页点击链接去到Google Play的,如:com.pushbullet.android/com.pushbullet.android.ui.LaunchActivity
    if [ $# -lt 2 ]; then # 大于或等于2个参数
      echo -e "\033[31mFATAL 请输入至少2个参数,参数1:应用包名,参数2:包路径 \033[0m"
      echo "当前参数 $*"
      return 0
    fi
    applicationId=$1
    if [[ -z "${applicationId}" ]] ; then
      echo -e "\033[31mFATAL: 请输入合法的applicationId: \033[0m"
      return 0
    fi
    apk_build_path=$2
    if [[ -z "${apk_build_path}" ]] ; then
      echo -e "\033[31mFATAL: 请输入合法的apk_build_path: \033[0m"
      return 0
    else
      # 这里的-d参数判断目录是否存在
      if [ ! -d "${apk_build_path}" ]; then
        echo -e "\033[31mFATAL: $apk_build_path 目录不存在 \033[0m"
        return 0
      fi
    fi
    test_link_splash=$3
    if [[ -z "${path}" ]] ; then
      echo -e "\033[31mFATAL: 没有输入splash页,默认为Pushbullet的splash: \033[0m"
      splash=com.pushbullet.android/com.pushbullet.android.ui.LaunchActivity
    fi

    echo '1. cd ' $apk_build_path
    cd $apk_build_path
    final_apk_path=$(find $apk_build_path -name  "*.apk")
    echo '2. find apk: ' $final_apk_path

    # 卸载App
    echo "3. 卸载$applicationId"
    adb uninstall $applicationId

    # 去掉代理
    echo '4. 清除手机Charles等抓包代理(代理会影响applinks的校验)'
    proxy:off

    echo '5. 杀死Google Play Store进程(避免影响测试)'
    adb shell am force-stop com.android.vending

    echo "6. 点击带Google Play Install Referrer的链接到Google Play Store, 点击安装按钮到下载中,然后取消来安装测试包(线上包不好调试/抓包)"
    echo " |--启动 $test_link_splash"
    adb shell am start -n $test_link_splash
    echo " |--点击链接到Google Play Store下载后,然后取消,按任意按键继续"
    read temp

    echo "7. 安装测试包:$final_apk_path"
    # 安装要测试的App
    adb install -r -t "$final_apk_path"

    echo "8. 休息3秒,等待 $applicationId 的applinks的校验(需要Android12及以上的手机)"
    echo "  |-- 如果结果返回none或1024继续重新执行命令,直到返回verified:adb shell pm verify-app-links --re-verify $applicationId 命令校验applinks"
    echo "  |-- 等个几秒再用命令adb shell pm get-app-links $applicationId 查看校验结果"
    # 等待校验applinks
    /bin/sleep 3

    echo "9. 获取 $applicationId 的applinks校验结果(会连接Google校验,需要科学上网,关闭抓包等代理,返回verified表示校验成功)"
    # 查看applinks
    adb shell pm get-app-links $applicationId

    echo "10. 开启手机Charles抓包代理"
    # 开启代理
    proxy:on

    echo "11. 重新打开 $applicationId,开始测试Google Play Install Referrer吧"
    echo "  |-- 即将启动xxx"
    if [ "$applicationId" = "$MAIN_APP_ID" ]; then
      adb shell am start -n $MAIN_APP_SPLASH
    elif [ "$applicationId" = "$SECOND_APP_ID" ]; then
      adb shell am start -n $SECOND_APP_SPLASH
    fi
}


function getWidthAndHeight() {
    # adb shell wm size
    # Physical size: 1440x2560
    w=$(adb shell wm size | awk '{print $3}' | awk -F'x' '{print $1}')
    h=$(adb shell wm size | awk '{print $3}' | awk -F'x' '{print $2}')
    echo "${w}"x"${h}"
}
function checkffmpeg() {
    if brew ls --versions ffmpeg > /dev/null; then
        echo 'ffmpeg已安装'
    else
        echo 'ffmpeg未安装'
        echo '开始安装ffmpeg'
        brew install ffmpeg
    fi
}
# mp4转gif
function adb:record() {
    checkffmpeg
    if [ $# -lt 1 ]; then # 大于或等于1个参数
    echo -e "\033[31mFATAL 请输入至少1个参数,参数1:录制时间,单位秒 \033[0m"
    echo "当前参数 $*"
    return 0
  fi
    # read -p "请输入录制时间(单位秒):" t
    # if [ -z "$t" ]; then
    #     echo "录制时间不能为空"
    #     exit 1
    # fi
    echo -e '录制时间为:' $1 '秒'
    echo -e '开始录制'
    adb shell screenrecord  --time-limit $1 /sdcard/demo.mp4
    adb pull /sdcard/demo.mp4 > /dev/null

    # 获取时间戳
    currentTimeStamp=$(date +%s)
    echo $currentTimeStamp

    # 获取设备分辨率
    size=$(getWidthAndHeight)
    echo "设备分辨率:${size}"

    # 'ffmpeg转换'
    ffmpeg -i demo.mp4 -s "$size" -r 10 target-$currentTimeStamp.gif
    # '删除缓存的视频'
    rm -f demo.mp4
    # '输出打开我们最后的gif /r'
    echo "$(cd `dirname $0`; pwd)"/target-$currentTimeStamp.gif
    open .
}

##############################################
################### open #####################
##############################################

### 打开zshrc文件
function open:zshrc() {
  open ~/.zshrc
}
### ~/Library/LaunchAgents
function open:launchagent() {
  open ~/Library/LaunchAgents
}
### launchctl
function launch:list() { # list
  cd ~/Library/LaunchAgents
  launchctl list |grep $1
}
function launch:load() { # load
  cd ~/Library/LaunchAgents
  launchctl load -w $1
}
function launch:unload() { # unload
  cd ~/Library/LaunchAgents
  launchctl unload $1
}

### source zshrc文件
function source:zshrc() {
  source ~/.zshrc
}

### 打开home目录
function open:home() {
  open ~
}


##############################################
################# git-batch ##################
##############################################

## gits 批量处理git仓库
alias py='python3' # 默认使用Python3
alias gits="python3 $GITS"

#### 将~/py/git-batch/clone.txt目录下的repo全部clone下到当前位置
function gits:clone() {
  cd $WORKSPACE_MAIN
  pwd
  python3 $GITS clone -f ~/py/git-batch/clone.txt -p $WORKSPACE_MAIN
}

#### 主工程 gits:checkout master 所有分支切换到master
function gits:checkout() {
  cd $WORKSPACE_MAIN
  pwd
  echo $0 $1
  python3 $GITS checkout $1
}
#### 主工程 gits:pull master 所有分支先切换到master,然后pull,不传递参数pull当前分支
function gits:pull() {
  cd $WORKSPACE_MAIN
  pwd
  echo $0  $1
  python3 $GITS pull $1
}
#### 主工程 push指定分支,如果指定分支本地没有,就不会push python3 git-batch.py branch 9.5.4/user-zfs
function gits:push() {
  cd $WORKSPACE_MAIN
  pwd
  echo $0 $1
  python3 $GITS push $1
}
#### 查看主工程 所有仓库当前分支名
function gits:query() {
  cd $WORKSPACE_MAIN
  pwd
  echo $0 $1
  python3 $GITS query $1
}
function gits:status() {
  cd $WORKSPACE_MAIN
  pwd
  python3 $GITS status
}
function gits:delete() {
  cd $WORKSPACE_MAIN
  pwd
  python3 $GITS delete $1
}

function gits:clone:bug() {
  cd $WORKSPACE_BUG
  pwd
  python3 $GITS clone -f ~/py/git-batch/clone.txt -p $WORKSPACE_BUG
}
### bug工程 gits:checkout master 所有分支切换到master
function gits:checkout:bug() {
  cd $WORKSPACE_BUG
  pwd
  echo $0  $1
  python3 $GITS checkout $1
}
function gits:pull:bug() {
  cd $WORKSPACE_BUG
  pwd
  echo $0  $1
  python3 $GITS pull $1
}
function gits:push:bug() {
  cd $WORKSPACE_BUG
  pwd
  echo $0 $1
  python3 $GITS push $1
}
function gits:query:bug() {
  cd $WORKSPACE_BUG
  pwd
  echo $0 $1
  python3 $GITS query $1
}
function gits:delete:bug() {
  cd $WORKSPACE_BUG
  pwd
  python3 $GITS delete $1
}

##############################################
################## tomcat ####################
##############################################

## tomcat
TOMCAT_HOME=~/Downloads/apache-tomcat-9.0.75
function tomcat:start() {
  echo ${${(%):-%x}:A:h}
  $TOMCAT_HOME/bin/startup.sh
}
function tomcat:stop() {
  $TOMCAT_HOME/bin/shutdown.sh
}


##############################################
################# 工作相关 ####################
##############################################

## 进程
### 获取某个进程的pid pid:print '企业微信'
function pid:print() {
  ps -ef | grep $1
  ps -ef | grep $1 | awk 'NR==1{print $2}'
  # 或者  ps -e | grep ClashX.app | awk 'NR==1{print $1}'
}
### macOS根据名称杀死进程 pid:kill '企业微信'
function pid:kill() {
  ps -ef | grep $1 | awk 'NR==1{print $2}' | xargs kill -9
}
### 杀死常用的APP
function pid:kill:all() {
  pid:kill 企业微信
  pid:kill WeCom
  pid:kill WeChat
  pid:kill Google\ Chrome
  pid:kill AndroidStudioPreview
  pid:kill AndroidStudioBeta0622
  pid:kill PyCharm
  pid:kill Fork
  pid:kill Microsoft\ Edge
  pid:kill Microsoft\ Edge\ Dev
  pid:kill Visual\ Studio\ Code
  pid:kill Terminal
  pid:kill iTerm
  pid:kill Flipper
  pid:kill Charles
  pid:kill 语雀
  pid:kill 有道云笔记
  pid:kill TickTick
  pid:kill TeamViewer
  pid:kill ToDesk
  pid:kill Proxifier
  pid:kill ClashX
  pid:kill scrcpy
  pid:kill Postman
}

## cp相关
WORK_ID=xxx
WOEK_EMAIL=xxx@xxx.com
function cp:id() { # 拷贝工号ip
  echo $WORK_ID | pbcopy | echo $WORK_ID copied
}
function cp:email() { # 拷贝工作邮箱
  echo $WOEK_EMAIL | pbcopy | echo $WOEK_EMAIL copied
}
function cp:pwd() { # 拷贝当前路径
  pwd | pbcopy
}
function cp:ip() { # 拷贝当前IP
  # 获取 IP
  ip=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
  # echo $ip
  count=$(echo $ip | tr ' ' '\n' | wc -l )
  if [ $count -gt 1 ];then
    echo "多个ip, 请手动选择一个"
    exit
  fi
  echo "$ip" | pbcopy | echo ip copied
}
function cp:ws() { # 获取websocket测试链接
  # ws://10.102.156.58:8800/mock
  ip=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')
  echo "ws://$ip:8080/mock" | pbcopy | echo "ws://$ip:8080/mock" copied
}

## Terminal开启和关闭proxy
function ss:on() {
    local_hosts="127.0.0.1:7890"
    export http_proxy="http://${local_hosts}" https_proxy="http://${local_hosts}" ftp_proxy="http://${local_hosts}" all_proxy="socks5://${local_hosts}"
    echo "proxy on"
}
function ss:off() {
    unset http_proxy https_proxy ftp_proxy all_proxy
    echo "proxy off"
}

## Open

### 用Android Studio打开文件


function open:as() {
  # 以AS打开一个项目
  open -a /Applications/AndroidStudioBeta0622.app $1
}
function open:gradle() {
  # 打开gradle文件夹
  open $USER_HOME/.gradle
}
function open:initgradle() {
  # 打开nit.gradle文件
  open $GRADLE_USER_HOME/init.gradle
}
function open:charles() {
  # 打开存放charles模拟的数据的目录
  open $CHARLES_HOME
}

### 打开AS工作目录
function open:workspace() {
  open ~/Workspace
}
### 启动工作所需APP、脚本
function open:work:sh() {
  $SHELL_HOME/work.sh
}
function open:gohome() {
  $SHELL_HOME/go_home.sh
}

# 反编译

alias apktool="apktool d -s "
alias dex2jar="$HACK_HOME/dex-tools-2.1-20190905-lanchon/d2j-dex2jar.sh "
alias dex2jar2="$HACK_HOME/dex2jar-2.0/d2j-dex2jar.sh "
alias jadx="$HACK_HOME/jadx-1.4.7/bin/jadx "
alias jadx-gui="$HACK_HOME/jadx-1.4.7/bin/jadx-gui "
alias classyshark="java -jar $HACK_HOME/ClassyShark.jar -open "
# 查看keystore的指纹:MD5、SHA1、SHA256,新版的keytool不支持MD5了
function keytool:list() {
  keytool -list -v -keystore $1
}
function keytool:list:md5() {
  keytool -exportcert -keystore $1 | openssl dgst -md5
}

### click.sh
function click() {
  sh $SHELL_HOME/click.sh $1
}

## 查询ip
alias ip="curl -i http://ip.cn"

## 工作特定的脚本

### 过时的分支管理脚本
alias git_branch="sh $WORKSPACE_MAIN/git_branch.sh"
alias git_branch_name="sh $WORKSPACE_MAIN/git_branch_name.sh"
alias git_branch_bugfix="sh $WORKSPACE_BUG/git_branch.sh"
alias git_branch_name_bugfix="sh $WORKSPACE_BUG/git_branch_name.sh"

function whenceFunction() {
    whence -w $@
}

git-batch.py
click.sh

遇到的问题

.zshrc 和 shell 独立有些命令找不到

  • .zshrc.common_profile.sh 都要将使用到的命令的路径进行 export
  • 被调用的 function/alias 要写在使用的前面

oh-my-zsh 特殊字符无法显示

lykx2
原因
现有的字体不支持这些符合的显示
解决 1

  1. 安装 nerd fonts
  2. iTerm2 中设置字体为 nerd 相关的字体

iosst

  1. 解决后

tl64l
解决 2:

  • 安装 Menlo for Powerline 字体 mkdir ~/.fonts

mkdir ~/.fonts cd ~/.fonts wget https://github.com/abertsch/Menlo-for-Powerline/raw/master/Menlo%20for%20Powerline.ttf fc-cache -vf ~/.fonts

Ref

https://juejin.cn/post/6973069891691610126

本文由作者按照 CC BY 4.0 进行授权