MacOS上使用oh-my-zsh
本帖最后由 lichao 于 2024-7-17 16:54 编辑## 简介
  ZSH是一款常用的命令行工具,也是MacOS默认shell。ohmyzsh是基于zsh的开源项目<https://github.com/ohmyzsh/ohmyzsh>,提供了一系列命令行主题及插件。可以让命令行更炫酷。笔者第一次使用ohmyzsh是在18年,从同事那里看到的,最近又想起来就玩了玩。oh-my-zsh支持的操作系统包括 Android/FreeBSD/Linux/MacOS/WSL2
  在ZSH的众多主题中,ohmyzsh的agnoster主题和Powerlevel10k是最火的2款,Powerlevel10k也是个开源项目<https://github.com/romkatv/powerlevel10k>
## 安装
   agnoster主题安装过程
* 安装ZSH并设为默认shell. (MacOS自带)
* 参考项目说明安装ohmyzsh. `sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"`
* `~/.zshrc`中设置`ZSH_THEME="agnoster"`
* 安装字体<https://github.com/powerline/fonts>(agnoster需要Meslo系字体才能正常显示,否则会有乱码)
* 自带终端和iTerm2中设置字体为Meslo,然后配置配色
   Powerlevel10k主题安装
* 参考项目说明安装Meslo LGS NF字体
* 安装Powerlevel10k. `git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k`
* `~/.zshrc`中设置`ZSH_THEME="powerlevel10k/powerlevel10k"`
* 新建shell窗口按提示继续配置
  最终笔者配置效果如图
## ohmyzsh主题大全
  ohmyzsh包含100多个内置提示符样式,笔者写了个脚本来展示所有主题
```python
import os
import sys
import time
import pyautogui
from AppKit import NSWorkspace
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)
def find_window_by(title=None, owner=None):
curr_app = NSWorkspace.sharedWorkspace().frontmostApplication()
curr_pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
curr_app_name = curr_app.localizedName()
options = kCGWindowListOptionOnScreenOnly
windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
for window in windowList:
pid_ = window['kCGWindowOwnerPID']
owner_ = window['kCGWindowOwnerName'].UTF8String().decode()
bounds_ = window['kCGWindowBounds']
title_ = window.get('kCGWindowName', b"")
wind_info = {
"pid": pid_,
"owner": owner_,
"bounds": {
"h": int(bounds_["Height"]),
"w": int(bounds_["Width"]),
"x": int(bounds_["X"]),
"y": int(bounds_["Y"]),
},
"title": title_,
}
if owner and owner == owner_:
return wind_info
if title and title == title_:
return wind_info
return None
if __name__ == "__main__":
theme_path = os.path.expanduser("~/.oh-my-zsh/themes")
#terminal_path = "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal"
terminal_path = "/Applications/iTerm.app/Contents/MacOS/iTerm2"
index = 1
for name in os.listdir(theme_path):
theme = name.split(".")
img_path = "/tmp/theme/{}.png".format(index)
if os.path.exists(img_path):
print("pass {}".format(theme))
else:
os.system("export ZSH_THEME={};{} {}&".format(theme, terminal_path, theme_path))
wind_info = None
time.sleep(5)
for i in range(6):
wind_info = find_window_by("", "iTerm2") # 终端
if wind_info:
break
time.sleep(1)
if not wind_info:
print("err handle {}".format(theme))
continue
bounds = wind_info["bounds"]
top_bar_height = 45
content_width = 570
content_height = 70
shot_range = , bounds["y"] + top_bar_height, content_width, content_height]
im = pyautogui.screenshot(region=shot_range)
im.save(img_path)
os.kill(wind_info["pid"], 9)
wind_info = find_window_by("-zsh")
if wind_info:
os.kill(wind_info["pid"], 9)
index += 1
# convert -append *.png all.png
```
页:
[1]