aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/sh/.local/bin/lessfilter
blob: 340ed27ac59eb11e06ac55550d3c30f61e4970ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/sh

SCRIPT="$(basename "$0")"

long='width,verbose,help'
short='wvh'

if ! opts="$(getopt -o $short -l $long -n "$SCRIPT" -- "$@")"; then
	exit 1
fi

eval set -- "$opts"

help() {
	cat <<-EOF
		$SCRIPT
		Toby Vincent <tobyv@tobyvin.dev>

		$SCRIPT
		    Filter script used for lesspipe.sh filtering

		USAGE:
		    $SCRIPT [OPTION ...] <PATH> [PATH ...]

		OPTIONS:
		    -w, --width           Specify width. By default uses FZF_PREVIEW_COLUMNS if
		                              set, and falls back to COLUMNS
		    -v, --verbose         Increase verbosity
		    -h, --help            Show this help
	EOF
}

say() {
	if [ -z "$LESSQUIET" ]; then
		printf "%s: %s\n" "$SCRIPT" "$*"
	fi
}

say_verbose() {
	if [ "$verbose" -gt "0" ]; then
		say "$*"
	fi
}

say_err() {
	say "$*" >&2
}

err() {
	err_dir="$1"
	shift
	say_err "cannot preview '$err_dir': $*"
	exit 1
}

err_help() {
	help
	err "$*"
}

has() {
	command -v "$1" >/dev/null
}

in_git_repo() {
	[ -d "$1/.git" ] || git -C "$1" rev-parse --is-inside-work-tree >/dev/null 2>&1
}

filetype() {
	case "$1" in
	https://git.sr.ht*) ftype="sourcehut" ;;
	https://github.com*) ftype="github" ;;
	*.md | *.MD | *.mkd | *.markdown | *.rst) ftype='markdown' ;;
	/*) [ -d "$1" ] && ftype='directory' ;;
	"${1%/*}/${1#*/}")
		for remote in ${GIT_REMOTES:-https://git.sr.ht https://github.com}; do
			if git ls-remote "$remote/$1" CHECK_GIT_REMOTE_URL_REACHABILITY; then
				ftype=$(filetype "$remote/$1")
				break
			fi
		done
		;;
	esac

	printf %s\\n "$ftype"
}

verbose=0
width=${FZF_PREVIEW_COLUMNS:-$COLUMNS}
while true; do
	case "$1" in
	-h | --help)
		help
		exit 0
		;;
	-v | --verbose)
		verbose=$((verbose + 1))
		shift
		;;
	-w | --width)
		width=$2
		shift 2
		;;
	--)
		shift
		break
		;;
	*)
		err_help "Invalid argument: $1"
		;;
	esac
done

if [ "$#" -eq 0 ]; then
	IFS='
'
	set -o noglob
	# shellcheck disable=2046
	set -- $(cat)
fi

if [ -z "$width" ]; then
	width=$(tput cols)
fi

ft=$(filetype "$1")

case "$ft" in
directory)
	if has onefetch && in_git_repo "$1" 2>/dev/null; then
		if [ "$width" -lt "80" ]; then
			version=$(
				(
					onefetch --version | sed 's/onefetch //'
					printf '2.16'
				) | sort -V | tail -n1
			)

			if [ "$version" = '2.16.0' ]; then
				args='--no-art'
			else
				args='--show-logo=never'
			fi
		fi

		cmd="onefetch --include-hidden $args"
	elif [ -f "$1/README.md" ]; then
		exec $SCRIPT "$1/README.md"
	else
		cmd="exa --tree --git-ignore --level=3 --icons"
	fi
	;;
markdown)
	had bat && cmd="bat --color always"
	;;
sourcehut)
	has hut && cmd="hut git show --repo"
	;;
github)
	has gh && cmd="gh repo view"
	;;
esac

if [ -z "$cmd" ]; then
	exit 1
fi

$cmd "$1" 2>/dev/null