
夏天到了,该关注关注 MAC 的 CPU 温度了。
周末的时候,看到有人推荐了一个 MAC 命令行工具 iStats,可以直接在命令行查看系统的 CPU 温度、电池运行等情况。突然结合自己一直使用 powerlevel9k
,比较之下,发现 powerlevel9k
也有电池使用插件(Segment),但貌似没有查看 CPU 温度的插件。
所以萌生出了,自己动手试着写一个 Segment,随时查看和关注 CPU 温度,避免温度过高,同时也可以做到不用下载其他应用,不用一直开着应用,只要你在用命令行,就可以看到 CPU 温度。
在 Gitlab 上,我找到一个开源的代码 lavoiesl/osx-cpu-temp
。
Outputs current CPU temperature for OSX
https://github.com/lavoiesl/osx-cpu-temp
只要下载回来,编译即可运行:
编译后,就可以把 osx-cpu-temp
加入环境变量中:
export PATH=/Users/yourname/Documents/tools/osx-cpu-temp:$PATH
我们开始尝试调用,让它显示在右侧
zsh_temperature(){
local signal=$(osx-cpu-temp)
echo $signal
}
POWERLEVEL9K_CUSTOM_TEMPERATURE="zsh_temperature"
# command line 左邊想顯示的內容
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) # <= left prompt 設了 "dir"
# command line 右邊想顯示的內容
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(ram time custom_temperature)
可以 source ~/.zshrc
看看效果了。
不错,可以把 osx-cpu-temp 执行的结果输出了。
但这么看,和其他 elements 样式不搭,我们需要再改造改造,至少看起来没有违和感,而且和插件 iStats 效果也差不太多。
第一步,让 osx-cpu-temp 只输出温度数字
首先,修改 osx-cpu-temp 源文件代码,让默认只显示数字,便于后续拿到数字去设计:
void readAndPrintCpuTemp(int show_title, char scale)
{
double temperature = SMCGetTemperature(SMC_KEY_CPU_TEMP);
if (scale == 'F') {
temperature = convertToFahrenheit(temperature);
}
char* title = "";
if (show_title) {
title = "CPU: ";
}
// 默认设置 scale 为空字符,只输出温度值
if (scale == '\0') {
printf("%0.1f", temperature);
} else {
printf("%s%0.1f °%c\n", title, temperature, scale);
}
}
第二步,使用 nerd-font 字体
The Nerd-Fonts project is an effort to create fonts truly tricked out with as many glyphs as possible. After installing nerd-fonts and configuring your terminal emulator to use one, configure Powerlevel9k by putting the following in your ~/.zshrc:
根据官网提示,安装 nerd-font 字体,需要在 .zshrc
增加一行:
POWERLEVEL9K_MODE='nerdfont-complete'
我用的是 sourcecodepro-nerd-font-mono 字体,可以通过命令下载:
brew tap homebrew/cask-fonts
brew cask install font-sourcecodepro-nerd-font
具体可参考:https://github.com/ryanoasis/nerd-fonts
🔡 Iconic font aggregator, collection, and patcher. 40+ patched fonts, over 3,600 glyph/icons, includes popular collections such as Font Awesome & fonts such as Hack https://NerdFonts.com
其中和 nerd-font
相关的字体还有很多,根据自己的需求下载:
有了字体后,就可以在 iTerm 上配置了,如下图所示:
其中,「Font」和「Non-ASCII Font」都用相同字号的相同字体。
第三步,编写自己的 Segments
参考官网的 Battery Status 案例,我们可以在 .zshrc 里先写函数:
zsh_temperature(){
local signal=$(osx-cpu-temp)
local signalint=${signal%.*}
local color='%F{darkgreen1}'
local temp=$'\uf2cb'
if [ $signalint -ge 80 ]; then
color='%F{red1}'
temp=$'\uf2c7'
elif [ $signalint -lt 80 -a $signalint -ge 60 ]; then
color='%F{orange1}'
temp=$'\uf2c8'
elif [ $signalint -lt 60 -a $signalint -ge 40 ]; then
color='%F{yellow1}'
temp=$'\uf2c9'
elif [ $signalint -lt 40 -a $signalint -ge 20 ]; then
color='%F{green1}'
temp=$'\uf2ca'
else
color='%F{darkgreen1}'
temp=$'\uf2cb'
fi
echo "%{$color%}$signal$temp%{%f%}"
}
函数很简单,就是获取电脑 CPU 温度,根据温度值,选择不同颜色和 Awesome Icons。
最后,就是注册这个Segment,在 iTerm 显示:
POWERLEVEL9K_CUSTOM_TEMPERATURE="zsh_temperature"
# command line 左邊想顯示的內容
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir) # <= left prompt 設了 "dir"
# command line 右邊想顯示的內容
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(custom_temperature ram)
好了,可以 source ~/.zshrc
看效果了:
只要 CPU 温度超过 80℃,直接标红,一眼就可以看出,及时优化降温~
总结
简单的一个案例,只有你有 idea,动动手,也能做出点不一样的东西出来,既不用安装太多的应用,又能嵌入现有的 iTerm 中,美化我们的命令行,在代码之余,自娱自乐一番。
参考
User Segments https://github.com/bhilburn/powerlevel9k/wiki/User-Segments
Font Awesome Icons https://fontawesome.com/icons?d=gallery&m=free
Outputs current CPU temperature for OSX https://github.com/lavoiesl/osx-cpu-temp
coding01 期待您继续关注
本文分享自微信公众号 - coding01(coding01)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。