]> git.armaanb.net Git - charsel.git/blobdiff - charsel
implement shellcheck suggestions
[charsel.git] / charsel
diff --git a/charsel b/charsel
index cef174685cf360ca8b4651ede1b25766573d420f..800b964b55d63d71183b439088c9c2d051a705f5 100755 (executable)
--- a/charsel
+++ b/charsel
@@ -19,9 +19,7 @@
 
 ########################################################################
 
-VERSION=2.0.7
-
-# Environment variables
+VERSION=2.0.8
 
 # Define argument functions
 function usage() {
@@ -34,7 +32,9 @@ A simple terminal character selector
   -h         show this message
   -L         show installed charfiles without the message
   -l         show installed charfiles
-  -v         print version
+  -n         dont copy character to clipboard, avoids Xclip dependency
+  -V         check for updates
+  -v         print current version
 
 Exit status:
    0         okay,
@@ -44,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() {
@@ -62,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
@@ -96,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
@@ -107,13 +119,23 @@ function validity() {
 }
 
 # Look for arguments
-if [ $# -eq 0 ]; then
-  usage
-  exit 3
-fi
-while getopts ":baLlchdv" arg
+while getopts ":abcdhlLnvV" arg
 do
   case ${arg} in
+    a)
+      SHOWALL="true"
+      ;;
+    b)
+      COLOR="bw"
+      ;;
+    c)
+      validity
+      exit 0
+      ;;
+    d)
+      readme
+      exit 0
+      ;;
     h)
       usage
       exit 0
@@ -127,24 +149,17 @@ do
       list
       exit 0
       ;;
-    d)
-      readme
-      exit 0
-      ;;
+    n)
+      COPY="no"
+      ;; 
     v)
       echo "charsel" $VERSION
       exit 0
       ;;
-    c)
-      validity
+    V)
+      version_check
       exit 0
       ;;
-    a)
-      SHOWALL="true"
-      ;;
-    b)
-      COLOR="bw"
-      ;;
     ?)
       echo "Invalid option"
       usage
@@ -154,27 +169,28 @@ 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
 validity
 
 # Dependency check
-if [[ -x xclip ]]
+if [[ ! $COPY == "no" ]]
 then
-  echo "Please install xclip."
-  exit 4
-fi
-
-if [[ $(echo $LANG | grep UTF-8) -ne 0 ]]
-then
-  echo "Please enable unicode support"
-  exit 4
+  if [[ -x xclip ]]
+  then
+    echo "Please install xclip."
+    exit 4
+  fi
 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)
 
@@ -198,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
@@ -230,19 +246,22 @@ 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
 
     # Copy output to clipboard
-    echo -n $OUTPUT | xclip -selection clipboard
+    if [[ ! $COPY == "no" ]]
+    then
+      echo -n "$OUTPUT" | xclip -selection clipboard
+    fi
 
     clear
   fi