Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkShawn2020 committed Dec 21, 2024
0 parents commit c348da6
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 YOUR_NAME

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# zsh-git-relative-path

A ZSH plugin that shows your current path relative to the git project root in your prompt.

## Features

- Works with any oh-my-zsh theme
- Shows path relative to git project root when in a git repository
- Falls back to normal path display when not in a git repository
- Special support for popular themes (robbyrussell, agnoster, avit)
- Zero configuration needed!

## Installation

### One-line Installation

```bash
curl -fsSL https://raw.githubusercontent.com/cs-magic-open/zsh-git-relative-path/main/install.sh | bash && source ~/.zshrc
```

That's it! No additional steps needed.

### Alternative: Local Installation

If you prefer to inspect the code first:

```bash
# 1. Clone the repository
git clone https://github.com/cs-magic-open/zsh-git-relative-path
cd zsh-git-relative-path

# 2. Run the installer
bash install.sh && source ~/.zshrc
```

## Usage

Once installed, the plugin will automatically modify your prompt to show paths relative to the git project root. No additional configuration is needed!

Example:
```
# Before (in /Users/username/projects/my-project/src/components)
username ~/projects/my-project/src/components $
# After (same directory)
username src/components $
```

## Uninstallation

```bash
rm -rf ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-git-relative-path
```
Then manually remove the plugin from the plugins list in your .zshrc.

## License

MIT

## Author

cs-magic-open
49 changes: 49 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}Installing zsh-git-relative-path...${NC}"

# 检查是否安装了 oh-my-zsh
if [ ! -d "$HOME/.oh-my-zsh" ]; then
echo "Oh My Zsh is not installed. Please install it first."
exit 1
fi

# 设置插件目录
PLUGIN_NAME="zsh-git-relative-path"
PLUGIN_DIR="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/$PLUGIN_NAME"

# 如果插件目录已存在,先删除
if [ -d "$PLUGIN_DIR" ]; then
echo "Removing existing plugin..."
rm -rf "$PLUGIN_DIR"
fi

# 创建插件目录
mkdir -p "$PLUGIN_DIR"

# 复制插件文件
cp "$(dirname "$0")/zsh-git-relative-path.plugin.zsh" "$PLUGIN_DIR/"
cp "$(dirname "$0")/README.md" "$PLUGIN_DIR/"
cp "$(dirname "$0")/LICENSE" "$PLUGIN_DIR/"

# 检查 .zshrc 中是否已经有插件配置
if grep -q "plugins=.*$PLUGIN_NAME" "$HOME/.zshrc"; then
echo "Plugin already in .zshrc"
else
# 找到 plugins 行并添加我们的插件
if grep -q "^plugins=(" "$HOME/.zshrc"; then
# 在现有的 plugins 行中添加插件
sed -i '' 's/^plugins=(/plugins=($PLUGIN_NAME /' "$HOME/.zshrc"
else
# 如果没有 plugins 行,添加一个新的
echo "plugins=($PLUGIN_NAME)" >> "$HOME/.zshrc"
fi
fi

echo -e "${GREEN}Installation complete!${NC}"
echo -e "Please run: ${BLUE}source ~/.zshrc${NC} to activate the plugin"
46 changes: 46 additions & 0 deletions zsh-git-relative-path.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# zsh-vscode-path.plugin.zsh

# 定义获取 vscode 相对路径的函数
function get_vscode_relative_path() {
local workspace_root=$(git rev-parse --show-toplevel 2>/dev/null)
local current_path=$(pwd)
local path_format=${1:-%~} # 默认使用 %~,但允许传入其他格式

if [[ -n "$workspace_root" && $current_path == $workspace_root* ]]; then
echo ${current_path#$workspace_root/}
else
echo $path_format
fi
}

# 保存原始的 prompt_subst 设置
if [[ -z $original_prompt_subst ]]; then
original_prompt_subst=$prompt_subst
fi

# 启用命令替换
setopt prompt_subst

# 定义一个函数来包装任意主题的提示符
function wrap_prompt_with_vscode_path() {
# 处理主题特定的变量
case $ZSH_THEME in
avit)
_current_dir="%{$fg_bold[blue]%}$(get_vscode_relative_path %3~)%{$reset_color%} "
;;
agnoster)
# 为 agnoster 主题添加特殊处理
prompt_dir() {
prompt_segment blue $CURRENT_FG "$(get_vscode_relative_path)"
}
;;
*)
# 默认替换
PROMPT=${PROMPT//\%\~/'$(get_vscode_relative_path)'}
PROMPT=${PROMPT//\%c/'$(get_vscode_relative_path %c)'}
;;
esac
}

# 添加到主题加载后的钩子
add-zsh-hook precmd wrap_prompt_with_vscode_path

0 comments on commit c348da6

Please sign in to comment.