├── LICENSE
├── README.md
├── assets
├── gosearch.gif
└── wallpaper.png
├── gosearch
└── install.sh
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Sina Shabani
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 | # Gosearch
2 |
3 | > Gosearch is a zsh widget that lists packages when you are trying to work with `go get`. See [Description](#description).
4 |
5 | 
6 |
7 |
10 |
11 |
12 | ## Description
13 |
14 | Imagine you started a new go project or working on an existing project and want to add some packages you already know the name of but don't know the actual address. Packages like gin, fiber, testify, json, uuid and ... . This script does exactly that. It searches for the packages by their name without exiting the terminal and opening the browser.
15 |
16 | 
17 |
18 |
19 | ## Installation
20 |
21 | ```bash
22 | curl -sSfL https://raw.githubusercontent.com/itzloop/gosearch/main/install.sh | bash
23 | ```
24 |
25 | ## Usage
26 | Searching for go packages in the terminal
27 | ```bash
28 | $ go get
29 | $ go get require
30 | $ go get misrua
31 | ```
32 |
33 | Currently, symbols aren't supported, so these **WILL NOT** work
34 | ```bash
35 | # These WILL NOT work
36 | $ go get sql.Conn
37 | $ go get pgx.Conn
38 | ```
39 | Depending on where you live, you might need to setup a proxy
40 |
41 | ## Debugging
42 |
43 | There is the env, `GOSEARCH_DEBUG_FILE`, by setting this in the shell you are working debug logs will be written to that file.
44 |
45 | ```
46 | $ export GOSEARCH_DEBUG_FILE=/path/to/some/file
47 | $ go get http
48 | # logs will be written to '/path/to/some/file'
49 | ```
50 |
51 | ## Limitation
52 |
53 | 1. Currently symbols aren't supported, so these **WILL NOT** work
54 | 2. Depending on where you live, you might need to setup a proxy
55 | 3. It's only for zsh, so if you use another shell and like this project, submit an issue or even better a PR!
56 |
57 | ## Contributions
58 |
59 | This project is only for my needs, so it's pretty limited. If you have another usecase that is not covered, feel free to submit an issue or a PR.
60 |
--------------------------------------------------------------------------------
/assets/gosearch.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itzloop/gosearch/2173f2dcb95e22eeb375926ff751c51609016c7f/assets/gosearch.gif
--------------------------------------------------------------------------------
/assets/wallpaper.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/itzloop/gosearch/2173f2dcb95e22eeb375926ff751c51609016c7f/assets/wallpaper.png
--------------------------------------------------------------------------------
/gosearch:
--------------------------------------------------------------------------------
1 | # Inspired by https://www.jonathanh.co.uk/blog/current-word-completion/
2 |
3 | ErrNoResults="no results"
4 |
5 | gosearch_debug()
6 | {
7 | local file="$GOSEARCH_DEBUG_FILE"
8 | if [[ -n ${file} ]]; then
9 | echo "$*" >> "${file}"
10 | fi
11 | }
12 |
13 | _gosearch () {
14 | local query=$1
15 |
16 | html=$(curl -s --location "https://pkg.go.dev/search?q=$query")
17 |
18 | # if result is empty, there are two possible scenarios:
19 | # 1. the query returned no actual results
20 | # 2. you are unlucky enough to be born in a country banned by the U.S
21 | if [[ -z "$html" ]]; then
22 | echo -n $ErrNoResults
23 | else
24 | packages=$(echo -n $html \
25 | | pup '.SearchSnippet-header-path text{}' \
26 | | sed -r 's/^\((.*)\)$/\1/g')
27 |
28 | # descriptions=$(echo -n $html \
29 | # | pup '.SearchSnippet-synopsis text{}' \
30 | # | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
31 |
32 | IFS=$'\n' packages=($(echo -n $packages))
33 | # IFS=$'\n' descriptions=($(echo -n $descriptions))
34 |
35 | gosearch_debug "packages found: ${#packages[@]}"
36 |
37 | if [[ "${#packages[@]}" == "0" ]]; then
38 | echo -n $ErrNoResults
39 | else
40 | echo -n "$packages"
41 | fi
42 | fi
43 | }
44 |
45 | # ignore will fallback to default completion bound to ^I or
46 | ignore () {
47 | gosearch_debug "ignoring widget"
48 | zle ${default_completion:-expand-or-complete}
49 | }
50 |
51 | gosearch(){
52 | tokens=(${(z)LBUFFER})
53 | query="${tokens[-1]}"
54 | cmd="${tokens[1]}"
55 | subcmd="${tokens[2]}"
56 |
57 | gosearch_debug "query: $query, cmd: $cmd, subcmd: $subcmd"
58 |
59 | if [[ "$cmd" != "go" || "$subcmd" != "get" ]]; then
60 | ignore
61 | return 0
62 | fi
63 |
64 | gosearch_debug "go get has been called"
65 |
66 | # Only search for packages if no space is pressed
67 | # for exampe when go get http is in the buffer
68 | # and we hit , do nothing!
69 | if [ "${LBUFFER[-1]}" != " " ]; then
70 | results=$(_gosearch $query)
71 | if [[ "$results" == "$ErrNoResults" ]]; then
72 | gosearch_debug "$results"
73 | ignore
74 | return 0
75 | fi
76 |
77 | result=$(echo -n "$results" | fzf -1)
78 | gosearch_debug "result: $result"
79 | tokens[-1]=$(printf "%q" "$result")
80 | LBUFFER=${tokens[@]}
81 | fi
82 | }
83 |
84 | # Record what ctrl+i is currently set to
85 | # That way we can call it if currentword_default_completion doesn't result in anything
86 | [ -z "$default_completion" ] && {
87 | binding=$(bindkey '^I')
88 | [[ $binding =~ 'undefined-key' ]] || default_completion=$binding[(s: :w)2]
89 | unset binding
90 | }
91 | zle -N gosearch
92 | bindkey '^I' gosearch
93 |
--------------------------------------------------------------------------------
/install.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | set -e
4 |
5 | # Script URL
6 | SCRIPT_URL="https://raw.githubusercontent.com/itzloop/gosearch/main/gosearch" # Replace with your actual script URL
7 |
8 | # Check for zsh
9 | if ! which zsh &> /dev/null; then
10 | echo "zsh is not installed. This script is designed for zsh."
11 | echo "Installation instructions: https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH"
12 | exit 1
13 | fi
14 |
15 | if [[ $SHELL != *"zsh"* ]]; then
16 | echo "$SHELL is the default shell"
17 | echo "but zsh must be your defaul shell"
18 | echo "chsh -s $(command -v zsh)"
19 | exit 1
20 | fi
21 |
22 | # Check for fzf
23 | if ! command -v fzf &> /dev/null; then
24 | echo "fzf is not installed."
25 | echo "Installation instructions: https://github.com/junegunn/fzf"
26 | exit 1
27 | fi
28 |
29 | # Check for pup
30 | if ! command -v pup &> /dev/null; then
31 | echo "pup is not installed. It's used for parsing data."
32 | echo "Installation instructions: https://github.com/ericchiang/pup"
33 | exit 1
34 | fi
35 |
36 | # Download the script only if previous checks passed
37 | GOSEARCH_PATH="$HOME/.local/share/zsh/zle"
38 | mkdir -p $GOSEARCH_PATH
39 | GOSEARCH_PATH="$GOSEARCH_PATH/gosearch"
40 | ZSHFILE="$HOME/.zshrc"
41 |
42 | echo "Downloading gosearch script..."
43 | curl -sL "$SCRIPT_URL" -o $GOSEARCH_PATH && \
44 | chmod +x $GOSEARCH_PATH
45 |
46 |
47 | SOURCE_LINE="source $GOSEARCH_PATH"
48 |
49 | if grep -q "$SOURCE_LINE" "$ZSHFILE"; then
50 | echo "gosearch is already installed at: $GOSEARCH_PATH and sourced"
51 | exit 0
52 | fi
53 |
54 | echo $SOURCE_LINE >> "$ZSHFILE"
55 |
56 | echo "Installation complete! gosearch can be found in: $GOSEARCH_PATH"
57 | echo "For the current shell you might need to source $GOSEARCH_PATH"
58 |
--------------------------------------------------------------------------------