clean up code

This commit is contained in:
Guilherme Rugai Freire 2021-08-02 11:48:33 -03:00
parent 759894c256
commit 1e3ce64d3e

View File

@ -1,34 +1,32 @@
#!/usr/bin/env bash #!/bin/bash
# Simple CLI for shell-color-scripts # Simple CLI for shell-color-scripts
# Check if is integrated terminal emulator # Check if is integrated terminal emulator
if [ -z $INTEG_EMU ]; if [ -z "$INTEG_EMU" ];
then : ; else then : ; else
exit 0 exit 0
fi fi
DIR_COLORSCRIPTS="$HOME/.local/share/shell-color-scripts/colorscripts" DIR_COLORSCRIPTS="$HOME/.local/share/shell-color-scripts/colorscripts"
fmt_help=" %-20s\t%-54s\n" list_colorscripts="$(find "${DIR_COLORSCRIPTS}" -printf '%P\n' | tail -n+2 | sort | nl)"
list_colorscripts="$(ls "${DIR_COLORSCRIPTS}" | cut -d ' ' -f 1 | nl)" length_colorscripts="$(find "${DIR_COLORSCRIPTS}" -printf '%P\n' | tail -n+2 | wc -l)"
length_colorscripts="$(ls "${DIR_COLORSCRIPTS}" | wc -l)" _help() {
function _help() {
echo "Description: A collection of terminal color scripts." echo "Description: A collection of terminal color scripts."
echo "" echo ""
echo "Usage: colorscript [OPTION] [SCRIPT NAME/INDEX]" echo "Usage: colorscript [OPTION] [SCRIPT NAME/INDEX]"
printf "${fmt_help}" \ printf " %-20s\t%-54s\n" \
"-h, --help, help" "Print this help." \ "-h, --help, help" "Print this help." \
"-l, --list, list" "List all installed color scripts." \ "-l, --list, list" "List all installed color scripts." \
"-r, --random, random" "Run a random color script." \ "-r, --random, random" "Run a random color script." \
"-e, --exec, exec" "Run a specified color script by SCRIPT NAME or INDEX." "-e, --exec, exec" "Run a specified color script by SCRIPT NAME or INDEX."
} }
function _list() { _list() {
echo "There are "$(ls "${DIR_COLORSCRIPTS}" | wc -l)" installed color scripts:" echo There are "$length_colorscripts" installed color scripts:
echo "${list_colorscripts}" echo "${list_colorscripts}"
} }
function _random() { _random() {
declare -i random_index=$RANDOM%$length_colorscripts declare -i random_index=$RANDOM%$length_colorscripts
[[ $random_index -eq 0 ]] && random_index=1 [[ $random_index -eq 0 ]] && random_index=1
@ -39,11 +37,11 @@ function _random() {
exec "${DIR_COLORSCRIPTS}/${random_colorscript}" exec "${DIR_COLORSCRIPTS}/${random_colorscript}"
} }
function ifhascolorscipt() { ifhascolorscipt() {
[[ -e "${DIR_COLORSCRIPTS}/$1" ]] && echo "Has this color script." [[ -e "${DIR_COLORSCRIPTS}/$1" ]] && echo "Has this color script."
} }
function _run_by_name() { _run_by_name() {
if [[ "$1" == "random" ]]; then if [[ "$1" == "random" ]]; then
_random _random
elif [[ -n "$(ifhascolorscipt "$1")" ]]; then elif [[ -n "$(ifhascolorscipt "$1")" ]]; then
@ -54,10 +52,10 @@ function _run_by_name() {
fi fi
} }
function _run_by_index() { _run_by_index() {
if [[ "$1" -gt 0 && "$1" -le "${length_colorscripts}" ]]; then if [[ "$1" -gt 0 && "$1" -le "${length_colorscripts}" ]]; then
colorscript="$(echo "${list_colorscripts}" | sed -n ${1}p \ colorscript="$(echo "${list_colorscripts}" | sed -n "${1}"p \
| tr -d ' ' | tr '\t' ' ' | cut -d ' ' -f 2)" | tr -d ' ' | tr '\t' ' ' | cut -d ' ' -f 2)"
exec "${DIR_COLORSCRIPTS}/${colorscript}" exec "${DIR_COLORSCRIPTS}/${colorscript}"
else else
@ -66,7 +64,7 @@ function _run_by_index() {
fi fi
} }
function _run_colorscript() { _run_colorscript() {
if [[ "$1" =~ ^[0-9]+$ ]]; then if [[ "$1" =~ ^[0-9]+$ ]]; then
_run_by_index "$1" _run_by_index "$1"
else else