Skip to content

Commit 49c4e4f

Browse files
authored
Merge pull request #29 from z-shell/misc
Enhance zsh-diff-so-fancy plugin with error handling and customizatio…
2 parents 7959db3 + 28cd4b3 commit 49c4e4f

File tree

9 files changed

+162
-99
lines changed

9 files changed

+162
-99
lines changed

LICENSE renamed to .github/LICENSE

File renamed without changes.

.github/workflows/trunk-check.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
name: Trunk Code Quality
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
concurrency:
10+
group: ${{ github.head_ref || github.run_id }}
11+
cancel-in-progress: true
12+
13+
permissions: read-all
14+
15+
jobs:
16+
trunk-check:
17+
name: Trunk Code Quality Runner
18+
runs-on: ubuntu-latest
19+
# Provide minimal permissions instead of write-all
20+
permissions:
21+
pull-requests: write
22+
checks: write
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
27+
# Optional caching for trunk to speed up subsequent runs
28+
- name: Cache trunk
29+
uses: actions/cache@v4
30+
with:
31+
path: |
32+
~/.cache/trunk
33+
key: ${{ runner.os }}-trunk-${{ hashFiles('.trunk/trunk.yaml') }}
34+
35+
- name: Trunk Code Quality
36+
uses: trunk-io/trunk-action@v1

.trunk/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
*logs
33
*actions
44
*notifications
5+
tools
6+
tools.log
57
plugins
68
user_trunk.yaml
79
user.yaml
10+
tmp

.trunk/configs/.yamllint.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
rules:
2+
quoted-strings:
3+
required: only-when-needed
4+
extra-allowed: ["{|}"]
5+
key-duplicates: {}
6+
octal-values:
7+
forbid-implicit-octal: true

.trunk/trunk.yaml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
version: 0.1
22
cli:
3-
version: 1.3.2
3+
version: 1.24.0
44
plugins:
55
sources:
66
- id: trunk
7-
ref: v0.0.8
7+
ref: v1.7.0
88
uri: https://github.com/trunk-io/plugins
99
lint:
1010
enabled:
11-
12-
13-
14-
15-
11+
12+
13+
14+
15+
16+
17+
18+
1619
- git-diff-check
17-
- prettier@2.8.3
20+
- prettier@3.5.3
1821
runtimes:
1922
enabled:
20-
21-
23+
24+
25+
2226
actions:
2327
enabled:
2428
- trunk-announce

bin/fancy-diff

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@
33
# Use diff for unified output, and pipe the output to diff-so-fancy
44

55
ff() {
6-
if [[ -z $1 ]] && [[ -z $2 ]]; then
7-
print -Pr "Provide two files to show diff."
6+
if (( $# != 2 )); then
7+
print -Pr "%F{red}Error: Exactly two file arguments are required.%f" >&2
8+
print -P "Usage: fancy-diff <file1> <file2>" >&2
89
return 1
9-
else
10-
command diff -u "$1" "$2" --color=auto | diff-so-fancy | less --tabs=4 -FRXSi
1110
fi
11+
12+
if [[ ! -f "$1" ]]; then
13+
print -Pr "%F{red}Error: File '$1' not found.%f" >&2
14+
return 1
15+
fi
16+
17+
if [[ ! -f "$2" ]]; then
18+
print -Pr "%F{red}Error: File '$2' not found.%f" >&2
19+
return 1
20+
fi
21+
22+
# Use FANCY_DIFF_LESS_OPTS if set, otherwise fallback to a default.
23+
# The plugin typically sets a default for FANCY_DIFF_LESS_OPTS.
24+
local less_opts="${FANCY_DIFF_LESS_OPTS:---tabs=4 -FRXSi}"
25+
command diff -u "$1" "$2" --color=always | diff-so-fancy --patch | less "${less_opts}"
1226
}
27+
1328
ff "$@"

bin/git-dsf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#!/usr/bin/env zsh
22

33
# Git will search $PATH for this file, when invoked by "git dsf ...".
4-
54
f() {
5+
# Use GIT_DSF_LESS_OPTS if set, otherwise fallback to a default.
6+
# The plugin zsh-diff-so-fancy.plugin.zsh typically sets a default for GIT_DSF_LESS_OPTS.
7+
local less_opts="${GIT_DSF_LESS_OPTS:---tabs=4 -FRXSi}"
8+
69
[[ -z "${GIT_PREFIX}" ]] || builtin cd -q "${GIT_PREFIX}" && \
7-
command git diff --color=auto "$@" | diff-so-fancy --patch | less --tabs=4 -FRXSi
10+
command git diff --color=always "$@" | diff-so-fancy --patch | less "${less_opts}"
811
}
12+
913
f "$@"

docs/README.md

Lines changed: 47 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,116 +8,80 @@
88
<p><img align="center" src="https://raw.githubusercontent.com/z-shell/wiki/main/static/img/gif/zsh-diff-so-fancy.gif" alt="zsh-diff-so-fancy" /></p>
99
</div><hr />
1010

11-
## The `diff-so-fancy`
11+
# zsh-diff-so-fancy
1212

13-
The [so-fancy/diff-so-fancy](https://github.com/so-fancy/diff-so-fancy) as Github submodule.
13+
A simple plugin integrating [so-fancy/diff-so-fancy](https://github.com/so-fancy/diff-so-fancy) into Zsh. It provides:
1414

15-
The plugin has `bin/git-dsf` script which adds subcommand `dsf` to `git`, and `bin/fancy-diff` to pipe the `diff` output to diff-so-fancy.
15+
- `git dsf`: A subcommand to run Git diffs through diff-so-fancy
16+
- `fancy-diff`: A helper to run a unified diff and beautify it with diff-so-fancy
1617

17-
## Manual configuration
18+
## Quick Start
1819

19-
```shell
20-
diff-so-fancy --colors # View the commands to set the recommended colors
21-
diff-so-fancy --set-defaults # Configure git-diff to use diff-so-fancy and suggested colors
22-
diff-so-fancy --patch # Use diff-so-fancy in patch mode (interoperable with `git add --patch`)
23-
```
20+
1. **Install** [diff-so-fancy](https://github.com/so-fancy/diff-so-fancy#installation) or load it via your preferred method.
21+
2. **Load the plugin** in your `.zshrc` or plugin manager:
22+
```shell
23+
# Example using Zi
24+
zi ice as'program' pick'bin/*'
25+
zi light z-shell/zsh-diff-so-fancy
26+
```
27+
3. **Use** the commands:
28+
- `git dsf <files>` to see a diff rendered by diff-so-fancy
29+
- `fancy-diff <file1> <file2>` to compare two files with diff-so-fancy
2430

25-
```shell
26-
# Configure git to use d-s-f for *all* diff operations
27-
git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"
28-
```
31+
## Manual Configuration
2932

3033
```shell
31-
# Configure git to use d-s-f for `git add --patch`
32-
git config --global interactive.diffFilter "diff-so-fancy --patch"
34+
diff-so-fancy --colors # Show recommended color config
35+
diff-so-fancy --set-defaults # Configure Git to use diff-so-fancy with suggested colors
36+
diff-so-fancy --patch # Patch mode, interoperable with `git add --patch`
3337
```
3438

35-
### Options
36-
37-
#### markEmptyLines
38-
39-
Should the first block of an empty line be colored. (Default: true)
39+
### Git Integration
4040

4141
```shell
42-
git config --bool --global diff-so-fancy.markEmptyLines false
43-
```
44-
45-
#### changeHunkIndicators
46-
47-
Simplify git header chunks to a more human readable format. (Default: true)
48-
49-
```shell
50-
git config --bool --global diff-so-fancy.changeHunkIndicators false
51-
```
52-
53-
#### stripLeadingSymbols
54-
55-
Should the pesky `+` or `-` at line-start be removed. (Default: true)
56-
57-
```shell
58-
git config --bool --global diff-so-fancy.stripLeadingSymbols false
59-
```
60-
61-
#### useUnicodeRuler
62-
63-
By default, the separator for the file header uses Unicode line-drawing characters. If this is causing output errors on your terminal, set this to `false` to use ASCII characters instead. (Default: true)
64-
65-
```shell
66-
git config --bool --global diff-so-fancy.useUnicodeRuler false
67-
```
68-
69-
#### rulerWidth
70-
71-
By default, the separator for the file header spans the full width of the terminal. Use this setting to set the width of the file header manually.
42+
# Use diff-so-fancy for all diffs
43+
git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"
7244

73-
```shell
74-
git config --global diff-so-fancy.rulerWidth 47 # git log's commit header width
45+
# Use diff-so-fancy for `git add --patch`
46+
git config --global interactive.diffFilter "diff-so-fancy --patch"
7547
```
7648

77-
## Install with [Zi](https://github.com/z-shell/zi)
49+
### diff-so-fancy Options
7850

79-
The project [so-fancy/diff-so-fancy](https://github.com/so-fancy/diff-so-fancy) integration with Zi.
51+
- **markEmptyLines** (bool, default: true)
52+
Color the first block of an empty line.
53+
- **changeHunkIndicators** (bool, default: true)
54+
Simplify Git header chunks to a more readable format.
55+
- **stripLeadingSymbols** (bool, default: true)
56+
Remove leading `+` or `-` from lines.
57+
- **useUnicodeRuler** (bool, default: true)
58+
Use Unicode line-drawing characters for file headers (disable if your terminal cannot display them).
59+
- **rulerWidth** (int, default: full width)
60+
Set a fixed width for the file header separator.
8061

81-
Simply add two lines to `.zshrc`:
82-
83-
Using [bin-gem-node](https://wiki.zshell.dev/ecosystem/annexes/bin-gem-node) annex (recommended):
62+
### Installation with [Zi](https://github.com/z-shell/zi)
8463

8564
```shell
8665
zi ice as'null' sbin'bin/*'
8766
zi light z-shell/zsh-diff-so-fancy
8867
```
8968

90-
Default:
69+
Or:
9170

9271
```shell
9372
zi ice as'program' pick'bin/*'
9473
zi light z-shell/zsh-diff-so-fancy
9574
```
9675

97-
This will add `diff-so-fancy`, `fancy-diff`, `git-dsf` to `$PATH` and automatically equip `git` with subcommand `dsf`.
98-
99-
**No need to use the system package manager or manually configure** `git`, however, if you have the following standard line in your `.gitconfig`, it will still work as expected:
100-
101-
```ini
102-
[core]
103-
pager = diff-so-fancy | less --tabs=4 -RFX
104-
[interactive]
105-
diffFilter = diff-so-fancy --patch
106-
```
107-
108-
## Other plugin managers
109-
110-
### Zplug
111-
112-
```shell
113-
zplug "z-shell/zsh-diff-so-fancy", as:command, use:"bin/"
114-
```
115-
116-
### Zgen
76+
This places `diff-so-fancy`, `fancy-diff`, and `git-dsf` in `$PATH`, adding the `dsf` subcommand to Git automatically.
11777

118-
```shell
119-
zgen load z-shell/zsh-diff-so-fancy
120-
```
78+
## Other Plugin Managers
12179

122-
Without `as"program"`-like functionality the `.plugin.zsh` file picks up setup
123-
and simulates adding a command to the system, so `Zgen` and others can work.
80+
- **Zplug**
81+
```shell
82+
zplug "z-shell/zsh-diff-so-fancy", as:command, use:"bin/"
83+
```
84+
- **Zgen**
85+
```shell
86+
zgen load z-shell/zsh-diff-so-fancy
87+
```

zsh-diff-so-fancy.plugin.zsh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,37 @@
88
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
99
0="${${(M)0:#/*}:-$PWD/$0}"
1010

11+
# Ensure consistent behavior and set recommended options
12+
builtin emulate -L zsh ${=${options[xtrace]:#off}:+-o xtrace}
13+
setopt extended_glob warn_create_global typeset_silent no_short_loops rc_quotes no_auto_pushd
14+
15+
# Check if diff-so-fancy is installed
16+
if ! command -v diff-so-fancy >/dev/null 2>&1; then
17+
print -Pr "%F{yellow}Warning: zsh-diff-so-fancy plugin loaded, but 'diff-so-fancy' command not found in PATH.%f" >&2
18+
print -Pr "%F{yellow}Please install diff-so-fancy: https://github.com/so-fancy/diff-so-fancy#installation%f" >&2
19+
return 1
20+
fi
21+
22+
# Default options for `less` used by `fancy-diff`.
23+
# Users can override this by setting FANCY_DIFF_LESS_OPTS in their zshrc.
24+
: "${FANCY_DIFF_LESS_OPTS:=--tabs=4 -FRXSi}"
25+
26+
# Default options for `less` used by `git-dsf`.
27+
# Users can override this by setting GIT_DSF_LESS_OPTS in their zshrc.
28+
: "${GIT_DSF_LESS_OPTS:=--tabs=4 -FRXSi}"
29+
1130
# https://wiki.zshell.dev/community/zsh_plugin_standard#binaries-directory
1231
if [[ $PMSPEC != *b* ]] {
1332
path+=( "${0:h}/bin" )
1433
}
34+
35+
# Optional unload function
36+
zsh_diff_so_fancy_plugin_unload() {
37+
if [[ $PMSPEC != *b* ]]; then
38+
# Remove the bin path if it was added by this plugin.
39+
# This reconstructs the path based on $0, relying on its initial normalization.
40+
path=( "${(@)path:#${0:h}/bin}" )
41+
fi
42+
unfunction zsh_diff_so_fancy_plugin_unload
43+
# Add any other cleanup needed, like unsetting global variables or options set by the plugin.
44+
}

0 commit comments

Comments
 (0)