Skip to content

Commit 3fbd094

Browse files
committed
Reindent and stricter fail checking in editor picker
1 parent ef96f58 commit 3fbd094

2 files changed

Lines changed: 39 additions & 34 deletions

File tree

lib/include/common.sh

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
#!/bin/bash
22

33
function echo_arr() {
4-
local i
5-
for i in "$@"
6-
do
7-
echo "$i"
8-
done
4+
local i
5+
for i in "$@"
6+
do
7+
echo "$i"
8+
done
9+
}
10+
11+
function run_cmd() {
12+
local cmd="$1"
13+
bash -c "set -euo pipefail;$cmd" || { err "failed to run '$cmd'"; }
914
}
1015

1116
function fzf_select() {
12-
# usage: fzf_select prompt callback options.."
13-
# example: fzf_select "pick a foo" my_callback foo bar baz
14-
local prompt="$1"
15-
local callback="$2"
16-
local choice
17-
local o
18-
shift
19-
shift
20-
if [ -x "$(command -v fzf)" ]
21-
then
22-
choice="$(echo_arr "$@" | fzf)"
23-
eval "$callback $choice"
24-
else
25-
PS3="$prompt"
26-
select choice in "$@"
27-
do
28-
for o in "$@"
29-
do
30-
if [[ "$o" == "$choice" ]]
31-
then
32-
eval "$callback $choice"
33-
return
34-
fi
35-
done
36-
echo "invalid option $REPLY"
37-
done
38-
fi
17+
# usage: fzf_select prompt callback options.."
18+
# example: fzf_select "pick a foo" my_callback foo bar baz
19+
local prompt="$1"
20+
local callback="$2"
21+
local choice
22+
local o
23+
shift
24+
shift
25+
if [ -x "$(command -v fzf)" ]
26+
then
27+
choice="$(echo_arr "$@" | fzf)"
28+
run_cmd "$callback $choice"
29+
else
30+
PS3="$prompt"
31+
select choice in "$@"
32+
do
33+
for o in "$@"
34+
do
35+
if [[ "$o" == "$choice" ]]
36+
then
37+
run_cmd "$callback $choice"
38+
return
39+
fi
40+
done
41+
echo "invalid option $REPLY"
42+
done
43+
fi
3944
}
4045

lib/include/editor.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function callback_editor() {
44
local file="$1"
55
local _editor="$2"
6-
eval "$_editor $file"
6+
bash -c "set -euo pipefail;$_editor $file" || { err "failed to open editor"; }
77
}
88

99
function edit_file() {
@@ -18,7 +18,7 @@ function edit_file() {
1818
fi
1919
if [ "$selected_editor" != "" ]
2020
then
21-
eval "$selected_editor $file"
21+
bash -c "set -euo pipefail;$selected_editor $file" || { err "failed to open editor"; }
2222
return
2323
fi
2424
fzf_select \

0 commit comments

Comments
 (0)