]> git.armaanb.net Git - charsel.git/blobdiff - charsel
implement shellcheck suggestions
[charsel.git] / charsel
diff --git a/charsel b/charsel
index 6fb47d21309070a9936967e617215b7b19c56cb4..800b964b55d63d71183b439088c9c2d051a705f5 100755 (executable)
--- a/charsel
+++ b/charsel
@@ -19,7 +19,7 @@
 
 ########################################################################
 
-VERSION=2.0.7
+VERSION=2.0.8
 
 # Define argument functions
 function usage() {
@@ -33,7 +33,8 @@ A simple terminal character selector
   -L         show installed charfiles without the message
   -l         show installed charfiles
   -n         dont copy character to clipboard, avoids Xclip dependency
-  -v         print version
+  -V         check for updates
+  -v         print current version
 
 Exit status:
    0         okay,
@@ -43,9 +44,21 @@ Exit status:
    4         other error"
 }
 
+function version_check(){
+  NEW_VERSION=$(curl -s https://codeberg.org/armaan/charsel/raw/branch/master/charsel \
+    | grep VERSION= | tail -c +9)
+  if [[ $VERSION != "$NEW_VERSION" ]]
+  then
+    echo "an update is available"
+  else
+    echo "you are up to date"
+  fi
+
+}
+
 CHARDIR=$HOME/.cache/charsel
 function list() {
-  ls $CHARDIR/charfiles
+  ls "$CHARDIR"/charfiles
 }
 
 function readme() {
@@ -61,27 +74,27 @@ function missing_charfile() {
 # Merge global and local charfiles
 if [[ -d $CHARDIR ]]
 then
-  rm -rf $CHARDIR/*
+  rm -rf "${CHARDIR:?}"/*
 else
-  mkdir $CHARDIR
+  mkdir "$CHARDIR"
 fi
 
 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
 if [[ ! -d $LOCALCHARDIR ]]
 then
-  mkdir -p $LOCALCHARDIR
+  mkdir -p "$LOCALCHARDIR"
 fi
 
-localdirfiles=($LOCALCHARDIR/*)
-if [[ ! -z "${#localdirfiles[*]}" ]]
+localdirfiles="$LOCALCHARDIR"/*
+if [[ -n "${#localdirfiles[*]}" ]]
 then
-  cp -rf $LOCALCHARDIR/../* $CHARDIR
+  cp -rf "$LOCALCHARDIR"/../* "$CHARDIR"
 fi
 
 globaldirfiles=($/usr/share/charfiles/)
-if [[ ! -z "${#globaldirfiles[*]}" ]]
+if [[ -n "${#globaldirfiles[*]}" ]]
 then
-  cp -rf /usr/share/charsel/* $CHARDIR
+  cp -rf /usr/share/charsel/* "$CHARDIR"
 fi
 
 # Check if charfile exists
@@ -95,7 +108,7 @@ function existence() {
 
 # VERY basic syntax check
 function validity() {
-  if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
+  if [[ $(grep , "$CHARFILE" && grep - "$CHARFILE") ]]
   then
     echo "valid charfile"
   else
@@ -106,11 +119,7 @@ function validity() {
 }
 
 # Look for arguments
-if [ $# -eq 0 ]; then
-  usage
-  exit 3
-fi
-while getopts ":abcdhlLnv" arg
+while getopts ":abcdhlLnvV" arg
 do
   case ${arg} in
     a)
@@ -147,6 +156,10 @@ do
       echo "charsel" $VERSION
       exit 0
       ;;
+    V)
+      version_check
+      exit 0
+      ;;
     ?)
       echo "Invalid option"
       usage
@@ -156,6 +169,11 @@ do
 done
 shift $((OPTIND-1))
 
+if [ $# -eq 0 ]; then
+  echo "Please enter a valid charfile, or use charsel -h for help."
+  charsel -l
+  exit 3
+fi
 # Redefine charfile and check file validity
 CHARFILE="$CHARDIR/charfiles/$1"
 existence
@@ -171,15 +189,8 @@ then
   fi
 fi
 
-if [[ $(echo $LANG | grep UTF-8) -ne 0 ]]
-then
-  echo "Please enable unicode support"
-  exit 4
-fi
-
 # Define length of shortcode
-LENGTH=$(cat $CHARFILE \
-  | cut -f 1 -d ',' -s \
+LENGTH=$(cut "$CHARFILE" -f 1 -d ',' -s \
   | wc -L \
   | cut -b 1)
 
@@ -203,27 +214,27 @@ while true
 do
   # Format output
   echo "*---------*---------------*"
-  echo "| ${bold}${magenta}CHARSEL${white}${normal} |" $1
+  echo "| ${bold}${magenta}CHARSEL${white}${normal} |" "$1"
   echo "*---------*---------------*"
   echo ""
 
   if [[ $SHOWALL == true ]]
   then
-    cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
+    sed "$CHARFILE" -e '/^[ \t]*#/d' | tail -n +2 \
       | column -t -N "input","output" --output-separator ' | ' --separator ','
   else
-    cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
+    sed "$CHARFILE" -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
       | column -t -N "input","output" --output-separator ' | ' --separator ','
   fi
 
   echo ""
   echo "*-------------------------*"
-  echo "| previous shorcode:" $INPUT
-  echo "| previous output:  " $OUTPUT
+  echo "| previous shorcode:" "$INPUT"
+  echo "| previous output:  " "$OUTPUT"
   echo "*-------------------------*"
 
   # User input
-  read -p "| ${bold}input shortcode: ${normal}  " -N $LENGTH INPUT
+  read -p "| ${bold}input shortcode: ${normal}  " -N "$LENGTH" INPUT
 
   if [[ $INPUT == ";"* ]] # Semicolon exts
   then
@@ -235,13 +246,13 @@ do
     clear
   else
     # Finds line number of shortcode
-    LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
+    LINENUMBER=$(cut -f 1 -d ',' -s "$CHARFILE" | grep -wn "$INPUT" | cut -d : -f 1)
 
     # Outputs character selected above
-    OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
+    OUTPUT=$(cut -f 2 -d ',' -s "$CHARFILE" | head -"$LINENUMBER" | tail +"$LINENUMBER")
 
     # Check to see if the shortcode actually exists
-    if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
+    if [[ $(cut -f 1 -d ',' -s "$CHARFILE" | grep -wnc "$INPUT") == "0" ]]
     then
       OUTPUT=""
     fi
@@ -249,7 +260,7 @@ do
     # Copy output to clipboard
     if [[ ! $COPY == "no" ]]
     then
-      echo -n $OUTPUT | xclip -selection clipboard
+      echo -n "$OUTPUT" | xclip -selection clipboard
     fi
 
     clear