-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpuswitch.sh
executable file
·234 lines (210 loc) · 8.05 KB
/
gpuswitch.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
##########################################################################
# File Name: gpuswitch.sh
# Author: EmotionalAmo
# mail: emotionalamo@starpin.cn
# Created Time: Sat 7/11 15:46:07 2020
#########################################################################
#!/bin/bash
#### Function
detect_user_level(){
if [ $(id -u) != 0 ];then
clear
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo "┃ 当前用户等级:\033[32mUser\033[0m\c"
echo " 请使用\033[31mRoot\033[0m用户运行 ┃"
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
sudo $0
exit
else
clear
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo "┃ MacBook Pro 15.4′/16′ 显卡切换器 ┃"
echo "┃ 当前用户等级:\033[31mRoot\033[0m 版本:\033[35mV ${version}\033[0m ┃"
echo "┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫"
fi
}
detect_gpu_mode(){
if [ $custom_gpu_info = '22' ] || [ $custom_gpu_info = '11' ] || [ $custom_gpu_info = '00' ];then
gpu_switch_status_all $custom_gpu_info
else
gpu_switch_status_bt ${custom_gpu_info:0:1}
gpu_switch_status_ac ${custom_gpu_info:1:1}
fi
}
gpu_switch_status_all(){
if [ $1 = '22' ];then
echo "┃ 显卡模式:\033[32m全局-自动切换\033[0m ┃"
elif [ $1 = '00' ];then
echo "┃ 显卡模式:\033[32m全局-核芯显卡\033[0m ┃"
elif [ $1 = '11' ];then
echo "┃ 显卡模式:\033[32m全局-独立显卡\033[0m ┃"
fi
}
gpu_switch_status_bt(){
if [ $1 = '0' ];then
echo "┃ 显卡模式:\033[32m电池-核芯显卡\033[0m\c"
elif [ $1 = '1' ];then
echo "┃ 显卡模式:\033[32m电池-独立显卡\033[0m\c"
elif [ $1 = '2' ];then
echo "┃ 显卡模式:\033[32m电池-自动切换\033[0m\c"
fi
}
gpu_switch_status_ac(){
if [ $1 = '0' ];then
echo " \033[32m电源-核芯显卡\033[0m ┃"
elif [ $1 = '1' ];then
echo " \033[32m电源-独立显卡\033[0m ┃"
elif [ $1 = '2' ];then
echo " \033[32m电源-自动切换\033[0m ┃"
fi
}
battery_level_detect(){
counter=`expr $bt_cyc / 10`
if [ $counter -le 30 ]; then
battery_level='健康'
elif [ $counter -le 60 ]; then
battery_level='一般'
elif [ $counter -le 90 ]; then
battery_level='差'
else
battery_level='未知'
fi
}
battery_cycle_detect(){
bt_cyc_len=${#bt_cyc}
if [ $bt_cyc_len == '3' ]; then
bt_cyc_ct="${bt_cyc} "
else
bt_cyc_ct="${bt_cyc}"
fi
}
power_status(){
if [ ! -n "$adapter_info" ]; then
echo "┃ \033[31m无法获取电源状态/或状态异常\033[0m ┃"
echo "┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫"
elif [ "$adapter_info" == 'No adapter attached.' ]; then
echo "┃ 电源状态:\033[31m未接通\033[0m\c"
if [ "$battery_level" == '健康' ]; then
echo " 电源健康:\033[36m${battery_level}\033[0m ┃"
else
echo " 电源健康:\033[36m${battery_level}\033[0m ┃"
fi
echo "┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫"
else
wattage=$(pmset -g adapter | grep Wattage | tr -d ' WwAaTtGgEe=')
current=$(pmset -g adapter | grep Current | tr -d ' CcMmAaUuRrEeNnTt=')
voltage=$(pmset -g adapter | grep Voltage | tr -d ' VvOoLlTtAaGgEeMm=')
echo "┃ 电源状态:\033[36m已接通\033[0m\c"
echo " 当前功率:\033[36m${wattage} W\033[0m ┃"
echo "┃ 当前电压:\033[36m$(echo "scale=1; ${voltage} / 1000" | bc) V\033[0m\c"
echo " 当前电流:\033[36m$(echo "scale=1; ${current} / 1000" | bc) A\033[0m ┃"
echo "┃ 电池健康:\033[36m${battery_level}\033[0m\c" # ┃"
echo " 循环计数:\033[36m${bt_cyc_ct}\033[0m ┃"
echo "┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫"
fi
}
gpu_switch_mode(){
echo "┃ \033[33m选择设置类型\033[0m ┃"
echo "┃ \033[33m1. \033[0m全局模式 \033[33m2. \033[0m电池模式 ┃\n┃ \033[33m3. \033[0m电源模式 \033[33m4. \033[0m刷新 ┃\n┃ \033[33m5. \033[0m退出 ┃"
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"
read -p "请选择(1/2/3/4/5):" i
case "$i" in
1)
clear
mode='-a'
mode_name='全局模式'
gpu_switch_mode_set
;;
2)
clear
mode='-b'
mode_name='电池模式'
gpu_switch_mode_set
;;
3)
clear
mode='-c'
mode_name='电源模式'
gpu_switch_mode_set
;;
4)
clear
;;
5)
break
;;
q)
break
esac
}
gpu_switch_mode_set(){
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo "┃ 设置类型:\033[32m$mode_name\033[0m ┃"
echo "┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫"
echo "┃ 1. 核芯显卡 2. 独立显卡 ┃\n┃ 3. 自动切换 4. 返回上一层 ┃\n┃ 5. 退出 ┃"
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"
read -p "请选择(1/2/3/4/5):" i
gpu_switch_mode_set_sel_name
case "$i" in
1)
clear
pmset $mode GPUSwitch 0
;;
2)
clear
pmset $mode GPUSwitch 1
;;
3)
clear
pmset $mode GPUSwitch 2
;;
4)
clear
continue
;;
5)
break
esac
}
gpu_switch_mode_set_sel_name(){
if [ $i = '1' ];then
sel_name='核芯显卡'
elif [ $i = '2' ];then
sel_name='独立显卡'
elif [ $i = '3' ];then
sel_name='自动切换'
fi
}
exit_echo(){
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo "┃ \033[31m已退出\033[0m 显卡切换器.sh ┃"
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n"
}
detect_user_level
# Create Battery_Info Script
echo 'pmset -g rawlog > .tmp' > .tmp.sh
chmod +x .tmp.sh
while True
do
#### Pretreatment
custom_gpu_info=$(pmset -g custom | grep gpuswitch | tr -d ' \nGgPpUuSsWwIiTtCcHh')
adapter_info=$(pmset -g adapter)
bt_level_info=$(pmset -g sysload | grep 'battery' | tr -d ' -' | sed 's/batterylevel=//g')
bt_cyc=$(pmset -g rawbatt | grep -Eo 'Cycles=([0-9]+)' | sed 's/Cycles=//g')
version='1.5'
#### Get Status
clear
detect_user_level
#### Determine the graphics card switching mode
detect_gpu_mode
#### Determine battery level
battery_level_detect
#### Determine battery cycle
battery_cycle_detect
#### Determine the power status
power_status
#### Choose graphics card mode and switch
gpu_switch_mode
done
clear
exit_echo