Skip to content

Commit

Permalink
fix: fix fuzzy completion bash script and update default commands(#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehran-prs authored May 21, 2024
1 parent 0690b87 commit 9594eb0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
72 changes: 46 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ Snip is a simple and minimal command-line snippet manager.

- View your snippets on command line and also manage them (create, edit, delete) using your favorite editor.
- Command-line auto-completion for the snippets names (supports `bash`, `zsh`, `fish` and `powershell`).
- Seamlessly integration with `fzf` to provide fuzzy search.
- Seamlessly integration with `fzf` to provide fuzzy completion.
- Syntax highlighting (using `bat` and `glow` by default) and Git integration

### Usage
### How to use

#### View a snippet

- Run `snip {snippet_name}` to view a snippet.
- If you've
enabled [`fzf` shell integration](https://github.com/junegunn/fzf?tab=readme-ov-file#setting-up-shell-integration) in
you bash or zsh,
you can find snippets by fuzzy search. e.g., type `snip **` and pres tab.
you can find snippets by fuzzy completion. e.g., type `snip **` and pres tab.

![snip view snippets](docs/images/snip-view.gif)

Expand Down Expand Up @@ -71,24 +71,14 @@ git add -A && git commit -m "Initial commit"
git push -u origin main
```

### Prerequisites

- [`bat`](https://github.com/sharkdp/bat) and [`glow`](https://github.com/charmbracelet/glow) to render your snippets. (
you can customize `snip` to use other tools to
render files)
- (optional): [`fzf`](https://github.com/junegunn/fzf) to enable fuzzy-search in auto-completion.
(currently `fzf` autocompletion feature is supported just in `bash` and `zsh`).

### Getting started

- [Install prerequisites](#prerequisites): Install `bat` and `glow` commands. and optionally(recommended) `fzf` if
you're
using bash or zsh.
- [Install the snip command](#installation).
- [Enable auto-completion](#setting-up-shell-integration)
- [Set `SNIP_DIR`](#customization) env variables in your shell config (e.g., `~/.zshrc`) to point to your snippets
directory
- [Use `snip`](#usage) :))
- (optional) Set custom [snippets directory path](#customization).
- (optional) [Enable syntax highlighting](#enable-syntax-highlighting)
- (optional) [Enable fuzzy completion if your shell is bash or zsh](#enable-fuzzy-completion).
- [Use `snip`](#how-to-use) :))

### Installation

Expand All @@ -100,7 +90,10 @@ go install -ldflags "-X main.Version=main -X main.Date=`date +'%FT%TZ%z'`" gith

Or get pre-compiled executables [here](http://github.com/mehran-prs/snip/releases)

### Setting up shell integration
> [!IMPORTANT]
> To set up completion, see the [instructions below](#setting-up-shell-integration).
### Shell integration

Add the following line to your shell configuration file.

Expand All @@ -122,23 +115,23 @@ Add the following line to your shell configuration file.

> [!NOTE]
> [fzf shell integration](https://github.com/junegunn/fzf?tab=readme-ov-file#setting-up-shell-integration) is a
> pre-requisite
> of snip fuzzy search in auto-completion.
> pre-requisite of snip fuzzy completion.
### Customization

Set the following env variables to customize snip:
Set the following env variables to customize snip(e.g., put `export SNIP_DIR=/path/to/dir` in your shell config file):

| Name | Default | Description |
|--------------------------|-----------------------------------------------------|---------------------------------------------------------------------------------|
| SNIP_DIR | `~/snippets` | The snippets directory. It must be absolute path |
| SNIP_FILE_VIEWER_CMD | `bat --style plain --paging never` | The tool which renders non-markdown files in cmd |
| SNIP_MARKDOWN_VIEWER_CMD | `glow` | The tool which renders markdown files in cmd |
| SNIP_FILE_VIEWER_CMD | `cat` | The tool which renders non-markdown files in cmd |
| SNIP_MARKDOWN_VIEWER_CMD | `cat` | The tool which renders markdown files in cmd |
| SNIP_EDITOR | Value of the `EDITOR` env variable, otherwise `vim` | The editor which snip uses to let you edit snippets |
| SNIP_GIT | `git` | The git command which it uses to sync snippets with your remote git repository |
| SNIP_EXCLUDE | `.git,.idea` | comma-separated list of directories that you want to exclude in auto-completion |
| SNIP_VERBOSE | "" | Enable verbose mode |
| SNIP_LOG_TMP_FILENAME | "" | Set a temporary log file. it's helpful in autocompletion debugging |
| SNIP_VERBOSE | "" | Enable verbose mode (values: `true`) |
| SNIP_LOG_TMP_FILENAME | "" | Set path to a temporary log file. it's helpful in autocompletion debugging |


### Commands

Expand All @@ -159,6 +152,33 @@ Available Commands:
Flags:
-h, --help help for snip
```
### Enable syntax highlighting

- Install [`bat`](https://github.com/sharkdp/bat) and [`glow`](https://github.com/charmbracelet/glow).

- Set the following env variables in your shell configuration(e.g., `~/.zshrc`):

```bash
export SNIP_FILE_VIEWER_CMD="bat --style plain --paging never"
export SNIP_MARKDOWN_VIEWER_CMD="glow"
```

> [!IMPORTANT]
> On some operating systems (like ubuntu), the `bat` executable may be installed as `batcat` instead of `bat`, in such
> cases, set `batcat` instead of `bat` in `SNIP_FILE_VIEWER_CMD` env variable.
### Enable fuzzy completion

- Install [`fzf`](https://github.com/junegunn/fzf) to enable fuzzy completion.
- Set up [`fzf` shell integration](https://github.com/junegunn/fzf?tab=readme-ov-file#setting-up-shell-integration)

> [!Note]
> Fuzzy completion is supported just in bash and zsh.

> [!NOTE]
> In your shell configuration, `fzf` shell integration needs to be enabled before the `snip` shell integration.

### Multi-tenancy (Advanced usage)

Expand All @@ -174,7 +194,7 @@ for example to add the `tasks` command, follow these steps:
ln -s $(whereis snip | awk '{print $2}') /usr/local/bin/tasks
```

- Update your shell config to set the tasks directory for the `tasks` command(as its snippets directory) and also
- Update your shell config to set the tasks directory for the `tasks` command(as its snippets directory) and also
enable autocompletion for it:

```bash
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func loadConfig(globalPrefix string, appPrefix string) (err error) {
Cfg.Exclude = strings.Split(exclude, ",")
}

Cfg.FileViewerCMD, err = parseCommand(env("file_viewer_cmd", "bat --style plain --paging never"))
Cfg.FileViewerCMD, err = parseCommand(env("file_viewer_cmd", "cat"))
if err != nil {
return
}
Cfg.MarkdownViewerCMD, err = parseCommand(env("markdown_viewer_cmd", "glow"))
Cfg.MarkdownViewerCMD, err = parseCommand(env("markdown_viewer_cmd", "cat"))
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestLoadDefaultConfig(t *testing.T) {

assertEqual(t, loadConfig("TEST", "TEST"), nil)
assertEqual(t, Cfg.Dir, path.Join(homeDir, "snippets"))
assertEqualSlice(t, Cfg.FileViewerCMD, []string{"bat", "--style", "plain", "--paging", "never"})
assertEqualSlice(t, Cfg.MarkdownViewerCMD, []string{"glow"})
assertEqualSlice(t, Cfg.FileViewerCMD, []string{"cat"})
assertEqualSlice(t, Cfg.MarkdownViewerCMD, []string{"cat"})
assertEqual(t, Cfg.Editor, "abc")
assertEqual(t, Cfg.Git, "git")
assertEqualSlice(t, Cfg.Exclude, []string{".git", ".idea"})
Expand Down
9 changes: 8 additions & 1 deletion fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ _fzf_complete_%[1]s() {
}

func genFzfBashCompletion(appName string) string {
return genFzfZshCompletion(appName)
return fmt.Sprintf(`
### fzf Autocomplete function
_fzf_complete_%[1]s() {
_fzf_path_completion $(%[1]s dir $prefix) "$@"
}
type __fzf_defc &>/dev/null && __fzf_defc %[1]s _fzf_complete_%[1]s "-o default -o bashdefault"
`, appName)
}
14 changes: 11 additions & 3 deletions fzf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ package main
import "testing"

func TestGenFzfCompletion(t *testing.T) {
res := `
zshRes := `
### fzf Autocomplete function
_fzf_complete_abc() {
_fzf_path_completion $(abc dir $prefix) "$@"
}
`
assertEqual(t, genFzfZshCompletion("abc"), res)
assertEqual(t, genFzfBashCompletion("abc"), res)
bashRes := `
### fzf Autocomplete function
_fzf_complete_abc() {
_fzf_path_completion $(abc dir $prefix) "$@"
}
type __fzf_defc &>/dev/null && __fzf_defc abc _fzf_complete_abc "-o default -o bashdefault"
`
assertEqual(t, genFzfZshCompletion("abc"), zshRes)
assertEqual(t, genFzfBashCompletion("abc"), bashRes)
}

0 comments on commit 9594eb0

Please sign in to comment.