Format code using shfmt
This commit is contained in:
parent
8935c2c131
commit
5c3781ceb6
75
snippy
75
snippy
@ -85,7 +85,7 @@ snippet=
|
||||
# @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
|
||||
bashdown() {
|
||||
while IFS= read -r line; do
|
||||
line="$(eval "printf -- \"$( printf "%s" "$line" | sed 's/"/\\"/g' )\"")";
|
||||
line="$(eval "printf -- \"$(printf "%s" "$line" | sed 's/"/\\"/g')\"")"
|
||||
echo -e "$line"
|
||||
done
|
||||
}
|
||||
@ -149,7 +149,7 @@ move_cursor() {
|
||||
if [[ $count -gt 0 ]]; then
|
||||
until [ "$count" -eq 0 ]; do
|
||||
keys+="${key} "
|
||||
((count-=1)) || true
|
||||
((count -= 1)) || true
|
||||
done
|
||||
# shellcheck disable=SC2086
|
||||
if [[ $is_wayland ]]; then
|
||||
@ -164,9 +164,9 @@ init() {
|
||||
# Check basic dependency
|
||||
local all_needed_programs_installed=true
|
||||
if [[ $is_wayland ]]; then
|
||||
local needed_programs=( wofi fzf wtype wl-copy wl-paste jq )
|
||||
local needed_programs=(wofi fzf wtype wl-copy wl-paste jq)
|
||||
else
|
||||
local needed_programs=( rofi fzf xsel xclip jq xdotool )
|
||||
local needed_programs=(rofi fzf xsel xclip jq xdotool)
|
||||
fi
|
||||
for program in "${needed_programs[@]}"; do
|
||||
if ! command -v "$program" >/dev/null 2>&1; then
|
||||
@ -175,7 +175,7 @@ init() {
|
||||
fi
|
||||
done
|
||||
|
||||
if [ "$all_needed_programs_installed" = false ] ; then
|
||||
if [ "$all_needed_programs_installed" = false ]; then
|
||||
echo -e "\nPlease install the previous dependancies"
|
||||
exit 1
|
||||
fi
|
||||
@ -184,7 +184,7 @@ init() {
|
||||
if [[ ! -d "$snippets_directory" ]]; then
|
||||
mkdir -p "$snippets_directory"
|
||||
echo "$snippets_directory created"
|
||||
echo "hi it is \$(date)" > "$snippets_directory/test"
|
||||
echo "hi it is \$(date)" >"$snippets_directory/test"
|
||||
fi
|
||||
}
|
||||
|
||||
@ -223,9 +223,9 @@ usage() {
|
||||
|
||||
parse_options() {
|
||||
|
||||
while (( "$#" )); do
|
||||
while (("$#")); do
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
-h | --help)
|
||||
usage
|
||||
;;
|
||||
--) # end argument parsing
|
||||
@ -248,16 +248,16 @@ error() {
|
||||
}
|
||||
|
||||
cli() {
|
||||
snippet=$( list | fzf "${fzf_args[@]}")
|
||||
snippet=$(list | fzf "${fzf_args[@]}")
|
||||
}
|
||||
|
||||
list() {
|
||||
local type="${1:-f}"
|
||||
|
||||
find -L . -type "$type" \
|
||||
| grep -vE '^\.$|\.git|\.gitconfig|\.gitkeep|\.gitignore' \
|
||||
| sed -e 's!\.\/!!' \
|
||||
| sort
|
||||
find -L . -type "$type" |
|
||||
grep -vE '^\.$|\.git|\.gitconfig|\.gitkeep|\.gitignore' |
|
||||
sed -e 's!\.\/!!' |
|
||||
sort
|
||||
}
|
||||
|
||||
add() {
|
||||
@ -290,7 +290,7 @@ gui() {
|
||||
# shellcheck disable=SC2086
|
||||
set +o errexit
|
||||
|
||||
snippet=$( list | sed -re 's_^([^/]*)/(.*)_&\x0icon\x1f\1_' | rofi "${rofi_args[@]}" -p '❯ ')
|
||||
snippet=$(list | sed -re 's_^([^/]*)/(.*)_&\x0icon\x1f\1_' | rofi "${rofi_args[@]}" -p '❯ ')
|
||||
|
||||
if [ $? -eq 10 ]; then
|
||||
script_content=true
|
||||
@ -315,31 +315,32 @@ run() {
|
||||
|
||||
# Custom selection, copy the script content without parsing
|
||||
elif [ "$script_content" = true ]; then
|
||||
content="$( cat "${snippets_directory}/${snippet}" )"
|
||||
content="$(cat "${snippets_directory}/${snippet}")"
|
||||
|
||||
# don't parse file with the ##noparse header
|
||||
elif grep -qE "^##noparse" "${snippets_directory}/${snippet}"; then
|
||||
content="$( tail -n +2 "${snippets_directory}/${snippet}" )"
|
||||
content="$(tail -n +2 "${snippets_directory}/${snippet}")"
|
||||
|
||||
# replace tmpfile for snippets with ##tmpfile header
|
||||
elif grep -qE "^##tmpfile" "${snippets_directory}/${snippet}"; then
|
||||
content="$( bashdown_simple <<< "$(tail -n +2 "${snippets_directory}/${snippet}" | sed "s%\$tmpfile%$tmpfile%g" )" )"
|
||||
content="$(bashdown_simple <<<"$(tail -n +2 "${snippets_directory}/${snippet}" | sed "s%\$tmpfile%$tmpfile%g")")"
|
||||
|
||||
# execute bash script in scripts dir
|
||||
elif [[ $(dirname "${snippet}") == 'scripts' ]] && grep -qE "^#!/bin/bash" "${snippets_directory}/${snippet}"; then
|
||||
content="$( bash "${snippets_directory}/${snippet}" )"
|
||||
content="$(bash "${snippets_directory}/${snippet}")"
|
||||
|
||||
# default action
|
||||
else
|
||||
content="$( bashdown_simple < "${snippets_directory}/${snippet}" )"
|
||||
content="$(bashdown_simple <"${snippets_directory}/${snippet}")"
|
||||
fi
|
||||
|
||||
if [ -n "$content" ]; then
|
||||
printf "%s" "$content" > "$tmpfile"
|
||||
printf "%s" "$content" >"$tmpfile"
|
||||
fi
|
||||
|
||||
else [[ ${snippet} =~ ^$ ]]
|
||||
${snippet##*$} 2>/dev/null > "$tmpfile" # execute as bashcommand
|
||||
else
|
||||
[[ ${snippet} =~ ^$ ]]
|
||||
${snippet##*$} 2>/dev/null >"$tmpfile" # execute as bashcommand
|
||||
fi
|
||||
|
||||
# if tmpfile is empty at this step, nothing to do.
|
||||
@ -379,7 +380,7 @@ run() {
|
||||
|
||||
if grep -q "$placeholder_clipboard_urlencode" "$tmpfile"; then
|
||||
awk -i inplace \
|
||||
-v clipboard="$( echo "$current_clipboard" | jq -sRr @uri)" \
|
||||
-v clipboard="$(echo "$current_clipboard" | jq -sRr @uri)" \
|
||||
-v placeholder="$placeholder_clipboard_urlencode" \
|
||||
'{ gsub(placeholder, clipboard); print }' "$tmpfile"
|
||||
|
||||
@ -398,15 +399,15 @@ run() {
|
||||
# retrieve the line
|
||||
cursor_line=$(grep $placeholder_cursor "$tmpfile")
|
||||
# calculate snippet total lines
|
||||
file_lines=$(wc -l < "$tmpfile")
|
||||
file_lines=$(wc -l <"$tmpfile")
|
||||
# determine the number of line to go up
|
||||
cursor_line_position=$(( file_lines - cursor_line_position + 1 ))
|
||||
cursor_line_position=$((file_lines - cursor_line_position + 1))
|
||||
# Extract cursor position
|
||||
cursor_position=$(strindex "$cursor_line" $placeholder_cursor)
|
||||
# total cursor line lenght
|
||||
cursor_line_lenght=${#cursor_line}
|
||||
# Compute the final cursor position ( 8 is the lenght of the placeholder {cursor} )
|
||||
cursor_position=$(( cursor_line_lenght - cursor_position - 8 ))
|
||||
cursor_position=$((cursor_line_lenght - cursor_position - 8))
|
||||
# remove the placeholder
|
||||
sed -i -e "s/$placeholder_cursor//g" "$tmpfile"
|
||||
fi
|
||||
@ -414,19 +415,19 @@ run() {
|
||||
# Copy snippet in clipboard
|
||||
if is_rich_snippet "${snippets_directory}/${snippet}"; then
|
||||
if [[ $is_wayland ]]; then
|
||||
wl-copy --type text/html -o < "$tmpfile"
|
||||
wl-copy --type text/html -o <"$tmpfile"
|
||||
else
|
||||
xclip -target text/html -selection clipboard -in -loops 1 < "$tmpfile"
|
||||
xclip -target text/html -selection clipboard -in -loops 1 <"$tmpfile"
|
||||
fi
|
||||
else
|
||||
if [[ $is_wayland ]]; then
|
||||
wl-copy < "$tmpfile"
|
||||
wl-copy <"$tmpfile"
|
||||
else
|
||||
xsel --clipboard --input < "$tmpfile"
|
||||
xsel --clipboard --input <"$tmpfile"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "$paste" = true ] ; then
|
||||
if [ "$paste" = true ]; then
|
||||
# Paste into the current application.
|
||||
if is_gui; then
|
||||
# We need a little pause to handle the time to focus on the window
|
||||
@ -469,25 +470,25 @@ main() {
|
||||
cd "${snippets_directory}" || exit
|
||||
|
||||
case "$action" in
|
||||
'gui' )
|
||||
'gui')
|
||||
gui
|
||||
run
|
||||
;;
|
||||
'cli' )
|
||||
'cli')
|
||||
cli
|
||||
run false
|
||||
;;
|
||||
'list' )
|
||||
'list')
|
||||
list
|
||||
;;
|
||||
'cat' )
|
||||
'cat')
|
||||
list d
|
||||
;;
|
||||
'add' )
|
||||
'add')
|
||||
shift
|
||||
add "$@"
|
||||
;;
|
||||
'edit' )
|
||||
'edit')
|
||||
cli
|
||||
edit
|
||||
;;
|
||||
|
Loading…
Reference in New Issue
Block a user