From: Armaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me> Date: Sun, 18 Oct 2020 02:55:29 +0000 (-0400) Subject: See notes X-Git-Url: https://git.armaanb.net/?p=charsel.git;a=commitdiff_plain;h=fbbee478d81f974898259ba4307c1660fe72758d See notes 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. --- diff --git a/Makefile b/Makefile index d34ff02..d9ffea1 100644 --- 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 usage /usr/share/doc/charsel/ >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 usage /usr/share/doc/charsel/ >cp ./charsel /usr/bin/ diff --git a/README.md b/README.md index 295998d..a356335 100644 --- 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 -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 -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 diff --git a/charsel b/charsel index d2e7525..56e1fdf 100755 --- a/charsel +++ b/charsel @@ -19,53 +19,103 @@ ######################################################################## -# 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 +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 -# Check for user inputs -if [[ $1 == "-l" \ - || $1 == "--list" ]] +# Merge global and local charfiles +if [[ -d $CHARDIR ]] 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 - cat /usr/share/doc/charsel/usage - exit 0 -elif [[ $1 == "-d" \ - || $1 == "--doc" ]] + mkdir -p $LOCALCHARDIR +fi + +localdirfiles=($LOCALCHARDIR/*) +if [[ ! -z "${#localdirfiles[*]}" ]] then - cat /usr/share/doc/charsel/README.md - exit 0 + cp -rf $LOCALCHARDIR/../* $CHARDIR 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 - echo "Please enter a valid charfile." - charsel -l - exit 1 + invalid_charfile fi # Define length of shortcode @@ -87,15 +137,10 @@ do 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 - 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 @@ -110,26 +155,19 @@ do # 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 - 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 $OUTPUT \ - | xclip -selection clipboard + echo $OUTPUT | xclip -selection clipboard clear - done diff --git a/usage b/usage deleted file mode 100644 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