Skip to content

Commit 119610b

Browse files
dwmkerrCopilot
andauthored
feat(snippets): ghopen snippet (#370)
* feat(snippets): ghopen snippet * Update docs/07-shell-snippets/00-index.md Co-authored-by: Copilot <[email protected]> * chore: fix title --------- Co-authored-by: Copilot <[email protected]>
1 parent 387efe0 commit 119610b

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

assets/amazon-thumbnail.psd

1.18 MB
Binary file not shown.

docs/07-shell-snippets/00-index.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,43 @@ aigac() {
3636
```
3737

3838
This snippet may change over time - you should be able to find the latest version in my [`dotfiles`](https://github.com/dwmkerr/dotfiles).
39+
40+
### Open GitHub for Current Directory
41+
42+
When I'm working on some code I often want to open its GitHub home page, to check for issues, see the status of pipelines and so on. You can use the [`gh`](https://github.com/cli/cli) CLI for many of these operations, but if I just want to open the webpage, I like use the `ghopen` function:
43+
44+
![Demo](./casts/ghopen.svg)
45+
46+
This command will open the URL shown in your browser.
47+
48+
Here's the code:
49+
50+
```bash title="https://github.com/dwmkerr/dotfiles/blob/main/shell.functions.d/ghopen.sh"
51+
ghopen() {
52+
# Get the origin for the current repo.
53+
local origin=$(git remote get-url origin 2> /dev/null)
54+
55+
# Bail if we're not in a github repo.
56+
if [[ ($? -ne 0) || ("${origin}" != *github*) ]]; then
57+
echo "current dir '$(basename "${PWD}")' is not in a github repo..."
58+
return
59+
fi
60+
61+
# The origin probably looks like this:
62+
# [email protected]:dwmkerr/effective-shell.git
63+
# The org/repo is everything after the colon and before '.git'.
64+
local org_repo=$(echo "${origin%.git}" | cut -f2 -d:)
65+
local url="http://github.com/${org_repo}"
66+
67+
# Let the user know what we're opening, formatting org/repo in green, open.
68+
echo -e "opening github.com/\e[32m${org_repo}\e[0m"
69+
python3 -c "import webbrowser; webbrowser.open_new_tab('${url}')"
70+
}
71+
```
72+
73+
Some useful techniques - all of which are covered in various chapters across the book!
74+
75+
- Stream Redirection: We pipe errors from `git` to `/dev/null` so that we don't spam the user's screen, and check the result of the command with `$?`
76+
- Conditionals: The Bash 'if statement' let's us check whether the origin contains the text 'github'
77+
- Shell Expansion: We can use the `${origin%.git}` brace expansion to remove `.git` from the end of a variable
78+
- Opening a browser with `python3` is more portable than using `open` or similar
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{"version": 2, "width": 80, "height": 15, "timestamp": 1742313085, "env": {"SHELL": "/usr/local/bin/bash", "TERM": "xterm-256color"}, "title": "Terminal AI"}
2+
[0.501316, "o", "\u001b[?2004h\r\n\u001b[1m\u001b[34mgithub/dwmkerr/terminal-ai\u001b(B\u001b[m \u001b[32m\u001b[4mmain\u001b(B\u001b[m \u001b[31m!\u001b(B\u001b[m \u001b[33m1 in stash\u001b(B\u001b[m\r\n\u001b[1m\u001b[37m$\u001b(B\u001b[m "]
3+
[1.560621, "o", "g"]
4+
[1.684253, "o", "h"]
5+
[1.929254, "o", "o"]
6+
[2.121472, "o", "p"]
7+
[2.277122, "o", "e"]
8+
[2.427868, "o", "n"]
9+
[2.740099, "o", "\r\n"]
10+
[2.740164, "o", "\u001b[?2004l\r"]
11+
[2.763675, "o", "opening github.com/\u001b[32mdwmkerr/terminal-ai\u001b[0m\r\n"]
12+
[3.069906, "o", "\u001b[?2004h\r\n\u001b[1m\u001b[34mgithub/dwmkerr/terminal-ai\u001b(B\u001b[m \u001b[32m\u001b[4mmain\u001b(B\u001b[m \u001b[31m!\u001b(B\u001b[m \u001b[33m1 in stash\u001b(B\u001b[m\r\n\u001b[1m\u001b[37m$\u001b(B\u001b[m "]
13+
[4.522979, "o", "\u001b[?2004l\r\r\n"]
14+
[4.523026, "o", "exit\r\n"]
18.9 KB
Loading
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)