-
Notifications
You must be signed in to change notification settings - Fork 5
/
fuz_scripts.sh
executable file
·116 lines (103 loc) · 3.32 KB
/
fuz_scripts.sh
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
#!/bin/bash
# https://stackoverflow.com/questions/55186799/multi-process-bash-within-fzf-preview-feature
set -euo pipefail
string2arg() {
export arg_filename=$(cut -d":" -f1 <<<"$*")
export arg_linenum=$(cut -d":" -f2 <<<"$*")
min_offset=3
let max_offset="min_offset*10"
min=0
if (($min_offset < $arg_linenum)); then
let min="arg_linenum-$min_offset"
fi
let max="arg_linenum+$max_offset"
bat --color=always --highlight-line "$arg_linenum" \
--style=header,grid,numbers \
--line-range "$min":"$max" "$arg_filename"
}
# Ctrl + E: Vim edit
# file="$dir/$file"
# Input: $file, $linematch
function _fze_read() {
file=${1:-.""}
linematch=${2:-0}
echo \"$(realpath "$file")\"
# If valid file, open with vim if edit (-e) flag, else less
if [[ -f $file ]]; then
# Less w/ colors, highlight match, insensitive+incremental search, enable CTRL+C
lineopen=$((${linematch:-0} >= 1 ? ${linematch:-0} - 1 : 0))
bat "$file" --color=always --style plain \
--highlight-line "${linematch:-0}" \
--pager="less -R +${lineopen:-0}g --ignore-case"
fi
}
# Ctrl + F: Less preview
# file="$dir/$file"
# Input: $file, $linematch
function _fze_write() {
file=${1:-.""}
linematch=${2:-0}
echo \"$(realpath "$file")\"
if [[ -f $file ]]; then
# Vim mouse-mode, start at top, softwrap, no numbering/highlight, clipboard copy-paste
"${FUZ_EDITOR:-vim}" \
+":set mouse=a" +":silent! normal g;" \
+":set number nohlsearch" \
+":set wrap linebreak nolist" \
+":set textwidth=0 showbreak= colorcolumn= conceallevel=0 " \
+":set clipboard^=unnamed,unnamedplus" \
+"${linematch:-0}" +"normal zt" \
"$file"
fi
}
function _fz_read() {
# Extract filename and linenumber from match
dir="$1"
filematch="$2"
file=$(cut -d":" -f1 <<<"$filematch")
file="$dir/$file"
linematch=$(cut -d":" -f2 <<<"$filematch")
echo "$file:$linematch"
# If valid file, open with vim if edit (-e) flag, else less
if [[ -f $file ]]; then
# Less w/ colors, highlight match, insensitive+incremental search, enable CTRL+C
lineopen=$((${linematch:-0} >= 1 ? ${linematch:-0} - 1 : 0))
bat "$file" --color=always --style plain \
--highlight-line "${linematch:-0}" \
--pager="less -R +${lineopen:-0}g --ignore-case"
fi
}
function _fz_write() {
# Extract filename and linenumber from match
dir="$1"
filematch="$2"
file=$(cut -d":" -f1 <<<"$filematch")
file="$dir/$file"
linematch=$(cut -d":" -f2 <<<"$filematch")
echo "$file:$linematch"
# If valid file, open with vim if edit (-e) flag, else less
if [[ -f $file ]]; then
# Vim mouse-mode, start at top, softwrap, no numbering/highlight, clipboard copy-paste
"${FUZ_EDITOR:-vim}" \
+":set mouse=a" +":silent! normal g;" \
+":set number nohlsearch" \
+":set wrap linebreak nolist" \
+":set textwidth=0 showbreak= colorcolumn= conceallevel=0 " \
+":set clipboard^=unnamed,unnamedplus" \
+"${linematch:-0}" +"normal zt" \
"$file"
fi
}
function _fz_open() {
# Extract filename and linenumber from match
dir="$1"
filematch="$2"
file=$(cut -d":" -f1 <<<"$filematch")
file="$dir/$file"
linematch=$(cut -d":" -f2 <<<"$filematch")
echo "$file:$linematch"
# If valid file, open with vim if edit (-e) flag, else less
if [[ -f $file ]]; then
open "$file"
fi
}