]> git.armaanb.net Git - charsel.git/commitdiff
See notes
authorArmaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me>
Sun, 18 Oct 2020 02:55:29 +0000 (22:55 -0400)
committerArmaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me>
Sun, 18 Oct 2020 02:55:29 +0000 (22:55 -0400)
This is a major update, in big part thanks to TJ- on freenode.net .
Instead of if statements, arguments are determined with bash's getopts.
Some other changes were made throughout to improve the quality of code.

Makefile
README.md
charsel
usage [deleted file]

index d34ff02ed78201a1452e0da5f8228f71cc6ba8ef..d9ffea19fb81c1170b8653ce12176276b40985b9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,6 @@ install:
 >cp charfiles/* /usr/share/charsel/charfiles/
 >mkdir /usr/share/doc/charsel
 >cp README.md /usr/share/doc/charsel/
 >cp charfiles/* /usr/share/charsel/charfiles/
 >mkdir /usr/share/doc/charsel
 >cp README.md /usr/share/doc/charsel/
->cp usage /usr/share/doc/charsel/
 >cp ./charsel /usr/bin/
 
 uninstall:
 >cp ./charsel /usr/bin/
 
 uninstall:
@@ -24,5 +23,4 @@ reinstall:
 >cp charfiles/* /usr/share/charsel/charfiles/
 >mkdir /usr/share/doc/charsel
 >cp README.md /usr/share/doc/charsel/
 >cp charfiles/* /usr/share/charsel/charfiles/
 >mkdir /usr/share/doc/charsel
 >cp README.md /usr/share/doc/charsel/
->cp usage /usr/share/doc/charsel/
 >cp ./charsel /usr/bin/
 >cp ./charsel /usr/bin/
index 295998dcac59bf9c4b41e8236fbb2513dac3e778..a356335653d7f8d2c954ac53532ed6ffbac8d6c8 100644 (file)
--- a/README.md
+++ b/README.md
@@ -14,14 +14,14 @@ Run `make uninstall` as root to remove. Will not delete your personal charfiles
   * A terminal with UTF-8 encoding enabled
 
 ## Usage
   * A terminal with UTF-8 encoding enabled
 
 ## Usage
-For usage, use `charsel --help`
+For usage, use `charsel -h`
 
 Once you have started the program, simply type the shortcut listed on the left side of the table to copy the character on the right side of the table to your clipboard. Shortcodes the same length as the longest one will automatically be entered. To quit, use ^C
 
 Some of the default charfiles have hidden shortcuts for capital letters that are not shown on the table. Simply type a capital version of the shortcut key to access the capital version of the special character.
 
 ## Creating a charfile
 
 Once you have started the program, simply type the shortcut listed on the left side of the table to copy the character on the right side of the table to your clipboard. Shortcodes the same length as the longest one will automatically be entered. To quit, use ^C
 
 Some of the default charfiles have hidden shortcuts for capital letters that are not shown on the table. Simply type a capital version of the shortcut key to access the capital version of the special character.
 
 ## Creating a charfile
-Make a new file in `~/.local/share/charsel/` or `/usr/share/charsel` with the name that you would like to use to call the charfile in the command.
+Make a new file in the `~/.local/share/charsel/charfiles/` or `/usr/share/charsel/charfiles/` directories with the name that you would like to use to call the charfile in the command.
 
 Fill it in, using the provided examples as a template. Hidden shortcuts go above the '-', lines can be commented out using a '#' at the start of the line
 
 
 Fill it in, using the provided examples as a template. Hidden shortcuts go above the '-', lines can be commented out using a '#' at the start of the line
 
diff --git a/charsel b/charsel
index d2e7525e0c246a9b4a4a165d024e169ce22c9d74..56e1fdf244dfd622206370bb0b014baccb10fe8c 100755 (executable)
--- a/charsel
+++ b/charsel
 
 ########################################################################
 
 
 ########################################################################
 
-# Merge global and local charfiles
+# Define argument functions
+function usage() {
+  echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
+A simple terminal character selector
+  -h              show this message
+  -l              show installed charfiles
+  -d              show readme
+      --all       include hidden shortcodes
+
+Exit status:
+  0    okay,
+  1    invalid charfile
+  2    usage error/invalid option"
+}
+
 CHARDIR=$HOME/.cache/charsel
 CHARDIR=$HOME/.cache/charsel
+function list() {
+  echo "The following charfiles are installed:"
+  ls $CHARDIR/charfiles
+}
 
 
-if [[ ! -d $CHARDIR ]]
-then
-  mkdir $CHARDIR/
-elif [[ ! "$(ls -A $CHARDIR)" ]]
-then
-  cp -rf /usr/share/charsel/- $CHARDIR/
-  cp -rf $HOME/.local/share/charsel/- $CHARDIR/
-fi
+function readme() {
+  cat /usr/share/doc/charsel/README.md
+}
+
+function invalid_charfile() {
+  echo "The selected charfile is missing or invalid"
+  list
+  exit 1
+}
+
+# Look for arguments
+while getopts ":lhd" arg
+do
+  case ${arg} in
+    h)
+      usage
+      exit 0
+      ;;
+    l)
+      list
+      exit 0
+      ;;
+    d)
+      readme
+      exit 0
+      ;;
+    ?)
+      echo "Invalid option"
+      usage
+      exit 2
+      ;;
+    :)
+      break
+      ;;
+  esac
+done
+shift $((OPTIND-1))
 
 
-# Check for xclip
-if [[ ! -f "$(which xclip)" ]]
+# Check if Xclip is installed
+if [[ -x xclip ]]
 then
   echo "Please install xclip."
   exit 1
 fi
 
 then
   echo "Please install xclip."
   exit 1
 fi
 
-# Check for user inputs
-if [[ $1 == "-l" \
-  || $1 == "--list" ]]
+# Merge global and local charfiles
+if [[ -d $CHARDIR ]]
 then
 then
-  echo "The following charfiles are installed:"
-  ls $CHARDIR/charfiles
-  exit 0
-elif [[ $1 == "-h" \
-  || $1 == "--help" ]]
+  rm -rf $CHARDIR/*
+else
+  mkdir $CHARDIR
+fi
+
+LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
+if [[ ! -d $LOCALCHARDIR ]]
 then
 then
-  cat /usr/share/doc/charsel/usage
-  exit 0
-elif [[ $1 == "-d" \
-  || $1 == "--doc" ]]
+  mkdir -p $LOCALCHARDIR
+fi
+
+localdirfiles=($LOCALCHARDIR/*)
+if [[ ! -z "${#localdirfiles[*]}" ]]
 then
 then
-  cat /usr/share/doc/charsel/README.md
-  exit 0
+  cp -rf $LOCALCHARDIR/../* $CHARDIR
 fi
 
 fi
 
-# Define charfile path
-CHARFILE=$CHARDIR/charfiles/$1
+globaldirfiles=($/usr/share/charfiles/)
+if [[ ! -z "${#globaldirfiles[*]}" ]]
+then
+  cp -rf /usr/share/charsel/* $CHARDIR
+fi
 
 
-# Check if given charfile exists
+# Check if charfile exists
+CHARFILE="$CHARDIR/charfiles/$1"
 if [[ ! -f $CHARFILE ]]
 then
 if [[ ! -f $CHARFILE ]]
 then
-  echo "Please enter a valid charfile."
-  charsel -l
-  exit 1
+  invalid_charfile
 fi
 
 # Define length of shortcode
 fi
 
 # Define length of shortcode
@@ -87,15 +137,10 @@ do
 
   if [[ $2 == "--all" ]]
   then
 
   if [[ $2 == "--all" ]]
   then
-    cat $CHARFILE \
-      | sed -e '/^[ \t]*#/d' \
-      | tail -n +2 \
+    cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
       | column -t -N input,output --output-separator ' | ' --separator ','
   else
       | column -t -N input,output --output-separator ' | ' --separator ','
   else
-    cat $CHARFILE \
-      | sed -e '/^[ \t]*#/d' \
-      | grep -A 100 - \
-      | tail -n +2 \
+    cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
       | column -t -N input,output --output-separator ' | ' --separator ','
   fi
 
       | column -t -N input,output --output-separator ' | ' --separator ','
   fi
 
@@ -110,26 +155,19 @@ do
 
   # This can definately be simplifed, but it works fine
   # Finds line number of shortcode
 
   # This can definately be simplifed, but it works fine
   # 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 charachter selected above
-  OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE \
-    | head -$LINENUMBER \
-    | tail +$LINENUMBER)
+  # Outputs character selected above
+  OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
 
   # Check to see if the shortcode actually exists
 
   # 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
   then
     OUTPUT=""
   fi
 
   # Copy output to clipboard
-  echo $OUTPUT \
-    | xclip -selection clipboard
+  echo $OUTPUT | xclip -selection clipboard
 
   clear
 
   clear
-
 done
 done
diff --git a/usage b/usage
deleted file mode 100644 (file)
index acf882b..0000000
--- a/usage
+++ /dev/null
@@ -1,10 +0,0 @@
-Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
-A simple terminal charachter selector
-  -h, --help      show this message
-  -l, --list      show installed charfiles
-  -d, --doc       show readme
-      --all       include hidden shortcodes
-
-Exit status:
-  0 if OK,
-  1 if error