Skip to content

Commit

Permalink
style: adding source code shell formatting (#35)
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
DeveloperC286 authored Feb 13, 2024
1 parent 46674b9 commit 5cb454b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 58 deletions.
10 changes: 7 additions & 3 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ golang-base:
shell-formatting-base:
FROM +golang-base
RUN go install mvdan.cc/sh/v3/cmd/[email protected]
DO +COPY_CI_DATA
DO +COPY_SOURCECODE


check-shell-formatting:
Expand All @@ -50,6 +50,7 @@ yaml-formatting-base:
COPY ".yamlfmt" ".yamlfmt"
DO +COPY_CI_DATA


check-yaml-formatting:
FROM +yaml-formatting-base
RUN ./ci/check-yaml-formatting.sh
Expand All @@ -61,9 +62,11 @@ check-formatting:


fix-shell-formatting:
FROM +sh-formatting-base
FROM +shell-formatting-base
RUN ./ci/fix-shell-formatting.sh
SAVE ARTIFACT "./ci" AS LOCAL "./ci"
SAVE ARTIFACT "./src" AS LOCAL "./src"
SAVE ARTIFACT "zsh-simple-abbreviations.zsh" AS LOCAL "zsh-simple-abbreviations.zsh"


fix-yaml-formatting:
Expand Down Expand Up @@ -94,7 +97,8 @@ check-github-actions-workflows-linting:

COPY_SOURCECODE:
COMMAND
COPY "./zsh-simple-abbreviations.zsh" "./zsh-simple-abbreviations.zsh"
DO +COPY_CI_DATA
COPY "./zsh-simple-abbreviations.zsh" "./zsh-simple-abbreviations.zsh"
COPY "./src" "./src"
COPY "./end-to-end-tests" "./end-to-end-tests"

Expand Down
2 changes: 1 addition & 1 deletion ci/check-shell-formatting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -o errexit
set -o xtrace

shfmt --simplify --diff ./ci/*
shfmt --simplify --diff ./ci/* ./src/* zsh-simple-abbreviations.zsh
2 changes: 1 addition & 1 deletion ci/fix-shell-formatting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -o errexit
set -o xtrace

shfmt --simplify --write ./ci/*
shfmt --simplify --write ./ci/* ./src/* zsh-simple-abbreviations.zsh
3 changes: 2 additions & 1 deletion src/__zsh_simple_abbreviations_expand
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local MATCH
# Can't use KEY_REGEX, but should match it.
LBUFFER=${LBUFFER%%(#m)[[:alnum:]]#}
# If matches set abbreviation add that else just add what we attempted to match on.
LBUFFER+=${${ZSH_SIMPLE_ABBREVIATIONS[$MATCH]}:-$MATCH}
abbreviation="${ZSH_SIMPLE_ABBREVIATIONS[$MATCH]}"
LBUFFER+=${abbreviation:-$MATCH}
# Causes the syntax highlighting.
zle self-insert
78 changes: 39 additions & 39 deletions src/zsh-simple-abbreviations
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
KEY_REGEX="^[[:alnum:]]+$"

if [[ $# -eq 0 ]]; then
echo "zsh_simple_abbreviations no sub-command or arguments provided."
return 1
echo "zsh_simple_abbreviations no sub-command or arguments provided."
return 1
fi

case $1 in
--set)
if [[ $# -ne 3 ]]; then
echo "zsh_simple_abbreviations set sub-command requires a key and value."
return 1
fi

local key=${2}
local value=${3}

if [[ "${key}" =~ ${KEY_REGEX} ]]; then
ZSH_SIMPLE_ABBREVIATIONS[$key]="${value}"
else
echo "zsh_simple_abbreviations key '${key}' contains non-alphanumeric characters."
return 1
fi
;;

--unset)
if [[ $# -ne 2 ]]; then
echo "zsh_simple_abbreviations unset sub-command requires only a key."
return 1
fi

local key=${2}

if [[ "${key}" =~ ${KEY_REGEX} ]]; then
unset "ZSH_SIMPLE_ABBREVIATIONS[${key}]"
else
echo "zsh_simple_abbreviations key '${key}' contains non-alphanumeric characters."
return 1
fi
;;

*)
echo "zsh_simple_abbreviations unrecognised sub-command."
return 1
;;
--set)
if [[ $# -ne 3 ]]; then
echo "zsh_simple_abbreviations set sub-command requires a key and value."
return 1
fi

local key=${2}
local value=${3}

if [[ ${key} =~ ${KEY_REGEX} ]]; then
ZSH_SIMPLE_ABBREVIATIONS[$key]="${value}"
else
echo "zsh_simple_abbreviations key '${key}' contains non-alphanumeric characters."
return 1
fi
;;

--unset)
if [[ $# -ne 2 ]]; then
echo "zsh_simple_abbreviations unset sub-command requires only a key."
return 1
fi

local key=${2}

if [[ ${key} =~ ${KEY_REGEX} ]]; then
unset "ZSH_SIMPLE_ABBREVIATIONS[${key}]"
else
echo "zsh_simple_abbreviations key '${key}' contains non-alphanumeric characters."
return 1
fi
;;

*)
echo "zsh_simple_abbreviations unrecognised sub-command."
return 1
;;
esac
26 changes: 13 additions & 13 deletions zsh-simple-abbreviations.zsh
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env zsh

# Only run if executed in Zsh environment.
if [[ -n "${ZSH_VERSION}" ]]; then
# Create new abbreviations map.
typeset -Ag ZSH_SIMPLE_ABBREVIATIONS
if [[ -n ${ZSH_VERSION} ]]; then
# Create new abbreviations map.
typeset -Ag ZSH_SIMPLE_ABBREVIATIONS

fpath+=${0:A:h}/src
autoload -Uz zsh-simple-abbreviations
fpath+=${0:A:h}/src
autoload -Uz zsh-simple-abbreviations

# Create key binding on space to expand into a possible abbreviations.
autoload -Uz __zsh_simple_abbreviations_expand
zle -N __zsh_simple_abbreviations_expand
bindkey " " __zsh_simple_abbreviations_expand
# Create key binding on space to expand into a possible abbreviations.
autoload -Uz __zsh_simple_abbreviations_expand
zle -N __zsh_simple_abbreviations_expand
bindkey " " __zsh_simple_abbreviations_expand

# Create key binding on control + space to insert a space without any possible abbreviations expansion.
autoload -Uz __zsh_simple_abbreviations_insert_space
zle -N __zsh_simple_abbreviations_insert_space
bindkey "^ " __zsh_simple_abbreviations_insert_space
# Create key binding on control + space to insert a space without any possible abbreviations expansion.
autoload -Uz __zsh_simple_abbreviations_insert_space
zle -N __zsh_simple_abbreviations_insert_space
bindkey "^ " __zsh_simple_abbreviations_insert_space
fi

0 comments on commit 5cb454b

Please sign in to comment.