aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2025-01-05 18:55:14 +0100
committerRandy Palamar <randy@rnpnr.xyz>2025-01-06 07:38:01 -0700
commit824a7d19ae5bdc8eb6b3d61a591dc26d33ab3a38 (patch)
treef2e3f324011a852c04247e97caaf2110b7260611
parent75892aad7939b40354be7ea71bd55c651956b27c (diff)
downloadvis-824a7d19ae5bdc8eb6b3d61a591dc26d33ab3a38.tar.gz
vis-824a7d19ae5bdc8eb6b3d61a591dc26d33ab3a38.tar.xz
scripts: fix shellcheck warnings and make scripts more uniform
-rwxr-xr-xvis-clipboard20
-rwxr-xr-xvis-complete46
-rwxr-xr-xvis-open28
3 files changed, 62 insertions, 32 deletions
diff --git a/vis-clipboard b/vis-clipboard
index ad0408d..0f9e90b 100755
--- a/vis-clipboard
+++ b/vis-clipboard
@@ -1,6 +1,7 @@
#!/bin/sh
-
# Copyright (C) 2016 Richard Burke, ISC licensed
+# shellcheck disable=SC2317
+set -e
vc_fatal() {
echo "$@" >&2
@@ -8,7 +9,13 @@ vc_fatal() {
}
vc_usage() {
- vc_fatal "$(basename "$0") [--selection sel] [--usable|--copy|--paste]"
+ vc_fatal "Usage: $(basename "$0") [--selection sel] [--usable|--copy|--paste]
+
+Copy/paste clipboard interface with support on all provided platforms.
+
+Available selections: clipboard, primary
+
+Example: $(basename "$0") --copy --selection primary"
}
vc_determine_command() {
@@ -153,8 +160,13 @@ while [ $# -gt 0 ]; do
--usable) fn=vc_usable;;
--copy) fn=vc_copy;;
--paste) fn=vc_paste;;
- --selection) shift; sel="$1";;
- *) ;;
+ --selection)
+ shift
+ if [ "$1" != "clipboard" ] && [ "$1" != "primary" ]; then
+ vc_fatal "Invalid selection: $1\nValid options are 'clipboard' or 'primary'"
+ fi
+ sel="$1";;
+ *) vc_usage;;
esac
shift
done
diff --git a/vis-complete b/vis-complete
index db925f6..50895b1 100755
--- a/vis-complete
+++ b/vis-complete
@@ -1,16 +1,28 @@
#!/bin/sh
set -e
+fatal() {
+ echo "$@" >&2
+ exit 1
+}
+
usage() {
- printf '%s\n' "usage: $(basename "$0") [--file|--word] pattern" \
- " $(basename "$0") -h|--help"
+ fatal "Usage: $(basename "$0") [--file|--word] [--] pattern
+
+Interactively complete file or word
+
+--file this passes pattern to obtain a list of matching
+ file names (this is the default).
+--word this reads standard input to obtain a list of lines
+ matching pattern.
+pattern pattern to be completed"
}
basic_regex_quote() { printf "%s" "$1" | sed 's|[\\.*^$[]|\\&|g'; }
glob_quote () { printf "%s" "$1" | sed 's|[\\?*[]]|\\&|g'; }
COMPLETE_WORD=0
-FIND_FILE_LIMIT=1000
+FIND_FILE_LIMIT="${FIND_FILE_LIMIT:-1000}"
while [ $# -gt 0 ]; do
case "$1" in
@@ -26,13 +38,8 @@ while [ $# -gt 0 ]; do
shift
break
;;
- -h|--help)
- usage
- exit 0
- ;;
- -*)
+ -*|'')
usage
- exit 1
;;
*)
break
@@ -40,45 +47,44 @@ while [ $# -gt 0 ]; do
esac
done
-if [ $# -ne 1 ]; then
- usage
- exit 1
-fi
+[ "$#" -lt 1 ] && usage
PATTERN="$1"
if [ $COMPLETE_WORD = 1 ]; then
+ # shellcheck disable=SC1003
tr -s '\t {}()[],<>%^&.\\' '\n' |
grep "^$(basic_regex_quote "$PATTERN")." |
sort -u
else
# Expand to absolute path because of the -path option below.
+ # shellcheck disable=SC2088
case $PATTERN in
/*)
- XPATTERN=$PATTERN
+ XPATTERN="$PATTERN"
;;
'~'|'~/'*)
- XPATTERN=$HOME$(echo $PATTERN | tail -c +2)
+ XPATTERN="$HOME$(echo "$PATTERN" | tail -c +2)"
;;
*)
- XPATTERN=$PWD/$PATTERN
+ XPATTERN="$PWD/$PATTERN"
;;
esac
# The first path condition rules out paths that start with "." unless
# they start with "..". That way, hidden paths should stay hidden, but
# non-normalised paths should still show up.
- find $(dirname "$XPATTERN") \
+ find "$(dirname "$XPATTERN")" \
-name '.*' -prune \
-o \( \
! -name '.*' \
-a -path "$(glob_quote "$XPATTERN")*" \
-print \
\) 2>/dev/null |
- head -n $FIND_FILE_LIMIT |
+ head -n "$FIND_FILE_LIMIT" |
sort |
- sed "s|^$(dirname $XPATTERN)/||"
+ sed "s|^$(dirname "$XPATTERN")/||"
fi |
vis-menu -b |
- sed "s|^$(basename $PATTERN)$(echo $PATTERN | tail -c 2 | grep -F /)||" |
+ sed "s|^$(basename "$PATTERN")$(echo "$PATTERN" | tail -c 2 | grep -F /)||" |
tr -d '\n'
diff --git a/vis-open b/vis-open
index 28d1b1a..bcd555a 100755
--- a/vis-open
+++ b/vis-open
@@ -5,13 +5,22 @@ set -e
NL='
'
-if [ -z "$VIS_OPEN_LINES" ]; then
- VIS_OPEN_LINES='0'
-fi
+fatal() {
+ echo "$@" >&2
+ exit 1
+}
-VIS_MENU_PROMPT=''
-ALLOW_AUTO_SELECT='1'
+usage() {
+ fatal "Usage: $(basename "$0") [-f] [-p prompt] [--] [file-pattern]
+
+Interactively select a file to open
+-f always present the given arguments, even when there is only one
+-p prompt
+file-pattern list of filenames and directories"
+}
+
+# print a list of filenames on stdin and distinguish directories
wrap_dirs() {
while read -r filename
do
@@ -23,6 +32,10 @@ wrap_dirs() {
done
}
+VIS_OPEN_LINES="${VIS_OPEN_LINES:-0}"
+VIS_MENU_PROMPT=''
+ALLOW_AUTO_SELECT='1'
+
while getopts fhp: opt; do
case "$opt" in
f)
@@ -31,9 +44,8 @@ while getopts fhp: opt; do
p)
VIS_MENU_PROMPT="$OPTARG"
;;
- h|?)
- printf 'usage: %s [-f] [-h] [-p prompt] [--] [file-pattern]\n' "${0##*/}"
- exit 0
+ *)
+ usage
;;
esac
done