Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gh.fish #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 57 additions & 17 deletions config/fish/functions/gh.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ end
# Prints the origin and remote
# Example: oguzbilgic/dotfiles
function org_repo
echo (origin | python -c "import sys; url = sys.stdin.readlines()[0].strip(); print url.split(':')[1][:-4]")
set --local origin_with_dot_git (string split ":" (git_origin))
set --local org_repo (string split "." $origin_with_dot_git[2])
echo $org_repo[1]
end

# Prints the current branch name
Expand All @@ -36,21 +38,59 @@ end
# gh commit opens the last commit of the current branch
# gh commits opens the commits page for the current branch
function gh
if test (count $argv) -lt 1
open "https://github.com/"(org_repo)"/tree/"(branch)
else if test -f $argv[1]
open "https://github.com/"(org_repo)"/blob/"(commit)"/"(echo $argv[1])
else if test $argv[1] = "issues"
open "https://github.com/"(org_repo)"/issues"
else if test $argv[1] = "pulls"
open "https://github.com/"(org_repo)"/issues"
else if test $argv[1] = "actions"
open "https://github.com/"(org_repo)"/issues"
else if test $argv[1] = "compare"
open "https://github.com/"(org_repo)"/compare/"(branch)
else if test $argv[1] = "commits"
open "https://github.com/"(org_repo)"/commits/"(branch)
else if test $argv[1] = "commit"
open "https://github.com/"(org_repo)"/commit/"(commit)
#Options for the argparse
set --local options 'h/help' 'f/file=' 'i/issue=!_validate_int --min 1' 'b/issues' 'u/pulls' 'p/pull=!_validate_int --min 1' 'a/actions' 'o/compare' 'c/commit' 's/commits' 'd'

# Read the arguments of the function and validates according to $options
argparse $options -- $argv

if set --query _flag_h
echo "Usage: gh [-h/help ] [-f/file [file] ] [-i/issue [issue number] ] [-b/issues] [-u/pulls] [-p/pull [pull number] ] [-a/actions] [-o/compare] [-c/commit] [-s/commits] [-d]"
echo "Options:"
echo_tabulated ". -f/file [file_name] Open the first file matching [file_name]"
echo_tabulated ". -i/issue [issue number] Open the [issue_number]."
echo_tabulated ". -b/issues Open issues list"
echo_tabulated ". -u/pulls Open the pull requests list"
echo_tabulated ". -p/pull [pull number] Open the pull request [pull_number]"
echo_tabulated ". -a/actions Open the actions"
echo_tabulated ". -o/compare Open the compare view"
echo_tabulated ". -c/commit Open the last commit"
echo_tabulated ". -s/commits Open the commits list"
echo_tabulated ". -d Open the current branch"
return 0
end

if set --query _flag_f
set file_path (find . -name "*$_flag_f*")
set file_path $file_path[1]
if not set --query file_path
echo_error "File $_flag_f not found."
return 1
end
set url "https://github.com/"(git_org_repo)"/blob/"(git_branch)"/$file_path"
else if set --query _flag_i
set url "https://github.com/"(git_org_repo)"/issue/$_flag_i"
else if set --query _flag_a
set url "https://github.com/"(git_org_repo)"/issues"
else if set --query _flag_u
set url "https://github.com/"(git_org_repo)"/pulls"
else if set --query _flag_p
set url "https://github.com/"(git_org_repo)"/pull/$_flag_p"
else if set --query _flag_a
set url "https://github.com/"(git_org_repo)"/actions"
else if set --query _flag_o
set url "https://github.com/"(git_org_repo)"/compare/"(git_branch)
else if set --query _flag_c
set url "https://github.com/"(git_org_repo)"/commit/"(git_commit)
else if set --query _flag_s
set url "https://github.com/"(git_org_repo)"/commits/"(git_branch)
else if set --query _flag_m
set url "https://github.com/"(git_org_repo)"/tree/"(git_branch)
end
if set --query url
open $url
else
echo_error "No valid argument provided."
end
return 0
end