-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathless-osc8-open.sh
More file actions
executable file
·64 lines (54 loc) · 1.04 KB
/
Copy pathless-osc8-open.sh
File metadata and controls
executable file
·64 lines (54 loc) · 1.04 KB
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
#!/bin/sh
# Less OSC 8 handler.
echo() { printf %s\\n "$*"; }
# Open a link of the form "file://HOST/PATH" or "file:///PATH"
open_file() {
case $1 in
file:///* | file://localhost/*)
less -- "/${1#file://*/}"
;;
file://*/*)
linkhost=${1#file://}; linkhost=${linkhost%%/*}
localhost=${HOSTNAME:-$(bash -c 'printf %s "$HOSTNAME"' || uname -n)} 2>/dev/null
case $localhost in
*/* | '')
echo "unable to detect local hostname: $localhost"
exit 1
;;
"$linkhost")
less -- "/${1#file://*/}"
;;
*)
echo "cannot open remote file: $1"
exit 1
;;
esac
;;
*)
echo "invalid file link: $1"
exit 1
;;
esac
}
# Open a link of the form "man:NAME" or "man:NAME(SECTION)".
open_man() {
case $1 in
man:?*\(*\) )
sect=${1#*\(}; sect=${sect%?}
name=${1#man:}; name=${name%%\(*}
man -- ${sect:+"$sect"} "$name"
;;
man:?*)
man -- "${1#man:}"
;;
*)
echo "invalid man link: $1"
exit 1
;;
esac
}
case $1 in
file:*) open_file "$1" ;;
man:*) open_man "$1" ;;
*) echo "unsupported link type: $1"; exit 1 ;;
esac