This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
/
hypr-cwd
executable file
·58 lines (57 loc) · 2.3 KB
/
hypr-cwd
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
#!/bin/sh
# 通过当前激活 window 的 title 获取当前的工作目录,
# 目前支持 emacs (需要配置 frame-title-format,如下文注释)
# 后续可以添加 alacritty/foot 等的支持(需要在 zsh/bash 中配置一定的规则以更新 window title)
# 可以实现的功能:
# 比如说 在 emacs 中 当前在/etc 目录下,我想打开 alacritty 终端并进入 /etc 目录,则可以使用以下命令
# alacritty --working-directory=$(hypr-cwd||echo $HOME)
# get current work directory of front most app
# supported app:
# 1. emacs
# frame-title-format '( "%f GNU/Emacs<" (:eval (expand-file-name default-directory)) ">")
# 详见:https://github.com/jixiuf/vmacs/blob/master/conf/conf-common.el#L56
# 2. alacritty with zsh
# autoload -U add-zsh-hook
# update_cwd(){
# print -Pn "\e]2;$(pwd)\a" #s
# }
# add-zsh-hook precmd update_cwd
# bashrc
# tramp(){
# printf "\e]2;/ssh:$(whoami)@$(ip addr | grep 'inet\b' |grep -v 127.0.0.1 | awk '{print $2}' | cut -d/ -f1):$(pwd)\a"
# }
# PS1=$PS1'\[$(tramp)\]'
appinfo=`hyprctl activewindow -j`
if [ -n "$appinfo" ]; then
# if front app exits
class=`echo $appinfo|jq -r '.class'|head -n 1`
if [[ "$class" = "emacs" ]]; then
cwd=$(echo $appinfo|jq -r '.title| split("GNU/Emacs<")[1]|split(">")[0]')
eval "cwd=$cwd" # replace ~ -> $HOME
echo $cwd
exit 0
elif [[ "$class" = "org.wezfurlong.wezterm" ||"$class" = "foot" ||"$class" = "kitty" || "$class" = "Alacritty" || "$class" = "dterm" ]]; then
# for zsh
# autoload -U add-zsh-hook
# update_cwd(){
# print -Pn "\e]2;$(pwd)\a" #s
# }
# add-zsh-hook precmd update_cwd
# 如果使用 oh my zsh ,请在source $ZSH/oh-my-zsh.sh 前加 DISABLE_AUTO_TITLE=true
# DISABLE_AUTO_TITLE=true
# source $ZSH/oh-my-zsh.sh
cwd=$(echo $appinfo|jq -r '.title')
if [[ $cwd == TMUX:* ]]; then
# Remove the prefix "TMUX:" and then split the remaining string by ":"
# 格式: "TMUX:session:path"
cwd=${cwd#TMUX:}
tmux_session=${cwd%%:*}
cwd=${cwd#*:}
fi
cwd="${cwd/#\~/$HOME}"
# eval "cwd=$cwd" # replace ~ -> $HOME
echo $cwd
exit 0
fi
fi
exit 1