├── LICENSE ├── README.md └── gh-ghq-cd /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Shu Kutsuzawa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gh-ghq-cd 2 | 3 | ## Requires 4 | 5 | - [`gh`](https://github.com/cli/cli) v2.0.0+ 6 | - [`ghq`](https://github.com/x-motemen/ghq) 7 | - [`fzf`](https://github.com/junegunn/fzf) 8 | - (Optional) [`bat`](https://github.com/sharkdp/bat) 9 | 10 | ## How to install 11 | 12 | ```bash 13 | gh extension install https://github.com/cappyzawa/gh-ghq-cd 14 | 15 | # Upgrade 16 | gh extension upgrade gh-ghq-cd 17 | ``` 18 | 19 | ### (Optional) Recommended Setting 20 | 21 | ```bash 22 | gh alias set cd ghq-cd 23 | ``` 24 | 25 | ## How to use 26 | 27 | ```bash 28 | gh ghq-cd 29 | 30 | # If you set "cd" as an alias for ghq-cd 31 | gh cd 32 | 33 | # If you want to open the selected repository with tmux new window 34 | gh cd -nw 35 | ``` 36 | -------------------------------------------------------------------------------- /gh-ghq-cd: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | use_tmux=false 5 | new_window=false 6 | 7 | [ -n "${TMUX}" ] && use_tmux=true 8 | while [[ $# -gt 0 ]]; do 9 | case "$1" in 10 | -nw) 11 | $use_tmux && new_window=true 12 | shift 13 | ;; 14 | *) 15 | echo "Unknown argument: $1" 16 | exit 1 17 | ;; 18 | esac 19 | done 20 | 21 | function exist_command() { 22 | local c=$1 23 | if ! type -p ${c} >/dev/null; then 24 | return 1 25 | fi 26 | return 0 27 | } 28 | 29 | function check() { 30 | local required_command=(fzf ghq) 31 | for c in ${required_command[@]}; do 32 | if ! exist_command "${c}"; then 33 | echo "${c} not found on the system" >&2 34 | exit 1 35 | fi 36 | done 37 | } 38 | 39 | function choose() { 40 | local cc="cat" 41 | if exist_command "bat"; then 42 | cc="bat" 43 | fi 44 | ghq list --full-path | fzf --reverse --preview "${cc} {1}/README.md" 45 | } 46 | 47 | function tmux_new_window() { 48 | local dir=$1 49 | repo=$(basename "${dir}") 50 | tmux new-window -n "${repo}" -c "${dir}" 51 | } 52 | 53 | function tmux_op() { 54 | local dir=$1 55 | repo=$(basename "${dir}") 56 | $new_window && tmux new-window -n "${repo}" -c "${dir}" && return 57 | tmux rename-window "${repo}" 58 | } 59 | 60 | check 61 | selected="$(choose)" 62 | [ -n "${selected}" ] || exit 1 63 | repo=$(basename "${selected}") 64 | if $new_window; then 65 | tmux new-window -n "${repo}" -c "${selected}" 66 | exit 0 67 | fi 68 | \cd ${selected} 69 | $use_tmux && tmux rename-window $repo 70 | $SHELL 71 | --------------------------------------------------------------------------------