From 34f8ca8c05b6a5d17f39e307ef7b513dff64b7c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= <100824266+OurCodeBase@users.noreply.github.com> Date: Thu, 25 Jan 2024 02:28:23 +0530 Subject: [PATCH 01/18] added release versions to readme.md --- docs/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/README.md b/docs/README.md index 2d88df7..f92b19e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,7 @@ A bash library to create standalone scripts. +[![GitHub release](https://img.shields.io/github/v/release/OurCodeBase/bash-sdk)](https://github.com/OurCodeBase/bash-sdk/releases) + There are some features of bash-sdk are mentioned here. * OOPS like code 💎. * Module based code 🗂️. From 61941d4d5184b348b902eb962a4fe01b57dab43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= <100824266+OurCodeBase@users.noreply.github.com> Date: Thu, 25 Jan 2024 02:33:00 +0530 Subject: [PATCH 02/18] converted release from advance to simple --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index f92b19e..b8ecc1e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ A bash library to create standalone scripts. -[![GitHub release](https://img.shields.io/github/v/release/OurCodeBase/bash-sdk)](https://github.com/OurCodeBase/bash-sdk/releases) +Release: 1.2.56 There are some features of bash-sdk are mentioned here. * OOPS like code 💎. From 45e8578d5ef2be6f16a4eac7c5e365cc13142c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= <100824266+OurCodeBase@users.noreply.github.com> Date: Thu, 25 Jan 2024 02:34:40 +0530 Subject: [PATCH 03/18] release updated --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index b8ecc1e..0ba407d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ A bash library to create standalone scripts. -Release: 1.2.56 +Release: v1.2.57 There are some features of bash-sdk are mentioned here. * OOPS like code 💎. From 2d598da654e510d4a0be7e67fa801346da9c63fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sat, 27 Jan 2024 10:14:26 +0530 Subject: [PATCH 04/18] Code optimised --- src/ask.sh | 11 +++++------ src/package.sh | 8 ++------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/ask.sh b/src/ask.sh index 24a0c74..a5a5a65 100644 --- a/src/ask.sh +++ b/src/ask.sh @@ -27,9 +27,9 @@ ask.case(){ read -p " ? [Y/n] " ARGS; echo; case "${ARGS}" in - y|Y) return 0;; + y|Y|'') return 0;; n|N) { say.error "Process Aborted.\n" && exit 1; };; - *) { say.error "You have to enter only\n\t\t'Y' for Yes &\n\t\t'n' for No.\n" && exit 1; };; + *) { say.error "You have to enter only \n\t\t'Y' for Yes & \n\t\t'n' for No.\n" && exit 1; };; esac } @@ -43,16 +43,15 @@ ask.case(){ ask.choice(){ PS3=" ${1} > "; + shift; local ARGs=("${@}"); - # leave first variable of array for title. - local ARGs2=("${ARGs[@]:1}"); echo; - select ARG in "${ARGs2[@]}" + select ARG in "${ARGs[@]}" do text.isdigit "${REPLY}" || { say.error "You can only input 'digits'.\n" && exit 1; }; - [[ "${REPLY}" -gt "${#ARGs2[@]}" ]] && + [[ "${REPLY}" -gt "${#ARGs[@]}" ]] && say.error "You should input correct digits.\n" && exit 1; askChoice="${ARG}"; askReply="${REPLY}"; diff --git a/src/package.sh b/src/package.sh index f8bcf5c..3af0839 100644 --- a/src/package.sh +++ b/src/package.sh @@ -45,9 +45,7 @@ pkg.size(){ 'MB') echo "${Size}";; 'kB') echo "$(( Size/1024 ))";; 'B') echo "$(( Size/1048576 ))";; - esac - # return function. - return; + esac } # pkg.chart(pkgs) @@ -114,9 +112,7 @@ pkg.install(){ do spinner.start 'Installing' "${ARG}"; # started installation. - if os.is_termux; then - apt-get install -qq "${ARG}" > /dev/null; - elif os.is_shell.zsh; then + if os.is_termux || os.is_shell.zsh; then apt-get install -qq "${ARG}" > /dev/null; else sudo apt-get install -qq "${ARG}" > /dev/null; From 3a5e6a1ee60cfc65894fd12cee8fbbddd3fc0ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= <100824266+OurCodeBase@users.noreply.github.com> Date: Sat, 27 Jan 2024 10:20:23 +0530 Subject: [PATCH 05/18] updated release version --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 0ba407d..80e25a2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ A bash library to create standalone scripts. -Release: v1.2.57 +Release: v1.2.58 There are some features of bash-sdk are mentioned here. * OOPS like code 💎. From d7ae166e9953a6704aaf76d628b74cf543e8f853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sat, 3 Feb 2024 12:37:38 +0530 Subject: [PATCH 06/18] updated and optimised say module. --- src/say.sh | 159 +++++++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 72 deletions(-) diff --git a/src/say.sh b/src/say.sh index 7f881ff..929b94d 100644 --- a/src/say.sh +++ b/src/say.sh @@ -3,114 +3,129 @@ Clear="\033[0m"; # Suppressed Variables if (( 1<2 )); then -# Colors -Gora="\033[1;97m" +# ---------- +# Colors. +# Gora="\033[0;97m" +# ---------- # BG Colors. -BGRed="\033[1;41m"; -BGGreen="\033[1;42m"; -BGYelo="\033[1;43m"; +# BGRed="\033[1;41m"; +# BGGreen="\033[1;42m"; +# BGYelo="\033[1;43m"; +# BGBold="\033[1m"; +# ---------- # Status Colors. -StatusRed="${BGRed}${Gora}"; -StatusGreen="${BGGreen}${Gora}"; -StatusYelo="${BGYelo}${Gora}"; +# StatusRed="${BGRed}${Gora}"; +# StatusGreen="${BGGreen}${Gora}"; +# StatusYelo="${BGYelo}${Gora}"; +# ---------- # Global variables. -Source="'([^']*)'"; +Pattern="'([^']*)'"; +# Color plate. +COLOR_STRIP=( + "\033[0;31m" # Red + "\033[0;32m" # Green + "\033[0;33m" # Yelo + "\033[0;34m" # Blue + "\033[0;35m" # Genta + "\033[0;36m" # Cyan +); fi -# Colors -Red="\033[1;31m"; -Green="\033[1;32m"; -Yelo="\033[1;33m"; - -# say.gencolor() -# Gives you a random color code. +# say.gencolor() ~ color +# Generates color codes for you. say.gencolor(){ - local STRIP=( - "\033[1;31m" - "\033[1;32m" - "\033[1;33m" - "\033[1;34m" - "\033[1;35m" - "\033[1;36m" - ); - echo "${STRIP[$(( RANDOM % ${#STRIP[@]} ))]}"; - return 0; + echo "${COLOR_STRIP[$(( RANDOM % ${#COLOR_STRIP[@]} ))]}"; } -# say(str) -> str -# Say strings with syntax highlighting feature. -say(){ - local String="${1}"; - local STRIP=(${String}); - local String=''; - for Uri in "${STRIP[@]}" +# _say.colorizeString(str,@--gencolor,@--color ) ~ str +# Create and returns you raw colorized strings. +# +# ARGS: +# - str (str): takes string as arg. +# - --gencolor (obj,optional): adds colors randomly to syntax. +# - --color (str,optional): adds given color to syntax. +_say.colorizeString(){ + local ARGString="${1}"; + local isGenColorEnabled=1; + local Sentence=(${ARGString}); + local String; + shift; + case "${1}" in + --gencolor) + local isGenColorEnabled=0; + ;; + --color) + local Color="${2}"; + ;; + esac + for Shabd in "${Sentence[@]}" do - if [[ "${Uri}" =~ ${Source} ]]; then + if [[ "${Shabd}" =~ ${Pattern} ]]; then + [[ "${isGenColorEnabled}" == 1 ]] || \ local Color="$(say.gencolor)"; - local String+="${Color}${Uri}${Clear} "; - else local String+="${Uri} "; + local String+="${Color}${Shabd}${Clear} "; + else + local String+="${Shabd} "; fi done + echo "${String}"; +} + +# say(str) ~ str +# Say strings with syntax highlighting feature. +say(){ + local String="$(_say.colorizeString "${1}" --gencolor)"; echo -e "${String}"; - return 0; } _status(){ case "${1}" in -error) - local Color="\033[1;31m"; - local StatusColor="${StatusRed}"; - ;; - -success) - local Color="\033[1;32m"; - local StatusColor="${StatusGreen}"; - ;; + local Color="\033[0;31m"; + local Header="ERR"; + ;; -warn) - local Color="\033[1;33m"; - local StatusColor="${StatusYelo}"; - ;; + local Color="\033[0;33m"; + local Header="WARN"; + ;; + -success) + local Color="\033[0;32m"; + local Header="INFO"; + ;; esac - local String="${2}"; - local xSTRIP=(${String}); - local String=''; - for Uri in "${xSTRIP[@]}" - do - if [[ "${Uri}" =~ ${Source} ]]; then - local String+="${Color}${Uri}${Clear} "; - else local String+="${Uri} "; - fi - done - echo -e "${StatusColor} INFO ${Clear} ➙ ${String}"; - return 0; + local String="$(_say.colorizeString "${2}" --color "${Color}")"; + echo -ne "[${Color} ${Header} ${Clear}]: "; + echo -e "${String}"; } -# say.error(str) -> str -# Says error to terminal. +# say.error(str) ~ str +# Says error to terminal. say.error(){ _status -error "${@}"; } -# say.warn(str) -> str -# Says warning to terminal. +# say.warn(str) ~ str +# Says warning to terminal. say.warn(){ _status -warn "${@}"; } -# say.success(str) -> str -# Says success to terminal. +# say.success(str) ~ str +# Says success to terminal. say.success(){ _status -success "${@}"; } -# say.checkStatus(status) -> str -# This prints success or failure according to exit code. -# Args: -# status (int) > takes exit code. +# say.checkStatus(exitCode) ~ str +# This prints success or failure according to exit code. +# +# ARGS: +# - exitCode (int): takes exit code. say.checkStatus(){ if [[ "${1}" == 0 ]]; then - echo -e " ➙ ${StatusGreen} SUCCESS ${Clear}"; + echo -e " ~ [${COLOR_STRIP[1]} SUCCESS ${Clear}]"; else - echo -e " ➙ ${StatusRed} FAILED ${Clear}"; + echo -e " ~ [${COLOR_STRIP[0]} FAILED ${Clear}]"; exit 1; fi -} +} \ No newline at end of file From 6fbe225b6f6ab93238c90572e74c8b3f145779de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sat, 3 Feb 2024 14:18:33 +0530 Subject: [PATCH 07/18] updated and more readable code and docs in ask module and updated cursor module docs --- src/ask.sh | 67 +++++++++++++++++++++++++++++++-------------------- src/cursor.sh | 2 +- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/ask.sh b/src/ask.sh index a5a5a65..9315a19 100644 --- a/src/ask.sh +++ b/src/ask.sh @@ -14,47 +14,62 @@ source "${Dir}"/string.sh # source string.sh # Stores object that you selected. -askChoice=''; +askObject=''; # Stores position of object that you selected. -askReply=''; +askPosition=''; -# ask.case(title) -> bool -# This takes case ( yes or no ) for any work. -# Args: -# title (str) > takes title (eg: You're Agree). +# ask.case(title) ~ bool +# This takes case ( yes or no ) for any work. +# +# ARGS: +# - title (str): takes title (eg: You're Agree). ask.case(){ echo -ne "\n ${1}"; read -p " ? [Y/n] " ARGS; echo; case "${ARGS}" in - y|Y|'') return 0;; - n|N) { say.error "Process Aborted.\n" && exit 1; };; - *) { say.error "You have to enter only \n\t\t'Y' for Yes & \n\t\t'n' for No.\n" && exit 1; };; + y|Y|'') + return 0; + ;; + n|N) + say.error "Process Aborted.\n"; + exit 1; + ;; + *) say.error "You have to enter only \n + \t\t 'Y' for Yes & \n + \t\t 'n' for No.\n"; + exit 1; + ;; esac } -# ask.choice(title,list) -> var -# This creates a simple menu. -# Args: -# title (str) > takes title (eg: Choose One). -# list (array) > takes array as arg. -# Returns: -# object (str) > result in 'askChoice' & 'askReply' variables. +# ask.choice(title,list) ~ var +# This creates a simple menu. +# +# ARGS: +# - title (str): takes title (eg: Choose One). +# - list (array): takes array as arg. +# +# RETURNS: +# - var (str): result in 'askObject' & 'askPosition' variables. ask.choice(){ PS3=" - ${1} > "; + ${1}: "; shift; local ARGs=("${@}"); echo; select ARG in "${ARGs[@]}" do - text.isdigit "${REPLY}" || { - say.error "You can only input 'digits'.\n" && exit 1; - }; - [[ "${REPLY}" -gt "${#ARGs[@]}" ]] && - say.error "You should input correct digits.\n" && exit 1; - askChoice="${ARG}"; - askReply="${REPLY}"; - break; + if text.isdigit "${REPLY}"; then + say.error "You can only input 'digits'.\n"; + exit 1; + elif [[ "${REPLY}" -gt "${#ARGs[@]}" ]]; then + say.error "You should input correct digits.\n"; + exit 1; + else + askObject="${ARG}"; + askPosition="${REPLY}"; + break; + fi done -} \ No newline at end of file +} diff --git a/src/cursor.sh b/src/cursor.sh index 59e8bb9..c147d12 100644 --- a/src/cursor.sh +++ b/src/cursor.sh @@ -1,7 +1,7 @@ #!/bin/bash # setCursor(on~off) -# Switch terminal cursor easily. +# Switch terminal cursor easily. setCursor(){ setterm -cursor "${1}"; } \ No newline at end of file From 4def0ea4def7fc0808f48346053c3bc47e7fc514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sat, 3 Feb 2024 22:26:37 +0530 Subject: [PATCH 08/18] updated docs and code in db module, update minor docs in say module. --- src/db.sh | 94 ++++++++++++++++++++++++------------------------------ src/say.sh | 2 +- 2 files changed, 42 insertions(+), 54 deletions(-) diff --git a/src/db.sh b/src/db.sh index 0d0ea4e..221a74b 100644 --- a/src/db.sh +++ b/src/db.sh @@ -1,95 +1,83 @@ #!/bin/bash -# Support only Yaml syntax database. +# Supports only yaml syntax database. -# _db.isfile(file) -> bool -# Checks that db file exist or not. -# Args: -# file (str) > takes file path. -_db.isfile(){ - # checking file exist or not. - test -f "${1}"; -} - -# _db.iskey(key,file) -> bool -# Checks that key is exist or not. -# Args: -# key (str) > takes key of db. -# file (str) > takes file path. -_db.iskey(){ +# _db.isKeyExist(key,file) ~ bool +# Checks that key is exist or not. +# +# ARGS: +# - key (str): takes key of db. +# - file (str): takes file path. +_db.isKeyExist(){ grep "${1}: " "${2}" &>/dev/null; } -# db.read(key,file) -> str -# Give you data of key of yaml db. -# Args: -# key (str) > takes key of db. -# file (str) > takes file path. +# db.read(key,file) ~ str +# Reads value of given key in db file. +# +# ARGS: +# - key (str): takes key of db. +# - file (str): takes file path. db.read(){ # checking args given or not. [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; - _db.iskey "${1}" "${2}" || + _db.isKeyExist "${1}" "${2}" || { echo "error: ${1}: 'key not exists'" && return 1; }; local dbValue="$(grep "${1}: " "${2}")"; echo "${dbValue}" | awk -F': ' '{sub(/^[^:]*: /, ""); print}'; } # db.create(key,value,file) -# Adds data key and value to db. -# Args: -# key (str) > takes key of db. -# value (str) > takes value of key. -# file (str) > takes file path. +# Creates data key and value to db. +# +# ARGS: +# - key (str): takes key of db. +# - value (str): takes value of key. +# - file (str): takes file path. db.create(){ # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - _db.iskey "${1}" "${3}" && + _db.isKeyExist "${1}" "${3}" && { echo "error: ${1}: 'key already exists'" && return 1; }; echo -ne "\n${1}: ${2}" >> "${3}"; } # db.update(key,value,file) -# Update data of key in db file. -# Args: -# key (str) > takes key of db. -# value (str) > takes update value of key. -# file (str) > takes file path. +# Update data of key in db file. +# +# ARGS: +# - key (str): takes key of db. +# - value (str): takes update value of key. +# - file (str): takes file path. db.update(){ # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - _db.iskey "${1}" "${3}" || + _db.isKeyExist "${1}" "${3}" || { echo "error: ${1}: 'key not exists'" && return 1; }; - # taking key arg. local dbKey="${1}"; - # taking update value of given key. - local dbUpdatedValue="${2}"; - # taking db file path. + local dbUpdateValue="${2}"; local dbFile="${3}"; - # getting old value of given key. - local dbValue="$(db.read "${dbKey}" "${dbFile}")"; - # concating key and old value. - local dbKeyValuePair="${dbKey}: ${dbValue}"; - # getting position of concated line in file. - local dbKeyValuePairPos="$(grep -n "${dbKeyValuePair}" "${dbFile}" | cut -d: -f1 | head -n 1;)"; - # replacing value from old to new. - local dbUpdatedKeyValuePair="${dbKeyValuePair/${dbValue}/"${dbUpdatedValue}"}"; - # placing updated value to position. - sed -i "${dbKeyValuePairPos}c\\${dbUpdatedKeyValuePair}" "${dbFile}"; + local dbCurrentValue="$(db.read "${dbKey}" "${dbFile}")"; + local dbCurrentPair="${dbKey}: ${dbCurrentValue}"; # concated to create pair. + local dbCurrentPairPos="$(grep -n "${dbCurrentPair}" "${dbFile}" | cut -d: -f1 | head -n 1;)"; + local dbUpdatedPair="${dbCurrentPair/${dbCurrentValue}/"${dbUpdateValue}"}"; + sed -i "${dbCurrentPairPos}c\\${dbUpdatedPair}" "${dbFile}"; # replaced full pair. } # db.delete(key,file) -# Delete key and value of db file. -# Args: -# key (str) > takes key of db. -# file (str) > takes file path. +# Delete key and value of db file. +# +# ARGS: +# - key (str): takes key of db. +# - file (str): takes file path. db.delete(){ # checking args given or not. [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; - _db.iskey "${1}" "${2}" || + _db.isKeyExist "${1}" "${2}" || { echo "error: ${1}: 'key not exists'" && return 1; }; local dbKey="${1}: "; local dbFile="${2}"; diff --git a/src/say.sh b/src/say.sh index 929b94d..dc70bcd 100644 --- a/src/say.sh +++ b/src/say.sh @@ -43,7 +43,7 @@ say.gencolor(){ # ARGS: # - str (str): takes string as arg. # - --gencolor (obj,optional): adds colors randomly to syntax. -# - --color (str,optional): adds given color to syntax. +# - --color (obj,str,optional): adds given color to syntax. _say.colorizeString(){ local ARGString="${1}"; local isGenColorEnabled=1; From d85216fa2fd43599c347dc7a04ff6559f041dfeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sun, 4 Feb 2024 08:58:20 +0530 Subject: [PATCH 09/18] docs updated in file module --- src/file.sh | 191 ++++++++++++++++++++++++++-------------------------- 1 file changed, 95 insertions(+), 96 deletions(-) diff --git a/src/file.sh b/src/file.sh index 414b1c6..2ba1448 100644 --- a/src/file.sh +++ b/src/file.sh @@ -1,84 +1,93 @@ #!/bin/bash -# path.isdir(dir) -> bool -# Checks that directory exist or not. -# Args: -# dir (str) > takes directory path. +# path.isdir(dir) ~ bool +# Checks that directory exist or not. +# +# ARGS: +# - dir (str): takes directory path. path.isdir(){ # checking dir exist or not. test -d "${1}"; } -# path.isfile(file) -> bool -# Checks that file exist or not. -# Args: -# file (str) > takes file path. +# path.isfile(file) ~ bool +# Checks that file exist or not. +# +# ARGS: +# - file (str): takes file path. path.isfile(){ # checking file exist or not. test -f "${1}"; } # file.move(src,dst) -# This can use to move files. -# Args: -# src (str) > takes source path. -# dst (str) > takes destination path. +# This can use to move files. +# +# ARGS: +# - src (str): takes source path. +# - dst (str): takes destination path. file.move(){ # moving file to destination. mv -f "${1}" "${2}" 2>/dev/null; } # file.copy(src,dst) -# This can use to copy files. -# Args: -# src (str) > takes source path. -# dst (str) > takes destination path. +# This can use to copy files. +# +# ARGS: +# - src (str): takes source path. +# - dst (str): takes destination path. file.copy(){ # copying files to destination. cp -rf "${1}" "${2}" 2>/dev/null; } # file.erase(file) -# Args: -# file (str) > takes file path. +# +# ARGS: +# - file (str): takes file path. file.erase(){ # cleaning file. truncate -s 0 "${1}"; } # file.pop(pos,file) -# Popout given position of line in file. -# Args: -# pos (int) > takes position. -# file (str) > takes file path. +# Popout given position of line in file. +# +# ARGS: +# - pos (int): takes position. +# - file (str): takes file path. file.pop(){ # return final result. sed -i "${1}d" "${2}"; } -# file.readlines.int(file) -> int -# Gives you total lines of a file. -# Args: -# file (str) > takes file path. +# file.readlines.int(file) ~ int +# Gives you total lines of a file. +# +# ARGS: +# - file (str): takes file path. file.readlines.int(){ # return final result. wc -l "${1}" | awk '{print $1}'; } -# file.readline(pos,file) -> str -# Gives you line from given position of file. -# Args: -# pos (int) > takes position. -# file (str) > takes file path. +# file.readline(pos,file) ~ str +# Gives you line from given position of file. +# +# ARGS: +# - pos (int): takes position. +# - file (str): takes file path. file.readline(){ # return final result. sed -n "${1}p" "${2}"; } -# file.readlines(file) -> STRIP. -# Gives you STRIP array of lines of given file. -# Args: -# file (str) > takes file path. +# file.readlines(file) ~ STRIP. +# Gives you STRIP array of lines of given file. +# +# ARGS: +# - file (str): takes file path. file.readlines(){ # taking file arg from user. local ARGFile="${1}"; @@ -96,56 +105,59 @@ file.readlines(){ return; } -# file.readline.tall(file) -> str -# Gives you largest line of file. -# Args: -# file (str) > takes file path. +# file.readline.tall(file) ~ str +# Gives you largest line of file. +# +# ARGS: +# - file (str): takes file path. file.readline.tall(){ # return final result. awk 'length > max_length { max_length = length; max_line = $0 } END { print max_line }' "${1}"; } # file.replace.pos(str,pos,file) -# This replace text from given line of file. -# Args: -# str (str) > takes string to replace. -# pos (int) > takes position. -# file (str) > takes file path. +# This replace text from given line of file. +# +# ARGS: +# - str (str): takes string to replace. +# - pos (int): takes position. +# - file (str): takes file path. file.replace.pos(){ # return final result. sed -i "${2}c\\${1}" "${3}"; } -# file.search(str,file) -> str -# Search given text in file & return you that line. -# Args: -# str (str) > takes string to search. -# file (str) > takes file path. +# file.search(str,file) ~ str +# Search given text in file & return you that line. +# +# ARGS: +# - str (str): takes string to search. +# - file (str): takes file path. file.search(){ # return final result. grep "${1}" "${2}"; } -# file.search.pos(str,file) -> pos -# Search given text in file & return you position (eg: 1,2). -# Args: -# str (str) > takes string to search. -# file (str) > takes file path. +# file.search.pos(str,file) ~ pos +# Search given text in file & return you position (eg: 1,2). +# +# ARGS: +# - str (str): takes string to search. +# - file (str): takes file path. file.search.pos(){ # return final result. grep -n "${1}" "${2}" | cut -d: -f1 | head -n 1; } -# path.dirArray(dir,*options) -> STRIP. -# Gives you array of files in given directory. -# Args: -# dir (str) > takes directory path. -# --by-time (obj) > Optional: list will be sorted by time. -# --no-extension (obj) > Optional: list have no file extensions. +# path.dirArray(dir,@--by-time,@--no-extension) ~ STRIP. +# Gives you array of files in given directory. +# +# ARGS: +# - dir (str): takes directory path. +# - --by-time (obj,optional): list will be sorted by time. +# - --no-extension (obj,optional): list have no file extensions. path.dirArray(){ - # taking directory arg from user. local ARGDir=${1} && shift; - # array list file location. local UriFile=".Uri"; # check directory exist. path.isdir "${ARGDir}" && { @@ -170,51 +182,38 @@ path.dirArray(){ } # file.append.hori(str,pos,file) -# This appends text to file horizontally. -# Args: -# str (str) > takes string to append it. -# pos (int) > takes position. -# file (str) > takes file path. +# This appends text to file horizontally. +# +# ARGS: +# - str (str): takes string to append it. +# - pos (int): takes position. +# - file (str): takes file path. file.append.hori(){ - # takes string as arg. - local String="${1}"; - # takes position as arg. - local Pos="${2}"; - # takes file as arg. - local ARGFile="${3}"; # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - # reading line from given position. - local Vir="$(file.readline "${Pos}" "${ARGFile}")"; - # concating string to it. - local Vir+="${String}"; - # replacing the line from file. - file.replace.pos "${Vir}" "${Pos}" "${ARGFile}"; - # return function. - return; + local String="${1}"; + local Pos="${2}"; + local ARGFile="${3}"; + local ConsVar="$(file.readline "${Pos}" "${ARGFile}")"; + local ConsVar+="${String}"; # concat + file.replace.pos "${ConsVar}" "${Pos}" "${ARGFile}"; } # file.append.vert(str,pos,file) -# This appends text to file vertically. -# Args: -# str (str) > takes string to append it. -# pos (int) > takes position. -# file (str) > takes file path. +# This appends text to file vertically. +# +# ARGS: +# - str (str): takes string to append it. +# - pos (int): takes position. +# - file (str): takes file path. file.append.vert(){ - # takes text as arg. - local String="${1}"; - # takes position as arg & have to reduce one. - local Pos="${2}"; - # takes file as arg. - local ARGFile="${3}"; # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - # reduce one from variable. + local String="${1}"; + local Pos="${2}"; # needs to reduce one. + local ARGFile="${3}"; local Pos="$(( Pos - 1 ))"; - # do operation. sed -i "${Pos}r /dev/stdin" "${ARGFile}" <<< "${String}"; - # return function. - return; } From b2816c194967ccb879aaa6caaa4a7d7f5215e783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sun, 4 Feb 2024 09:18:50 +0530 Subject: [PATCH 10/18] docs updated in inspect, os, and package module --- src/inspect.sh | 15 ++++++++------- src/os.sh | 25 +++++++++++++------------ src/package.sh | 42 +++++++++++++++++++++--------------------- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/inspect.sh b/src/inspect.sh index d31fccb..3107337 100644 --- a/src/inspect.sh +++ b/src/inspect.sh @@ -13,11 +13,12 @@ source "${Dir}"/os.sh # source say.sh # source os.sh -# inspect.ScreenSize(cols,lines) -> str -# Checks that screen size is sufficient to project. -# Args: -# cols (int) > takes columns as arg. -# lines (int) > takes lines as arg. +# inspect.ScreenSize(cols,lines) ~ str +# Checks that screen size is sufficient to project. +# +# ARGS: +# - cols (int): takes columns as arg. +# - lines (int): takes lines as arg. inspect.ScreenSize(){ local ARGCols="${1}"; local ARGRou="${2}"; @@ -34,8 +35,8 @@ inspect.ScreenSize(){ }; } -# inspect.is_func(function) -> str -# An extension of os.is_func. +# inspect.is_func(function) ~ str +# An extension of os.is_func. inspect.is_func(){ os.is_func "${1}" || { say.error "There is no '${1}'\n diff --git a/src/os.sh b/src/os.sh index 8d745ef..9c217ec 100644 --- a/src/os.sh +++ b/src/os.sh @@ -1,25 +1,25 @@ #!/bin/bash -# os.is_userland() -> bool -# OS is userland or not ? +# os.is_userland() ~ bool +# OS is userland or not ? os.is_userland(){ ls '/host-rootfs/data/data/tech.ula/files/home' &> /dev/null; } -# os.is_termux() -> bool -# OS is termux or not ? +# os.is_termux() ~ bool +# OS is termux or not ? os.is_termux(){ ls '/data/data/com.termux/files/' &> /dev/null; } -# os.is_windows() -> bool -# OS is windows or not ? +# os.is_windows() ~ bool +# OS is windows or not ? os.is_windows(){ [[ "$(uname -a)" == *"windows"* ]] } -# os.is_shell.zsh() -> bool -# Using z-shell or not ? +# os.is_shell.zsh() ~ bool +# Using z-shell or not ? os.is_shell.zsh(){ if os.is_userland; then [[ "$(OurCodeBase-CShell)" == "zsh: "* ]]; @@ -28,10 +28,11 @@ os.is_shell.zsh(){ fi } -# os.is_func(function) -> bool -# Checks that function is exist or not. -# Args: -# function (str) > takes function as string. +# os.is_func(function) ~ bool +# Checks that function is exist or not. +# +# ARGS: +# - function (str): takes function as string. os.is_func(){ command -v "${1}" &> /dev/null; } diff --git a/src/package.sh b/src/package.sh index 3af0839..01bf92f 100644 --- a/src/package.sh +++ b/src/package.sh @@ -17,24 +17,22 @@ source "${Dir}"/os.sh # source os.sh # source ask.sh -# pkg.size(dnload~install,package) -> int -# Gives you needed size of package. -# Args: -# dnload (str) > dnload to get file size. -# install (str) > install to get package installed size. -# package (str) > takes package (eg: python,nodejs). -# Returns: -# size (int) > Gives you size in MiBs. -# Usage: -# pkg.size(dnload,package) > Gives you package file size. -# pkg.size(install,package) > Gives you package installed size. +# pkg.size(@--dnload ,@--install ) ~ int +# Gives you needed size of package. +# +# ARGS: +# - --dnload (obj,str,optional): To get package file size. +# - --install (obj,str,optional): To get package installed size. +# +# RETURNS: +# - size (int): Gives you size in MiBs. pkg.size(){ # checking args given or not. [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; case "${1}" in - 'dnload') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Download-Size:')";; - 'install') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Installed-Size:')";; + --dnload) local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Download-Size:')";; + --install) local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Installed-Size:')";; esac local Size="$(echo "${SizeSource}" | awk '{print $2}')"; local SizeUnit="$(echo "${SizeSource}" | awk '{print $3}')"; @@ -49,9 +47,10 @@ pkg.size(){ } # pkg.chart(pkgs) -# Use to view chart of packages. -# Args: -# pkgs (array) > takes array of packages. +# Use to view chart of packages. +# +# ARGS: +# - pkgs (array): takes array of packages. pkg.chart(){ inspect.ScreenSize '62' '12'; # this takes all packages as arg. @@ -76,9 +75,9 @@ pkg.chart(){ # declare package version variable. local PackageVersion="$(echo "${PackageSource}" | grep 'Version:' | awk '{print $2}' | awk -F'-' '{print $1}' | awk -F'+' '{print $1}' | awk -F'~' '{print $1}')"; # declare package file size variable. - local PackageSizeDL="$(pkg.size 'dnload' "${ARG}")"; + local PackageSizeDL="$(pkg.size --dnload "${ARG}")"; # declare package installed size variable. - local PackageSizeIN="$(pkg.size 'install' "${ARG}")"; + local PackageSizeIN="$(pkg.size --install "${ARG}")"; printf " │ ${Green}%-25s${Clear} ${Yelo}%-10s${Clear} ${Yelo}%3s${Clear} %-3s ${Yelo}%3s${Clear} %-3s │\n" "${PackageVar}" "${PackageVersion}" "${PackageSizeDL}" 'MiB' "${PackageSizeIN}" 'MiB'; # Adding dl sizes of all packages. local PuraSizeDL=$(( PuraSizeDL + PackageSizeDL )); @@ -98,9 +97,10 @@ pkg.chart(){ } # pkg.install(packages) -# Used to install packages with good ui. -# Args: -# packages (array) > takes packages as args. (eg: python nodejs neovim) +# Used to install packages with good ui. +# +# ARGS: +# - packages (array): takes packages as args. (eg: python nodejs) pkg.install(){ # function starts here. local ARGs=("${@}"); From a7a302676a7966d701bab119bbe35f6c1bca5ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sun, 4 Feb 2024 11:17:47 +0530 Subject: [PATCH 11/18] repo, screen and spinner modules updated & spinner ui degrade to increase performance --- src/repo.sh | 32 ++++++++++++++++++-------------- src/screen.sh | 19 ++++++++++--------- src/spinner.sh | 46 +++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/repo.sh b/src/repo.sh index 5fe4300..27ee62a 100644 --- a/src/repo.sh +++ b/src/repo.sh @@ -15,12 +15,14 @@ source "${Dir}"/ask.sh # source inspect.sh # source ask.sh -# repo.size(api) -> int -# Used to get size of a repo. -# Args: -# api (str) > takes api of github repo. (eg: "OurCodeBase/bash-sdk") -# Returns: -# size (int) > gives you file size in MiB. (eg: 30) +# repo.size(api) ~ int +# Used to get size of a repo. +# +# ARGS: +# - api (str): takes api of github repo. (eg: "OurCodeBase/bash-sdk") +# +# RETURNS: +# - size (int): gives you file size in MiB. (eg: 30) repo.size(){ inspect.is_func 'curl'; local Api="$(echo "${1}" | awk '{print $1}')"; @@ -29,9 +31,10 @@ repo.size(){ } # repo.chart(apis) -# Used to view info of given repositories. -# Args: -# apis (array) > takes array of repository api. +# Used to view info of given repositories. +# +# ARGS: +# - apis (array): takes array of repository api. repo.chart(){ inspect.ScreenSize '50' '12'; local ARGs=("${@}"); @@ -58,11 +61,12 @@ repo.chart(){ return 0; } -# repo.clone(apis,*dirs) -# Used to start cloning of a repository. -# Args: -# apis (array) > takes apis of github repo. (eg: OurCodeBase/bash-sdk) -# dirs (array) > Optional: You can give directory path to clone to it. +# repo.clone(apis,@dirs) +# Used to start cloning of a repository. +# +# ARGS: +# - apis (array): takes apis of github repo. (eg: OurCodeBase/bash-sdk) +# - dirs (array,optional): You can give directory path to clone to it. repo.clone(){ # required to run. inspect.is_func 'git'; diff --git a/src/screen.sh b/src/screen.sh index 9ba5595..9eb67d1 100644 --- a/src/screen.sh +++ b/src/screen.sh @@ -1,24 +1,25 @@ #!/bin/bash -# screen.cols() -> int -# Gives you current columns count in terminal. +# screen.cols() ~ int +# Gives you current columns count in terminal. screen.cols(){ # current columns in terminal. stty size | awk '{print $2}' } -# screen.lines() -> int -# Gives you current lines count in terminal. +# screen.lines() ~ int +# Gives you current lines count in terminal. screen.lines(){ # current lines in terminal. stty size | awk '{print $1}' } -# screen.isSize(cols,lines) -> bool -# Checks that screen has atleast given lines and columns. -# Args: -# cols (int) > takes columns as int. -# lines (int) > takes lines as int. +# screen.isSize(cols,lines) ~ bool +# Checks that screen has atleast given lines and columns. +# +# ARGS: +# - cols (int): takes columns as int. +# - lines (int): takes lines as int. screen.isSize(){ local ARGCols=${1}; local ARGLines=${2}; diff --git a/src/spinner.sh b/src/spinner.sh index e7eec1d..5c71774 100644 --- a/src/spinner.sh +++ b/src/spinner.sh @@ -1,9 +1,9 @@ #!/bin/bash # Shorts: -# spinner.start(use,subject) -# run your functions. -# spinner.stop +# - spinner.start(use,subject) +# - run your functions. +# - spinner.stop # Suppressed Variables if (( 1<2 )); then @@ -14,15 +14,7 @@ Genta="\033[1;35m"; Green="\033[1;32m"; Red="\033[1;31m"; Yelo="\033[1;33m"; -Gora="\033[1;97m" -# BG Colors. -BGRed="\033[1;41m"; -BGGreen="\033[1;42m"; -BGYelo="\033[1;43m"; -# Status Colors. -StatusRed="${BGRed}${Gora}"; -StatusGreen="${BGGreen}${Gora}"; -StatusYelo="${BGYelo}${Gora}"; +# Global variables. CircleIcon="● "; Success="SUCCESS"; Failure="FAILED"; @@ -36,7 +28,7 @@ Scribe=( fi # spinner.setCursor(on~off) -# Switch terminal cursor easily. +# Switch terminal cursor easily. spinner.setCursor(){ setterm -cursor "${1}"; } @@ -63,23 +55,27 @@ _spinner(){ exit 1; }; kill ${3} > /dev/null 2>&1 - echo -en "\b${Clear} ➙ "; - [[ $2 -eq 0 ]] && - echo -e "${StatusGreen} ${Success} ${Clear}" || { - echo -e "${StatusRed} ${Failure} ${Clear}" && + echo -en "\b${Clear} → "; + if [[ $2 -eq 0 ]]; then + echo -e "[ ${Green}${Success}${Clear} ]"; + else + echo -e "[ ${Red}${Failure}${Clear} ]"; + spinner.setCursor on; exit 1; - }; + fi ;; esac } # spinner.start(use,subject) -# Starts spinner to spin. -# Args: -# use (str) > takes process (eg: installing, processing). -# subject (str) > takes subject (eg: file, function). -# Means: -# (use, subject) > (Processing 'Sleep') +# Starts spinner to spin. +# +# ARGS: +# - use (str): takes process (eg: installing, processing). +# - subject (str): takes subject (eg: file, function). +# +# EG: +# - (use,subject): (Processing 'Sleep') spinner.start(){ [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; @@ -91,7 +87,7 @@ spinner.start(){ } # spinner.stop() -# Stops spinner to spin. +# Stops spinner to spin. spinner.stop(){ _spinner stop ${?} ${_SpinnerPid}; unset ${_SpinnerPid}; From 1db20d17b49f633ac51d6234d6a241ad08086ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sun, 4 Feb 2024 16:49:10 +0530 Subject: [PATCH 12/18] docs updated in string and url module. --- src/string.sh | 115 ++++++++++++++++++++++++++------------------------ src/url.sh | 36 +++++++++------- 2 files changed, 81 insertions(+), 70 deletions(-) diff --git a/src/string.sh b/src/string.sh index 83d511c..0c243ff 100644 --- a/src/string.sh +++ b/src/string.sh @@ -1,28 +1,31 @@ #!/bin/bash -# text.randize(array) -> obj -# Gives you random object of array. -# Args: -# array (array) > takes array of objects. +# text.randize(array) ~ obj +# Gives you random object of array. +# +# ARGS: +# - array (array): takes array of objects. text.randize(){ local array=("$@"); echo "${array[$(( RANDOM % ${#array[@]} ))]}"; } -# text.isdigit(str) -> bool -# Checks string is digit or not. -# Args: -# str (str) > takes string as arg. +# text.isdigit(str) ~ bool +# Checks string is digit or not. +# +# ARGS: +# - str (str): takes string as arg. text.isdigit(){ [[ "${1}" =~ ^[[:digit:]]+$ ]]; } -# text.replace(str,old,neu) -> str -# This replace string to string. -# Args: -# str (str) > takes string as arg. -# old (str) > takes string to replace. -# neu (str) > takes string to replace with. +# text.replace(str,old,neu) ~ str +# This replace string to string. +# +# ARGS: +# - str (str): takes string as arg. +# - old (str): takes string to replace. +# - neu (str): takes string to replace with. text.replace(){ local String="${1}"; local lastString="${2}"; @@ -35,75 +38,79 @@ text.replace(){ echo "${String/${lastString}/"${toString}"}"; } -# text.len(str) -> int -# Gives you lenth of given string. +# text.len(str) ~ int +# Gives you lenth of given string. text.len(){ echo "${#1}"; } -# text.len.strip(str) -> int -# Gives you count of lines in given string. -# Args: -# str (str) > takes string as arg. +# text.len.strip(str) ~ int +# Gives you count of lines in given string. +# +# ARGS: +# - str (str): takes string as arg. text.len.strip(){ echo "${@}" | wc -l } -# text.startwith(str,startstr) -> bool -# Checks that string startswith given substring or not. -# Args: -# str (str) > takes string as arg. -# startstr (str) > takes substring as arg. +# text.startwith(str,startstr) ~ bool +# Checks that string startswith given substring or not. +# +# ARGS: +# - str (str): takes string as arg. +# - startstr (str): takes substring as arg. text.startwith(){ [[ "${1}" == "${2}"* ]]; } -# text.startwith(str,endstr) -> bool -# Checks that string endswith given substring or not. -# Args: -# str (str) > takes string as arg. -# endstr (str) > takes substring as arg. +# text.startwith(str,endstr) ~ bool +# Checks that string endswith given substring or not. +# +# ARGS: +# - str (str): takes string as arg. +# - endstr (str): takes substring as arg. text.endswith(){ [[ "${1}" == *"${2}" ]]; } -# text.contains(str,contain) -> bool -# Checks that string contains substring or not. -# Args: -# str (str) > takes string as arg. -# contain (str) > takes charachter. +# text.contains(str,contain) ~ bool +# Checks that string contains substring or not. +# +# ARGS: +# - str (str): takes string as arg. +# - contain (str): takes charachter. text.contains(){ [[ "${1}" == *"${2}"* ]]; } -# text.charCount(str,char) -> int -# Gives you count of a given character in given string. -# Args: -# str (str) > takes string as arg. -# char (char) > takes charachter. +# text.charCount(str,char) ~ int +# Gives you count of a given character in given string. +# +# ARGS: +# - str (str): takes string as arg. +# - char (char): takes charachter. text.charCount(){ echo "${1}" | tr -cd "${2}" | wc -c } -# text.replace_charAt(str,pos,char) -> str -# Replace character of a string at given pos. -# Args: -# str (str) > takes string as arg. -# pos (int) > takes position. -# char (char) > takes charachter. +# text.replace_charAt(str,pos,char) ~ str +# Replace character of a string at given pos. +# +# ARGS: +# - str (str): takes string as arg. +# - pos (int): takes position. +# - char (char): takes charachter. text.replace_charAt(){ + # checking args given or not. + [[ ${#} -eq 3 ]] || + { echo "error: 'missing args'" && return 1; }; local String="${1}"; local Pos="${2}"; local Char="${3}"; - # checking args given or not. - if [[ ! ${#} -eq 3 ]]; then - echo "error: 'missing args'"; - return 1; - fi - # Extract the substring before the pos + # Substring before the pos. local PreString="${String:0:Pos-1}"; - # Extract the substring after the pos + # Substring after the pos. local PostString="${String:Pos}"; - # Concatenate the substrings with the replacement character + # Concatenate substrings with replace character. echo "${PreString}${Char}${PostString}"; } diff --git a/src/url.sh b/src/url.sh index ce0ddc6..f09d8e6 100644 --- a/src/url.sh +++ b/src/url.sh @@ -17,12 +17,14 @@ source "${Dir}"/inspect.sh # source spinner.sh # source inspect.sh -# url.contentSize(url) -> int -# Gives you size of content file in MiB. -# Args: -# url (str) > takes url as string. -# Returns: -# size (int) > size in MiB (eg: 60). +# url.contentSize(url) ~ int +# Gives you size of content file in MiB. +# +# ARGS: +# - url (str): takes url as string. +# +# RETURNS: +# - size (int): size in MiB (eg: 60). url.contentSize(){ # checking required functions. inspect.is_func 'wget'; @@ -31,11 +33,12 @@ url.contentSize(){ echo "$(( ContentSize/1048576 ))"; } -# url.contentChart(urls,*paths) -# This is used to chart urls and size. -# Args: -# urls (array) > takes one or array of urls. -# paths (array) > Optional: takes file paths. +# url.contentChart(urls,@paths) +# This is used to chart urls and size. +# +# ARGS: +# - urls (array): takes one or array of urls. +# - paths (array,optional): takes file paths. url.contentChart(){ inspect.ScreenSize '50' '12'; local ARGs=("${@}") @@ -66,11 +69,12 @@ url.contentChart(){ return 0; } -# url.getContent(urls,*paths) -# This is used to get files via urls. -# Args: -# urls (array) > takes one or array of url. -# paths (array) > Optional: takes files path. +# url.getContent(urls,@paths) +# This is used to get files via urls. +# +# ARGS: +# - urls (array): takes one or array of url. +# - paths (array,optional): takes files path. url.getContent(){ local ARGs=("${@}") url.contentChart "${ARGs[@]}"; From 141de7f730754587693e0b9f71e5f0d6f59cc96a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Sun, 4 Feb 2024 20:15:34 +0530 Subject: [PATCH 13/18] added log experimental function to say module --- src/say.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/say.sh b/src/say.sh index dc70bcd..0f96e6f 100644 --- a/src/say.sh +++ b/src/say.sh @@ -128,4 +128,17 @@ say.checkStatus(){ echo -e " ~ [${COLOR_STRIP[0]} FAILED ${Clear}]"; exit 1; fi -} \ No newline at end of file +} + +# log(str) ~ str +# Say log to the user. +log(){ + case "${?}" in + 0) + _status -success "${@}"; + ;; + *) + _status -error "${@}"; + ;; + esac +} From 0b499484b3d4526eb73ab02b73ec436e43450e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= <100824266+OurCodeBase@users.noreply.github.com> Date: Sun, 4 Feb 2024 22:08:36 +0530 Subject: [PATCH 14/18] release version updated. --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 80e25a2..cf3caa9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ A bash library to create standalone scripts. -Release: v1.2.58 +Release: v1.2.59 There are some features of bash-sdk are mentioned here. * OOPS like code 💎. From 891514151f45d7ca973739290929aaf2914fc61a Mon Sep 17 00:00:00 2001 From: root Date: Sun, 4 Feb 2024 17:54:06 +0000 Subject: [PATCH 15/18] smallcase all args and returns keywords. --- src/ask.sh | 6 +++--- src/db.sh | 10 +++++----- src/file.sh | 32 ++++++++++++++++---------------- src/inspect.sh | 2 +- src/os.sh | 2 +- src/package.sh | 8 ++++---- src/repo.sh | 8 ++++---- src/say.sh | 4 ++-- src/screen.sh | 2 +- src/spinner.sh | 2 +- src/string.sh | 18 +++++++++--------- src/url.sh | 8 ++++---- 12 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/ask.sh b/src/ask.sh index 9315a19..ef7bb0f 100644 --- a/src/ask.sh +++ b/src/ask.sh @@ -21,7 +21,7 @@ askPosition=''; # ask.case(title) ~ bool # This takes case ( yes or no ) for any work. # -# ARGS: +# Args: # - title (str): takes title (eg: You're Agree). ask.case(){ echo -ne "\n ${1}"; @@ -46,11 +46,11 @@ ask.case(){ # ask.choice(title,list) ~ var # This creates a simple menu. # -# ARGS: +# Args: # - title (str): takes title (eg: Choose One). # - list (array): takes array as arg. # -# RETURNS: +# Returns: # - var (str): result in 'askObject' & 'askPosition' variables. ask.choice(){ PS3=" diff --git a/src/db.sh b/src/db.sh index 221a74b..5289989 100644 --- a/src/db.sh +++ b/src/db.sh @@ -5,7 +5,7 @@ # _db.isKeyExist(key,file) ~ bool # Checks that key is exist or not. # -# ARGS: +# Args: # - key (str): takes key of db. # - file (str): takes file path. _db.isKeyExist(){ @@ -15,7 +15,7 @@ _db.isKeyExist(){ # db.read(key,file) ~ str # Reads value of given key in db file. # -# ARGS: +# Args: # - key (str): takes key of db. # - file (str): takes file path. db.read(){ @@ -31,7 +31,7 @@ db.read(){ # db.create(key,value,file) # Creates data key and value to db. # -# ARGS: +# Args: # - key (str): takes key of db. # - value (str): takes value of key. # - file (str): takes file path. @@ -47,7 +47,7 @@ db.create(){ # db.update(key,value,file) # Update data of key in db file. # -# ARGS: +# Args: # - key (str): takes key of db. # - value (str): takes update value of key. # - file (str): takes file path. @@ -70,7 +70,7 @@ db.update(){ # db.delete(key,file) # Delete key and value of db file. # -# ARGS: +# Args: # - key (str): takes key of db. # - file (str): takes file path. db.delete(){ diff --git a/src/file.sh b/src/file.sh index 2ba1448..f2d3640 100644 --- a/src/file.sh +++ b/src/file.sh @@ -3,7 +3,7 @@ # path.isdir(dir) ~ bool # Checks that directory exist or not. # -# ARGS: +# Args: # - dir (str): takes directory path. path.isdir(){ # checking dir exist or not. @@ -13,7 +13,7 @@ path.isdir(){ # path.isfile(file) ~ bool # Checks that file exist or not. # -# ARGS: +# Args: # - file (str): takes file path. path.isfile(){ # checking file exist or not. @@ -23,7 +23,7 @@ path.isfile(){ # file.move(src,dst) # This can use to move files. # -# ARGS: +# Args: # - src (str): takes source path. # - dst (str): takes destination path. file.move(){ @@ -34,7 +34,7 @@ file.move(){ # file.copy(src,dst) # This can use to copy files. # -# ARGS: +# Args: # - src (str): takes source path. # - dst (str): takes destination path. file.copy(){ @@ -44,7 +44,7 @@ file.copy(){ # file.erase(file) # -# ARGS: +# Args: # - file (str): takes file path. file.erase(){ # cleaning file. @@ -54,7 +54,7 @@ file.erase(){ # file.pop(pos,file) # Popout given position of line in file. # -# ARGS: +# Args: # - pos (int): takes position. # - file (str): takes file path. file.pop(){ @@ -65,7 +65,7 @@ file.pop(){ # file.readlines.int(file) ~ int # Gives you total lines of a file. # -# ARGS: +# Args: # - file (str): takes file path. file.readlines.int(){ # return final result. @@ -75,7 +75,7 @@ file.readlines.int(){ # file.readline(pos,file) ~ str # Gives you line from given position of file. # -# ARGS: +# Args: # - pos (int): takes position. # - file (str): takes file path. file.readline(){ @@ -86,7 +86,7 @@ file.readline(){ # file.readlines(file) ~ STRIP. # Gives you STRIP array of lines of given file. # -# ARGS: +# Args: # - file (str): takes file path. file.readlines(){ # taking file arg from user. @@ -108,7 +108,7 @@ file.readlines(){ # file.readline.tall(file) ~ str # Gives you largest line of file. # -# ARGS: +# Args: # - file (str): takes file path. file.readline.tall(){ # return final result. @@ -118,7 +118,7 @@ file.readline.tall(){ # file.replace.pos(str,pos,file) # This replace text from given line of file. # -# ARGS: +# Args: # - str (str): takes string to replace. # - pos (int): takes position. # - file (str): takes file path. @@ -130,7 +130,7 @@ file.replace.pos(){ # file.search(str,file) ~ str # Search given text in file & return you that line. # -# ARGS: +# Args: # - str (str): takes string to search. # - file (str): takes file path. file.search(){ @@ -141,7 +141,7 @@ file.search(){ # file.search.pos(str,file) ~ pos # Search given text in file & return you position (eg: 1,2). # -# ARGS: +# Args: # - str (str): takes string to search. # - file (str): takes file path. file.search.pos(){ @@ -152,7 +152,7 @@ file.search.pos(){ # path.dirArray(dir,@--by-time,@--no-extension) ~ STRIP. # Gives you array of files in given directory. # -# ARGS: +# Args: # - dir (str): takes directory path. # - --by-time (obj,optional): list will be sorted by time. # - --no-extension (obj,optional): list have no file extensions. @@ -184,7 +184,7 @@ path.dirArray(){ # file.append.hori(str,pos,file) # This appends text to file horizontally. # -# ARGS: +# Args: # - str (str): takes string to append it. # - pos (int): takes position. # - file (str): takes file path. @@ -203,7 +203,7 @@ file.append.hori(){ # file.append.vert(str,pos,file) # This appends text to file vertically. # -# ARGS: +# Args: # - str (str): takes string to append it. # - pos (int): takes position. # - file (str): takes file path. diff --git a/src/inspect.sh b/src/inspect.sh index 3107337..e8d38fd 100644 --- a/src/inspect.sh +++ b/src/inspect.sh @@ -16,7 +16,7 @@ source "${Dir}"/os.sh # inspect.ScreenSize(cols,lines) ~ str # Checks that screen size is sufficient to project. # -# ARGS: +# Args: # - cols (int): takes columns as arg. # - lines (int): takes lines as arg. inspect.ScreenSize(){ diff --git a/src/os.sh b/src/os.sh index 9c217ec..8c964f8 100644 --- a/src/os.sh +++ b/src/os.sh @@ -31,7 +31,7 @@ os.is_shell.zsh(){ # os.is_func(function) ~ bool # Checks that function is exist or not. # -# ARGS: +# Args: # - function (str): takes function as string. os.is_func(){ command -v "${1}" &> /dev/null; diff --git a/src/package.sh b/src/package.sh index 01bf92f..d4100e1 100644 --- a/src/package.sh +++ b/src/package.sh @@ -20,11 +20,11 @@ source "${Dir}"/os.sh # pkg.size(@--dnload ,@--install ) ~ int # Gives you needed size of package. # -# ARGS: +# Args: # - --dnload (obj,str,optional): To get package file size. # - --install (obj,str,optional): To get package installed size. # -# RETURNS: +# Returns: # - size (int): Gives you size in MiBs. pkg.size(){ # checking args given or not. @@ -49,7 +49,7 @@ pkg.size(){ # pkg.chart(pkgs) # Use to view chart of packages. # -# ARGS: +# Args: # - pkgs (array): takes array of packages. pkg.chart(){ inspect.ScreenSize '62' '12'; @@ -99,7 +99,7 @@ pkg.chart(){ # pkg.install(packages) # Used to install packages with good ui. # -# ARGS: +# Args: # - packages (array): takes packages as args. (eg: python nodejs) pkg.install(){ # function starts here. diff --git a/src/repo.sh b/src/repo.sh index 27ee62a..d4bf4b9 100644 --- a/src/repo.sh +++ b/src/repo.sh @@ -18,10 +18,10 @@ source "${Dir}"/ask.sh # repo.size(api) ~ int # Used to get size of a repo. # -# ARGS: +# Args: # - api (str): takes api of github repo. (eg: "OurCodeBase/bash-sdk") # -# RETURNS: +# Returns: # - size (int): gives you file size in MiB. (eg: 30) repo.size(){ inspect.is_func 'curl'; @@ -33,7 +33,7 @@ repo.size(){ # repo.chart(apis) # Used to view info of given repositories. # -# ARGS: +# Args: # - apis (array): takes array of repository api. repo.chart(){ inspect.ScreenSize '50' '12'; @@ -64,7 +64,7 @@ repo.chart(){ # repo.clone(apis,@dirs) # Used to start cloning of a repository. # -# ARGS: +# Args: # - apis (array): takes apis of github repo. (eg: OurCodeBase/bash-sdk) # - dirs (array,optional): You can give directory path to clone to it. repo.clone(){ diff --git a/src/say.sh b/src/say.sh index 0f96e6f..625ffb7 100644 --- a/src/say.sh +++ b/src/say.sh @@ -40,7 +40,7 @@ say.gencolor(){ # _say.colorizeString(str,@--gencolor,@--color ) ~ str # Create and returns you raw colorized strings. # -# ARGS: +# Args: # - str (str): takes string as arg. # - --gencolor (obj,optional): adds colors randomly to syntax. # - --color (obj,str,optional): adds given color to syntax. @@ -119,7 +119,7 @@ say.success(){ # say.checkStatus(exitCode) ~ str # This prints success or failure according to exit code. # -# ARGS: +# Args: # - exitCode (int): takes exit code. say.checkStatus(){ if [[ "${1}" == 0 ]]; then diff --git a/src/screen.sh b/src/screen.sh index 9eb67d1..f745165 100644 --- a/src/screen.sh +++ b/src/screen.sh @@ -17,7 +17,7 @@ screen.lines(){ # screen.isSize(cols,lines) ~ bool # Checks that screen has atleast given lines and columns. # -# ARGS: +# Args: # - cols (int): takes columns as int. # - lines (int): takes lines as int. screen.isSize(){ diff --git a/src/spinner.sh b/src/spinner.sh index 5c71774..5ae9db5 100644 --- a/src/spinner.sh +++ b/src/spinner.sh @@ -70,7 +70,7 @@ _spinner(){ # spinner.start(use,subject) # Starts spinner to spin. # -# ARGS: +# Args: # - use (str): takes process (eg: installing, processing). # - subject (str): takes subject (eg: file, function). # diff --git a/src/string.sh b/src/string.sh index 0c243ff..68f5328 100644 --- a/src/string.sh +++ b/src/string.sh @@ -3,7 +3,7 @@ # text.randize(array) ~ obj # Gives you random object of array. # -# ARGS: +# Args: # - array (array): takes array of objects. text.randize(){ local array=("$@"); @@ -13,7 +13,7 @@ text.randize(){ # text.isdigit(str) ~ bool # Checks string is digit or not. # -# ARGS: +# Args: # - str (str): takes string as arg. text.isdigit(){ [[ "${1}" =~ ^[[:digit:]]+$ ]]; @@ -22,7 +22,7 @@ text.isdigit(){ # text.replace(str,old,neu) ~ str # This replace string to string. # -# ARGS: +# Args: # - str (str): takes string as arg. # - old (str): takes string to replace. # - neu (str): takes string to replace with. @@ -47,7 +47,7 @@ text.len(){ # text.len.strip(str) ~ int # Gives you count of lines in given string. # -# ARGS: +# Args: # - str (str): takes string as arg. text.len.strip(){ echo "${@}" | wc -l @@ -56,7 +56,7 @@ text.len.strip(){ # text.startwith(str,startstr) ~ bool # Checks that string startswith given substring or not. # -# ARGS: +# Args: # - str (str): takes string as arg. # - startstr (str): takes substring as arg. text.startwith(){ @@ -66,7 +66,7 @@ text.startwith(){ # text.startwith(str,endstr) ~ bool # Checks that string endswith given substring or not. # -# ARGS: +# Args: # - str (str): takes string as arg. # - endstr (str): takes substring as arg. text.endswith(){ @@ -76,7 +76,7 @@ text.endswith(){ # text.contains(str,contain) ~ bool # Checks that string contains substring or not. # -# ARGS: +# Args: # - str (str): takes string as arg. # - contain (str): takes charachter. text.contains(){ @@ -86,7 +86,7 @@ text.contains(){ # text.charCount(str,char) ~ int # Gives you count of a given character in given string. # -# ARGS: +# Args: # - str (str): takes string as arg. # - char (char): takes charachter. text.charCount(){ @@ -96,7 +96,7 @@ text.charCount(){ # text.replace_charAt(str,pos,char) ~ str # Replace character of a string at given pos. # -# ARGS: +# Args: # - str (str): takes string as arg. # - pos (int): takes position. # - char (char): takes charachter. diff --git a/src/url.sh b/src/url.sh index f09d8e6..16e2cd5 100644 --- a/src/url.sh +++ b/src/url.sh @@ -20,10 +20,10 @@ source "${Dir}"/inspect.sh # url.contentSize(url) ~ int # Gives you size of content file in MiB. # -# ARGS: +# Args: # - url (str): takes url as string. # -# RETURNS: +# Returns: # - size (int): size in MiB (eg: 60). url.contentSize(){ # checking required functions. @@ -36,7 +36,7 @@ url.contentSize(){ # url.contentChart(urls,@paths) # This is used to chart urls and size. # -# ARGS: +# Args: # - urls (array): takes one or array of urls. # - paths (array,optional): takes file paths. url.contentChart(){ @@ -72,7 +72,7 @@ url.contentChart(){ # url.getContent(urls,@paths) # This is used to get files via urls. # -# ARGS: +# Args: # - urls (array): takes one or array of url. # - paths (array,optional): takes files path. url.getContent(){ From 45d8e46e45e28c3d77d1d173dcda24fa926e4e0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Mon, 5 Feb 2024 00:38:17 +0530 Subject: [PATCH 16/18] Undo release version updated to v1.2.59. This reverts commit 0b499484b3d4526eb73ab02b73ec436e43450e0e. --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index cf3caa9..80e25a2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,6 @@ A bash library to create standalone scripts. -Release: v1.2.59 +Release: v1.2.58 There are some features of bash-sdk are mentioned here. * OOPS like code 💎. From e368d78cfa6d482585714de0f0ef58d3ee83a44e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Mon, 5 Feb 2024 00:54:38 +0530 Subject: [PATCH 17/18] code reverted to v1.2 --- src/ask.sh | 61 +++++++----------- src/cursor.sh | 2 +- src/db.sh | 84 ++++++++++++++----------- src/file.sh | 159 +++++++++++++++++++++++----------------------- src/inspect.sh | 13 ++-- src/os.sh | 23 ++++--- src/package.sh | 34 +++++----- src/repo.sh | 24 +++---- src/say.sh | 168 +++++++++++++++++++++---------------------------- src/screen.sh | 17 +++-- src/spinner.sh | 44 +++++++------ src/string.sh | 97 +++++++++++++--------------- src/url.sh | 28 ++++----- 13 files changed, 355 insertions(+), 399 deletions(-) diff --git a/src/ask.sh b/src/ask.sh index ef7bb0f..a5a5a65 100644 --- a/src/ask.sh +++ b/src/ask.sh @@ -14,62 +14,47 @@ source "${Dir}"/string.sh # source string.sh # Stores object that you selected. -askObject=''; +askChoice=''; # Stores position of object that you selected. -askPosition=''; +askReply=''; -# ask.case(title) ~ bool -# This takes case ( yes or no ) for any work. -# +# ask.case(title) -> bool +# This takes case ( yes or no ) for any work. # Args: -# - title (str): takes title (eg: You're Agree). +# title (str) > takes title (eg: You're Agree). ask.case(){ echo -ne "\n ${1}"; read -p " ? [Y/n] " ARGS; echo; case "${ARGS}" in - y|Y|'') - return 0; - ;; - n|N) - say.error "Process Aborted.\n"; - exit 1; - ;; - *) say.error "You have to enter only \n - \t\t 'Y' for Yes & \n - \t\t 'n' for No.\n"; - exit 1; - ;; + y|Y|'') return 0;; + n|N) { say.error "Process Aborted.\n" && exit 1; };; + *) { say.error "You have to enter only \n\t\t'Y' for Yes & \n\t\t'n' for No.\n" && exit 1; };; esac } -# ask.choice(title,list) ~ var -# This creates a simple menu. -# +# ask.choice(title,list) -> var +# This creates a simple menu. # Args: -# - title (str): takes title (eg: Choose One). -# - list (array): takes array as arg. -# +# title (str) > takes title (eg: Choose One). +# list (array) > takes array as arg. # Returns: -# - var (str): result in 'askObject' & 'askPosition' variables. +# object (str) > result in 'askChoice' & 'askReply' variables. ask.choice(){ PS3=" - ${1}: "; + ${1} > "; shift; local ARGs=("${@}"); echo; select ARG in "${ARGs[@]}" do - if text.isdigit "${REPLY}"; then - say.error "You can only input 'digits'.\n"; - exit 1; - elif [[ "${REPLY}" -gt "${#ARGs[@]}" ]]; then - say.error "You should input correct digits.\n"; - exit 1; - else - askObject="${ARG}"; - askPosition="${REPLY}"; - break; - fi + text.isdigit "${REPLY}" || { + say.error "You can only input 'digits'.\n" && exit 1; + }; + [[ "${REPLY}" -gt "${#ARGs[@]}" ]] && + say.error "You should input correct digits.\n" && exit 1; + askChoice="${ARG}"; + askReply="${REPLY}"; + break; done -} +} \ No newline at end of file diff --git a/src/cursor.sh b/src/cursor.sh index c147d12..59e8bb9 100644 --- a/src/cursor.sh +++ b/src/cursor.sh @@ -1,7 +1,7 @@ #!/bin/bash # setCursor(on~off) -# Switch terminal cursor easily. +# Switch terminal cursor easily. setCursor(){ setterm -cursor "${1}"; } \ No newline at end of file diff --git a/src/db.sh b/src/db.sh index 5289989..0d0ea4e 100644 --- a/src/db.sh +++ b/src/db.sh @@ -1,83 +1,95 @@ #!/bin/bash -# Supports only yaml syntax database. +# Support only Yaml syntax database. -# _db.isKeyExist(key,file) ~ bool -# Checks that key is exist or not. -# +# _db.isfile(file) -> bool +# Checks that db file exist or not. # Args: -# - key (str): takes key of db. -# - file (str): takes file path. -_db.isKeyExist(){ +# file (str) > takes file path. +_db.isfile(){ + # checking file exist or not. + test -f "${1}"; +} + +# _db.iskey(key,file) -> bool +# Checks that key is exist or not. +# Args: +# key (str) > takes key of db. +# file (str) > takes file path. +_db.iskey(){ grep "${1}: " "${2}" &>/dev/null; } -# db.read(key,file) ~ str -# Reads value of given key in db file. -# +# db.read(key,file) -> str +# Give you data of key of yaml db. # Args: -# - key (str): takes key of db. -# - file (str): takes file path. +# key (str) > takes key of db. +# file (str) > takes file path. db.read(){ # checking args given or not. [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; - _db.isKeyExist "${1}" "${2}" || + _db.iskey "${1}" "${2}" || { echo "error: ${1}: 'key not exists'" && return 1; }; local dbValue="$(grep "${1}: " "${2}")"; echo "${dbValue}" | awk -F': ' '{sub(/^[^:]*: /, ""); print}'; } # db.create(key,value,file) -# Creates data key and value to db. -# +# Adds data key and value to db. # Args: -# - key (str): takes key of db. -# - value (str): takes value of key. -# - file (str): takes file path. +# key (str) > takes key of db. +# value (str) > takes value of key. +# file (str) > takes file path. db.create(){ # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - _db.isKeyExist "${1}" "${3}" && + _db.iskey "${1}" "${3}" && { echo "error: ${1}: 'key already exists'" && return 1; }; echo -ne "\n${1}: ${2}" >> "${3}"; } # db.update(key,value,file) -# Update data of key in db file. -# +# Update data of key in db file. # Args: -# - key (str): takes key of db. -# - value (str): takes update value of key. -# - file (str): takes file path. +# key (str) > takes key of db. +# value (str) > takes update value of key. +# file (str) > takes file path. db.update(){ # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - _db.isKeyExist "${1}" "${3}" || + _db.iskey "${1}" "${3}" || { echo "error: ${1}: 'key not exists'" && return 1; }; + # taking key arg. local dbKey="${1}"; - local dbUpdateValue="${2}"; + # taking update value of given key. + local dbUpdatedValue="${2}"; + # taking db file path. local dbFile="${3}"; - local dbCurrentValue="$(db.read "${dbKey}" "${dbFile}")"; - local dbCurrentPair="${dbKey}: ${dbCurrentValue}"; # concated to create pair. - local dbCurrentPairPos="$(grep -n "${dbCurrentPair}" "${dbFile}" | cut -d: -f1 | head -n 1;)"; - local dbUpdatedPair="${dbCurrentPair/${dbCurrentValue}/"${dbUpdateValue}"}"; - sed -i "${dbCurrentPairPos}c\\${dbUpdatedPair}" "${dbFile}"; # replaced full pair. + # getting old value of given key. + local dbValue="$(db.read "${dbKey}" "${dbFile}")"; + # concating key and old value. + local dbKeyValuePair="${dbKey}: ${dbValue}"; + # getting position of concated line in file. + local dbKeyValuePairPos="$(grep -n "${dbKeyValuePair}" "${dbFile}" | cut -d: -f1 | head -n 1;)"; + # replacing value from old to new. + local dbUpdatedKeyValuePair="${dbKeyValuePair/${dbValue}/"${dbUpdatedValue}"}"; + # placing updated value to position. + sed -i "${dbKeyValuePairPos}c\\${dbUpdatedKeyValuePair}" "${dbFile}"; } # db.delete(key,file) -# Delete key and value of db file. -# +# Delete key and value of db file. # Args: -# - key (str): takes key of db. -# - file (str): takes file path. +# key (str) > takes key of db. +# file (str) > takes file path. db.delete(){ # checking args given or not. [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; - _db.isKeyExist "${1}" "${2}" || + _db.iskey "${1}" "${2}" || { echo "error: ${1}: 'key not exists'" && return 1; }; local dbKey="${1}: "; local dbFile="${2}"; diff --git a/src/file.sh b/src/file.sh index f2d3640..414b1c6 100644 --- a/src/file.sh +++ b/src/file.sh @@ -1,93 +1,84 @@ #!/bin/bash -# path.isdir(dir) ~ bool -# Checks that directory exist or not. -# +# path.isdir(dir) -> bool +# Checks that directory exist or not. # Args: -# - dir (str): takes directory path. +# dir (str) > takes directory path. path.isdir(){ # checking dir exist or not. test -d "${1}"; } -# path.isfile(file) ~ bool -# Checks that file exist or not. -# +# path.isfile(file) -> bool +# Checks that file exist or not. # Args: -# - file (str): takes file path. +# file (str) > takes file path. path.isfile(){ # checking file exist or not. test -f "${1}"; } # file.move(src,dst) -# This can use to move files. -# +# This can use to move files. # Args: -# - src (str): takes source path. -# - dst (str): takes destination path. +# src (str) > takes source path. +# dst (str) > takes destination path. file.move(){ # moving file to destination. mv -f "${1}" "${2}" 2>/dev/null; } # file.copy(src,dst) -# This can use to copy files. -# +# This can use to copy files. # Args: -# - src (str): takes source path. -# - dst (str): takes destination path. +# src (str) > takes source path. +# dst (str) > takes destination path. file.copy(){ # copying files to destination. cp -rf "${1}" "${2}" 2>/dev/null; } # file.erase(file) -# # Args: -# - file (str): takes file path. +# file (str) > takes file path. file.erase(){ # cleaning file. truncate -s 0 "${1}"; } # file.pop(pos,file) -# Popout given position of line in file. -# +# Popout given position of line in file. # Args: -# - pos (int): takes position. -# - file (str): takes file path. +# pos (int) > takes position. +# file (str) > takes file path. file.pop(){ # return final result. sed -i "${1}d" "${2}"; } -# file.readlines.int(file) ~ int -# Gives you total lines of a file. -# +# file.readlines.int(file) -> int +# Gives you total lines of a file. # Args: -# - file (str): takes file path. +# file (str) > takes file path. file.readlines.int(){ # return final result. wc -l "${1}" | awk '{print $1}'; } -# file.readline(pos,file) ~ str -# Gives you line from given position of file. -# +# file.readline(pos,file) -> str +# Gives you line from given position of file. # Args: -# - pos (int): takes position. -# - file (str): takes file path. +# pos (int) > takes position. +# file (str) > takes file path. file.readline(){ # return final result. sed -n "${1}p" "${2}"; } -# file.readlines(file) ~ STRIP. -# Gives you STRIP array of lines of given file. -# +# file.readlines(file) -> STRIP. +# Gives you STRIP array of lines of given file. # Args: -# - file (str): takes file path. +# file (str) > takes file path. file.readlines(){ # taking file arg from user. local ARGFile="${1}"; @@ -105,59 +96,56 @@ file.readlines(){ return; } -# file.readline.tall(file) ~ str -# Gives you largest line of file. -# +# file.readline.tall(file) -> str +# Gives you largest line of file. # Args: -# - file (str): takes file path. +# file (str) > takes file path. file.readline.tall(){ # return final result. awk 'length > max_length { max_length = length; max_line = $0 } END { print max_line }' "${1}"; } # file.replace.pos(str,pos,file) -# This replace text from given line of file. -# +# This replace text from given line of file. # Args: -# - str (str): takes string to replace. -# - pos (int): takes position. -# - file (str): takes file path. +# str (str) > takes string to replace. +# pos (int) > takes position. +# file (str) > takes file path. file.replace.pos(){ # return final result. sed -i "${2}c\\${1}" "${3}"; } -# file.search(str,file) ~ str -# Search given text in file & return you that line. -# +# file.search(str,file) -> str +# Search given text in file & return you that line. # Args: -# - str (str): takes string to search. -# - file (str): takes file path. +# str (str) > takes string to search. +# file (str) > takes file path. file.search(){ # return final result. grep "${1}" "${2}"; } -# file.search.pos(str,file) ~ pos -# Search given text in file & return you position (eg: 1,2). -# +# file.search.pos(str,file) -> pos +# Search given text in file & return you position (eg: 1,2). # Args: -# - str (str): takes string to search. -# - file (str): takes file path. +# str (str) > takes string to search. +# file (str) > takes file path. file.search.pos(){ # return final result. grep -n "${1}" "${2}" | cut -d: -f1 | head -n 1; } -# path.dirArray(dir,@--by-time,@--no-extension) ~ STRIP. -# Gives you array of files in given directory. -# +# path.dirArray(dir,*options) -> STRIP. +# Gives you array of files in given directory. # Args: -# - dir (str): takes directory path. -# - --by-time (obj,optional): list will be sorted by time. -# - --no-extension (obj,optional): list have no file extensions. +# dir (str) > takes directory path. +# --by-time (obj) > Optional: list will be sorted by time. +# --no-extension (obj) > Optional: list have no file extensions. path.dirArray(){ + # taking directory arg from user. local ARGDir=${1} && shift; + # array list file location. local UriFile=".Uri"; # check directory exist. path.isdir "${ARGDir}" && { @@ -182,38 +170,51 @@ path.dirArray(){ } # file.append.hori(str,pos,file) -# This appends text to file horizontally. -# +# This appends text to file horizontally. # Args: -# - str (str): takes string to append it. -# - pos (int): takes position. -# - file (str): takes file path. +# str (str) > takes string to append it. +# pos (int) > takes position. +# file (str) > takes file path. file.append.hori(){ - # checking args given or not. - [[ ${#} -eq 3 ]] || - { echo "error: 'missing args'" && return 1; }; + # takes string as arg. local String="${1}"; + # takes position as arg. local Pos="${2}"; + # takes file as arg. local ARGFile="${3}"; - local ConsVar="$(file.readline "${Pos}" "${ARGFile}")"; - local ConsVar+="${String}"; # concat - file.replace.pos "${ConsVar}" "${Pos}" "${ARGFile}"; + # checking args given or not. + [[ ${#} -eq 3 ]] || + { echo "error: 'missing args'" && return 1; }; + # reading line from given position. + local Vir="$(file.readline "${Pos}" "${ARGFile}")"; + # concating string to it. + local Vir+="${String}"; + # replacing the line from file. + file.replace.pos "${Vir}" "${Pos}" "${ARGFile}"; + # return function. + return; } # file.append.vert(str,pos,file) -# This appends text to file vertically. -# +# This appends text to file vertically. # Args: -# - str (str): takes string to append it. -# - pos (int): takes position. -# - file (str): takes file path. +# str (str) > takes string to append it. +# pos (int) > takes position. +# file (str) > takes file path. file.append.vert(){ + # takes text as arg. + local String="${1}"; + # takes position as arg & have to reduce one. + local Pos="${2}"; + # takes file as arg. + local ARGFile="${3}"; # checking args given or not. [[ ${#} -eq 3 ]] || { echo "error: 'missing args'" && return 1; }; - local String="${1}"; - local Pos="${2}"; # needs to reduce one. - local ARGFile="${3}"; + # reduce one from variable. local Pos="$(( Pos - 1 ))"; + # do operation. sed -i "${Pos}r /dev/stdin" "${ARGFile}" <<< "${String}"; + # return function. + return; } diff --git a/src/inspect.sh b/src/inspect.sh index e8d38fd..d31fccb 100644 --- a/src/inspect.sh +++ b/src/inspect.sh @@ -13,12 +13,11 @@ source "${Dir}"/os.sh # source say.sh # source os.sh -# inspect.ScreenSize(cols,lines) ~ str -# Checks that screen size is sufficient to project. -# +# inspect.ScreenSize(cols,lines) -> str +# Checks that screen size is sufficient to project. # Args: -# - cols (int): takes columns as arg. -# - lines (int): takes lines as arg. +# cols (int) > takes columns as arg. +# lines (int) > takes lines as arg. inspect.ScreenSize(){ local ARGCols="${1}"; local ARGRou="${2}"; @@ -35,8 +34,8 @@ inspect.ScreenSize(){ }; } -# inspect.is_func(function) ~ str -# An extension of os.is_func. +# inspect.is_func(function) -> str +# An extension of os.is_func. inspect.is_func(){ os.is_func "${1}" || { say.error "There is no '${1}'\n diff --git a/src/os.sh b/src/os.sh index 8c964f8..8d745ef 100644 --- a/src/os.sh +++ b/src/os.sh @@ -1,25 +1,25 @@ #!/bin/bash -# os.is_userland() ~ bool -# OS is userland or not ? +# os.is_userland() -> bool +# OS is userland or not ? os.is_userland(){ ls '/host-rootfs/data/data/tech.ula/files/home' &> /dev/null; } -# os.is_termux() ~ bool -# OS is termux or not ? +# os.is_termux() -> bool +# OS is termux or not ? os.is_termux(){ ls '/data/data/com.termux/files/' &> /dev/null; } -# os.is_windows() ~ bool -# OS is windows or not ? +# os.is_windows() -> bool +# OS is windows or not ? os.is_windows(){ [[ "$(uname -a)" == *"windows"* ]] } -# os.is_shell.zsh() ~ bool -# Using z-shell or not ? +# os.is_shell.zsh() -> bool +# Using z-shell or not ? os.is_shell.zsh(){ if os.is_userland; then [[ "$(OurCodeBase-CShell)" == "zsh: "* ]]; @@ -28,11 +28,10 @@ os.is_shell.zsh(){ fi } -# os.is_func(function) ~ bool -# Checks that function is exist or not. -# +# os.is_func(function) -> bool +# Checks that function is exist or not. # Args: -# - function (str): takes function as string. +# function (str) > takes function as string. os.is_func(){ command -v "${1}" &> /dev/null; } diff --git a/src/package.sh b/src/package.sh index d4100e1..3af0839 100644 --- a/src/package.sh +++ b/src/package.sh @@ -17,22 +17,24 @@ source "${Dir}"/os.sh # source os.sh # source ask.sh -# pkg.size(@--dnload ,@--install ) ~ int -# Gives you needed size of package. -# +# pkg.size(dnload~install,package) -> int +# Gives you needed size of package. # Args: -# - --dnload (obj,str,optional): To get package file size. -# - --install (obj,str,optional): To get package installed size. -# +# dnload (str) > dnload to get file size. +# install (str) > install to get package installed size. +# package (str) > takes package (eg: python,nodejs). # Returns: -# - size (int): Gives you size in MiBs. +# size (int) > Gives you size in MiBs. +# Usage: +# pkg.size(dnload,package) > Gives you package file size. +# pkg.size(install,package) > Gives you package installed size. pkg.size(){ # checking args given or not. [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; case "${1}" in - --dnload) local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Download-Size:')";; - --install) local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Installed-Size:')";; + 'dnload') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Download-Size:')";; + 'install') local SizeSource="$(apt show "${2}" 2> /dev/null | grep 'Installed-Size:')";; esac local Size="$(echo "${SizeSource}" | awk '{print $2}')"; local SizeUnit="$(echo "${SizeSource}" | awk '{print $3}')"; @@ -47,10 +49,9 @@ pkg.size(){ } # pkg.chart(pkgs) -# Use to view chart of packages. -# +# Use to view chart of packages. # Args: -# - pkgs (array): takes array of packages. +# pkgs (array) > takes array of packages. pkg.chart(){ inspect.ScreenSize '62' '12'; # this takes all packages as arg. @@ -75,9 +76,9 @@ pkg.chart(){ # declare package version variable. local PackageVersion="$(echo "${PackageSource}" | grep 'Version:' | awk '{print $2}' | awk -F'-' '{print $1}' | awk -F'+' '{print $1}' | awk -F'~' '{print $1}')"; # declare package file size variable. - local PackageSizeDL="$(pkg.size --dnload "${ARG}")"; + local PackageSizeDL="$(pkg.size 'dnload' "${ARG}")"; # declare package installed size variable. - local PackageSizeIN="$(pkg.size --install "${ARG}")"; + local PackageSizeIN="$(pkg.size 'install' "${ARG}")"; printf " │ ${Green}%-25s${Clear} ${Yelo}%-10s${Clear} ${Yelo}%3s${Clear} %-3s ${Yelo}%3s${Clear} %-3s │\n" "${PackageVar}" "${PackageVersion}" "${PackageSizeDL}" 'MiB' "${PackageSizeIN}" 'MiB'; # Adding dl sizes of all packages. local PuraSizeDL=$(( PuraSizeDL + PackageSizeDL )); @@ -97,10 +98,9 @@ pkg.chart(){ } # pkg.install(packages) -# Used to install packages with good ui. -# +# Used to install packages with good ui. # Args: -# - packages (array): takes packages as args. (eg: python nodejs) +# packages (array) > takes packages as args. (eg: python nodejs neovim) pkg.install(){ # function starts here. local ARGs=("${@}"); diff --git a/src/repo.sh b/src/repo.sh index d4bf4b9..5fe4300 100644 --- a/src/repo.sh +++ b/src/repo.sh @@ -15,14 +15,12 @@ source "${Dir}"/ask.sh # source inspect.sh # source ask.sh -# repo.size(api) ~ int -# Used to get size of a repo. -# +# repo.size(api) -> int +# Used to get size of a repo. # Args: -# - api (str): takes api of github repo. (eg: "OurCodeBase/bash-sdk") -# +# api (str) > takes api of github repo. (eg: "OurCodeBase/bash-sdk") # Returns: -# - size (int): gives you file size in MiB. (eg: 30) +# size (int) > gives you file size in MiB. (eg: 30) repo.size(){ inspect.is_func 'curl'; local Api="$(echo "${1}" | awk '{print $1}')"; @@ -31,10 +29,9 @@ repo.size(){ } # repo.chart(apis) -# Used to view info of given repositories. -# +# Used to view info of given repositories. # Args: -# - apis (array): takes array of repository api. +# apis (array) > takes array of repository api. repo.chart(){ inspect.ScreenSize '50' '12'; local ARGs=("${@}"); @@ -61,12 +58,11 @@ repo.chart(){ return 0; } -# repo.clone(apis,@dirs) -# Used to start cloning of a repository. -# +# repo.clone(apis,*dirs) +# Used to start cloning of a repository. # Args: -# - apis (array): takes apis of github repo. (eg: OurCodeBase/bash-sdk) -# - dirs (array,optional): You can give directory path to clone to it. +# apis (array) > takes apis of github repo. (eg: OurCodeBase/bash-sdk) +# dirs (array) > Optional: You can give directory path to clone to it. repo.clone(){ # required to run. inspect.is_func 'git'; diff --git a/src/say.sh b/src/say.sh index 625ffb7..7f881ff 100644 --- a/src/say.sh +++ b/src/say.sh @@ -3,142 +3,114 @@ Clear="\033[0m"; # Suppressed Variables if (( 1<2 )); then -# ---------- -# Colors. -# Gora="\033[0;97m" -# ---------- +# Colors +Gora="\033[1;97m" # BG Colors. -# BGRed="\033[1;41m"; -# BGGreen="\033[1;42m"; -# BGYelo="\033[1;43m"; -# BGBold="\033[1m"; -# ---------- +BGRed="\033[1;41m"; +BGGreen="\033[1;42m"; +BGYelo="\033[1;43m"; # Status Colors. -# StatusRed="${BGRed}${Gora}"; -# StatusGreen="${BGGreen}${Gora}"; -# StatusYelo="${BGYelo}${Gora}"; -# ---------- +StatusRed="${BGRed}${Gora}"; +StatusGreen="${BGGreen}${Gora}"; +StatusYelo="${BGYelo}${Gora}"; # Global variables. -Pattern="'([^']*)'"; -# Color plate. -COLOR_STRIP=( - "\033[0;31m" # Red - "\033[0;32m" # Green - "\033[0;33m" # Yelo - "\033[0;34m" # Blue - "\033[0;35m" # Genta - "\033[0;36m" # Cyan -); +Source="'([^']*)'"; fi -# say.gencolor() ~ color -# Generates color codes for you. +# Colors +Red="\033[1;31m"; +Green="\033[1;32m"; +Yelo="\033[1;33m"; + +# say.gencolor() +# Gives you a random color code. say.gencolor(){ - echo "${COLOR_STRIP[$(( RANDOM % ${#COLOR_STRIP[@]} ))]}"; + local STRIP=( + "\033[1;31m" + "\033[1;32m" + "\033[1;33m" + "\033[1;34m" + "\033[1;35m" + "\033[1;36m" + ); + echo "${STRIP[$(( RANDOM % ${#STRIP[@]} ))]}"; + return 0; } -# _say.colorizeString(str,@--gencolor,@--color ) ~ str -# Create and returns you raw colorized strings. -# -# Args: -# - str (str): takes string as arg. -# - --gencolor (obj,optional): adds colors randomly to syntax. -# - --color (obj,str,optional): adds given color to syntax. -_say.colorizeString(){ - local ARGString="${1}"; - local isGenColorEnabled=1; - local Sentence=(${ARGString}); - local String; - shift; - case "${1}" in - --gencolor) - local isGenColorEnabled=0; - ;; - --color) - local Color="${2}"; - ;; - esac - for Shabd in "${Sentence[@]}" +# say(str) -> str +# Say strings with syntax highlighting feature. +say(){ + local String="${1}"; + local STRIP=(${String}); + local String=''; + for Uri in "${STRIP[@]}" do - if [[ "${Shabd}" =~ ${Pattern} ]]; then - [[ "${isGenColorEnabled}" == 1 ]] || \ + if [[ "${Uri}" =~ ${Source} ]]; then local Color="$(say.gencolor)"; - local String+="${Color}${Shabd}${Clear} "; - else - local String+="${Shabd} "; + local String+="${Color}${Uri}${Clear} "; + else local String+="${Uri} "; fi done - echo "${String}"; -} - -# say(str) ~ str -# Say strings with syntax highlighting feature. -say(){ - local String="$(_say.colorizeString "${1}" --gencolor)"; echo -e "${String}"; + return 0; } _status(){ case "${1}" in -error) - local Color="\033[0;31m"; - local Header="ERR"; - ;; - -warn) - local Color="\033[0;33m"; - local Header="WARN"; - ;; + local Color="\033[1;31m"; + local StatusColor="${StatusRed}"; + ;; -success) - local Color="\033[0;32m"; - local Header="INFO"; - ;; + local Color="\033[1;32m"; + local StatusColor="${StatusGreen}"; + ;; + -warn) + local Color="\033[1;33m"; + local StatusColor="${StatusYelo}"; + ;; esac - local String="$(_say.colorizeString "${2}" --color "${Color}")"; - echo -ne "[${Color} ${Header} ${Clear}]: "; - echo -e "${String}"; + local String="${2}"; + local xSTRIP=(${String}); + local String=''; + for Uri in "${xSTRIP[@]}" + do + if [[ "${Uri}" =~ ${Source} ]]; then + local String+="${Color}${Uri}${Clear} "; + else local String+="${Uri} "; + fi + done + echo -e "${StatusColor} INFO ${Clear} ➙ ${String}"; + return 0; } -# say.error(str) ~ str -# Says error to terminal. +# say.error(str) -> str +# Says error to terminal. say.error(){ _status -error "${@}"; } -# say.warn(str) ~ str -# Says warning to terminal. +# say.warn(str) -> str +# Says warning to terminal. say.warn(){ _status -warn "${@}"; } -# say.success(str) ~ str -# Says success to terminal. +# say.success(str) -> str +# Says success to terminal. say.success(){ _status -success "${@}"; } -# say.checkStatus(exitCode) ~ str -# This prints success or failure according to exit code. -# +# say.checkStatus(status) -> str +# This prints success or failure according to exit code. # Args: -# - exitCode (int): takes exit code. +# status (int) > takes exit code. say.checkStatus(){ if [[ "${1}" == 0 ]]; then - echo -e " ~ [${COLOR_STRIP[1]} SUCCESS ${Clear}]"; + echo -e " ➙ ${StatusGreen} SUCCESS ${Clear}"; else - echo -e " ~ [${COLOR_STRIP[0]} FAILED ${Clear}]"; + echo -e " ➙ ${StatusRed} FAILED ${Clear}"; exit 1; fi } - -# log(str) ~ str -# Say log to the user. -log(){ - case "${?}" in - 0) - _status -success "${@}"; - ;; - *) - _status -error "${@}"; - ;; - esac -} diff --git a/src/screen.sh b/src/screen.sh index f745165..9ba5595 100644 --- a/src/screen.sh +++ b/src/screen.sh @@ -1,25 +1,24 @@ #!/bin/bash -# screen.cols() ~ int -# Gives you current columns count in terminal. +# screen.cols() -> int +# Gives you current columns count in terminal. screen.cols(){ # current columns in terminal. stty size | awk '{print $2}' } -# screen.lines() ~ int -# Gives you current lines count in terminal. +# screen.lines() -> int +# Gives you current lines count in terminal. screen.lines(){ # current lines in terminal. stty size | awk '{print $1}' } -# screen.isSize(cols,lines) ~ bool -# Checks that screen has atleast given lines and columns. -# +# screen.isSize(cols,lines) -> bool +# Checks that screen has atleast given lines and columns. # Args: -# - cols (int): takes columns as int. -# - lines (int): takes lines as int. +# cols (int) > takes columns as int. +# lines (int) > takes lines as int. screen.isSize(){ local ARGCols=${1}; local ARGLines=${2}; diff --git a/src/spinner.sh b/src/spinner.sh index 5ae9db5..e7eec1d 100644 --- a/src/spinner.sh +++ b/src/spinner.sh @@ -1,9 +1,9 @@ #!/bin/bash # Shorts: -# - spinner.start(use,subject) -# - run your functions. -# - spinner.stop +# spinner.start(use,subject) +# run your functions. +# spinner.stop # Suppressed Variables if (( 1<2 )); then @@ -14,7 +14,15 @@ Genta="\033[1;35m"; Green="\033[1;32m"; Red="\033[1;31m"; Yelo="\033[1;33m"; -# Global variables. +Gora="\033[1;97m" +# BG Colors. +BGRed="\033[1;41m"; +BGGreen="\033[1;42m"; +BGYelo="\033[1;43m"; +# Status Colors. +StatusRed="${BGRed}${Gora}"; +StatusGreen="${BGGreen}${Gora}"; +StatusYelo="${BGYelo}${Gora}"; CircleIcon="● "; Success="SUCCESS"; Failure="FAILED"; @@ -28,7 +36,7 @@ Scribe=( fi # spinner.setCursor(on~off) -# Switch terminal cursor easily. +# Switch terminal cursor easily. spinner.setCursor(){ setterm -cursor "${1}"; } @@ -55,27 +63,23 @@ _spinner(){ exit 1; }; kill ${3} > /dev/null 2>&1 - echo -en "\b${Clear} → "; - if [[ $2 -eq 0 ]]; then - echo -e "[ ${Green}${Success}${Clear} ]"; - else - echo -e "[ ${Red}${Failure}${Clear} ]"; - spinner.setCursor on; + echo -en "\b${Clear} ➙ "; + [[ $2 -eq 0 ]] && + echo -e "${StatusGreen} ${Success} ${Clear}" || { + echo -e "${StatusRed} ${Failure} ${Clear}" && exit 1; - fi + }; ;; esac } # spinner.start(use,subject) -# Starts spinner to spin. -# +# Starts spinner to spin. # Args: -# - use (str): takes process (eg: installing, processing). -# - subject (str): takes subject (eg: file, function). -# -# EG: -# - (use,subject): (Processing 'Sleep') +# use (str) > takes process (eg: installing, processing). +# subject (str) > takes subject (eg: file, function). +# Means: +# (use, subject) > (Processing 'Sleep') spinner.start(){ [[ ${#} -eq 2 ]] || { echo "error: 'missing args'" && return 1; }; @@ -87,7 +91,7 @@ spinner.start(){ } # spinner.stop() -# Stops spinner to spin. +# Stops spinner to spin. spinner.stop(){ _spinner stop ${?} ${_SpinnerPid}; unset ${_SpinnerPid}; diff --git a/src/string.sh b/src/string.sh index 68f5328..83d511c 100644 --- a/src/string.sh +++ b/src/string.sh @@ -1,31 +1,28 @@ #!/bin/bash -# text.randize(array) ~ obj -# Gives you random object of array. -# +# text.randize(array) -> obj +# Gives you random object of array. # Args: -# - array (array): takes array of objects. +# array (array) > takes array of objects. text.randize(){ local array=("$@"); echo "${array[$(( RANDOM % ${#array[@]} ))]}"; } -# text.isdigit(str) ~ bool -# Checks string is digit or not. -# +# text.isdigit(str) -> bool +# Checks string is digit or not. # Args: -# - str (str): takes string as arg. +# str (str) > takes string as arg. text.isdigit(){ [[ "${1}" =~ ^[[:digit:]]+$ ]]; } -# text.replace(str,old,neu) ~ str -# This replace string to string. -# +# text.replace(str,old,neu) -> str +# This replace string to string. # Args: -# - str (str): takes string as arg. -# - old (str): takes string to replace. -# - neu (str): takes string to replace with. +# str (str) > takes string as arg. +# old (str) > takes string to replace. +# neu (str) > takes string to replace with. text.replace(){ local String="${1}"; local lastString="${2}"; @@ -38,79 +35,75 @@ text.replace(){ echo "${String/${lastString}/"${toString}"}"; } -# text.len(str) ~ int -# Gives you lenth of given string. +# text.len(str) -> int +# Gives you lenth of given string. text.len(){ echo "${#1}"; } -# text.len.strip(str) ~ int -# Gives you count of lines in given string. -# +# text.len.strip(str) -> int +# Gives you count of lines in given string. # Args: -# - str (str): takes string as arg. +# str (str) > takes string as arg. text.len.strip(){ echo "${@}" | wc -l } -# text.startwith(str,startstr) ~ bool -# Checks that string startswith given substring or not. -# +# text.startwith(str,startstr) -> bool +# Checks that string startswith given substring or not. # Args: -# - str (str): takes string as arg. -# - startstr (str): takes substring as arg. +# str (str) > takes string as arg. +# startstr (str) > takes substring as arg. text.startwith(){ [[ "${1}" == "${2}"* ]]; } -# text.startwith(str,endstr) ~ bool -# Checks that string endswith given substring or not. -# +# text.startwith(str,endstr) -> bool +# Checks that string endswith given substring or not. # Args: -# - str (str): takes string as arg. -# - endstr (str): takes substring as arg. +# str (str) > takes string as arg. +# endstr (str) > takes substring as arg. text.endswith(){ [[ "${1}" == *"${2}" ]]; } -# text.contains(str,contain) ~ bool -# Checks that string contains substring or not. -# +# text.contains(str,contain) -> bool +# Checks that string contains substring or not. # Args: -# - str (str): takes string as arg. -# - contain (str): takes charachter. +# str (str) > takes string as arg. +# contain (str) > takes charachter. text.contains(){ [[ "${1}" == *"${2}"* ]]; } -# text.charCount(str,char) ~ int -# Gives you count of a given character in given string. -# +# text.charCount(str,char) -> int +# Gives you count of a given character in given string. # Args: -# - str (str): takes string as arg. -# - char (char): takes charachter. +# str (str) > takes string as arg. +# char (char) > takes charachter. text.charCount(){ echo "${1}" | tr -cd "${2}" | wc -c } -# text.replace_charAt(str,pos,char) ~ str -# Replace character of a string at given pos. -# +# text.replace_charAt(str,pos,char) -> str +# Replace character of a string at given pos. # Args: -# - str (str): takes string as arg. -# - pos (int): takes position. -# - char (char): takes charachter. +# str (str) > takes string as arg. +# pos (int) > takes position. +# char (char) > takes charachter. text.replace_charAt(){ - # checking args given or not. - [[ ${#} -eq 3 ]] || - { echo "error: 'missing args'" && return 1; }; local String="${1}"; local Pos="${2}"; local Char="${3}"; - # Substring before the pos. + # checking args given or not. + if [[ ! ${#} -eq 3 ]]; then + echo "error: 'missing args'"; + return 1; + fi + # Extract the substring before the pos local PreString="${String:0:Pos-1}"; - # Substring after the pos. + # Extract the substring after the pos local PostString="${String:Pos}"; - # Concatenate substrings with replace character. + # Concatenate the substrings with the replacement character echo "${PreString}${Char}${PostString}"; } diff --git a/src/url.sh b/src/url.sh index 16e2cd5..ce0ddc6 100644 --- a/src/url.sh +++ b/src/url.sh @@ -17,14 +17,12 @@ source "${Dir}"/inspect.sh # source spinner.sh # source inspect.sh -# url.contentSize(url) ~ int -# Gives you size of content file in MiB. -# +# url.contentSize(url) -> int +# Gives you size of content file in MiB. # Args: -# - url (str): takes url as string. -# +# url (str) > takes url as string. # Returns: -# - size (int): size in MiB (eg: 60). +# size (int) > size in MiB (eg: 60). url.contentSize(){ # checking required functions. inspect.is_func 'wget'; @@ -33,12 +31,11 @@ url.contentSize(){ echo "$(( ContentSize/1048576 ))"; } -# url.contentChart(urls,@paths) -# This is used to chart urls and size. -# +# url.contentChart(urls,*paths) +# This is used to chart urls and size. # Args: -# - urls (array): takes one or array of urls. -# - paths (array,optional): takes file paths. +# urls (array) > takes one or array of urls. +# paths (array) > Optional: takes file paths. url.contentChart(){ inspect.ScreenSize '50' '12'; local ARGs=("${@}") @@ -69,12 +66,11 @@ url.contentChart(){ return 0; } -# url.getContent(urls,@paths) -# This is used to get files via urls. -# +# url.getContent(urls,*paths) +# This is used to get files via urls. # Args: -# - urls (array): takes one or array of url. -# - paths (array,optional): takes files path. +# urls (array) > takes one or array of url. +# paths (array) > Optional: takes files path. url.getContent(){ local ARGs=("${@}") url.contentChart "${ARGs[@]}"; From 879f91dac82a02dad1336390359df9d94936590f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?h=CE=B1rsh=20v=CE=B1ir=CE=B1gi?= Date: Mon, 5 Feb 2024 12:13:21 +0530 Subject: [PATCH 18/18] say module updated --- src/say.sh | 152 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 89 insertions(+), 63 deletions(-) diff --git a/src/say.sh b/src/say.sh index 7f881ff..939017d 100644 --- a/src/say.sh +++ b/src/say.sh @@ -3,114 +3,140 @@ Clear="\033[0m"; # Suppressed Variables if (( 1<2 )); then -# Colors -Gora="\033[1;97m" +# ---------- +# Colors. +# Gora="\033[0;97m" +# ---------- # BG Colors. -BGRed="\033[1;41m"; -BGGreen="\033[1;42m"; -BGYelo="\033[1;43m"; +# BGRed="\033[1;41m"; +# BGGreen="\033[1;42m"; +# BGYelo="\033[1;43m"; +# BGBold="\033[1m"; +# ---------- # Status Colors. -StatusRed="${BGRed}${Gora}"; -StatusGreen="${BGGreen}${Gora}"; -StatusYelo="${BGYelo}${Gora}"; +# StatusRed="${BGRed}${Gora}"; +# StatusGreen="${BGGreen}${Gora}"; +# StatusYelo="${BGYelo}${Gora}"; +# ---------- # Global variables. -Source="'([^']*)'"; +Pattern="'([^']*)'"; +# Color plate. +COLOR_STRIP=( + "\033[0;31m" # Red + "\033[0;32m" # Green + "\033[0;33m" # Yelo + "\033[0;34m" # Blue + "\033[0;35m" # Genta + "\033[0;36m" # Cyan +); fi -# Colors -Red="\033[1;31m"; -Green="\033[1;32m"; -Yelo="\033[1;33m"; - # say.gencolor() # Gives you a random color code. say.gencolor(){ - local STRIP=( - "\033[1;31m" - "\033[1;32m" - "\033[1;33m" - "\033[1;34m" - "\033[1;35m" - "\033[1;36m" - ); - echo "${STRIP[$(( RANDOM % ${#STRIP[@]} ))]}"; - return 0; + echo "${COLOR_STRIP[$(( RANDOM % ${#COLOR_STRIP[@]} ))]}"; } -# say(str) -> str -# Say strings with syntax highlighting feature. -say(){ - local String="${1}"; - local STRIP=(${String}); - local String=''; - for Uri in "${STRIP[@]}" +# _say.colorizeString(str,@--gencolor,@--color ) ~ str +# Create and returns you raw colorized strings. +# Args: +# str (str): takes string as arg. +# --gencolor (obj,optional): adds colors randomly to syntax. +# --color (str,optional): adds given color to syntax. +_say.colorizeString(){ + local ARGString="${1}"; + local isGenColorEnabled=1; + local Sentence=(${ARGString}); + local String; + shift; + case "${1}" in + --gencolor) + local isGenColorEnabled=0; + ;; + --color) + local Color="${2}"; + ;; + esac + for Shabd in "${Sentence[@]}" do - if [[ "${Uri}" =~ ${Source} ]]; then + if [[ "${Shabd}" =~ ${Pattern} ]]; then + [[ "${isGenColorEnabled}" == 1 ]] || \ local Color="$(say.gencolor)"; - local String+="${Color}${Uri}${Clear} "; - else local String+="${Uri} "; + local String+="${Color}${Shabd}${Clear} "; + else + local String+="${Shabd} "; fi done + echo "${String}"; +} + +# say(str) ~ str +# Say strings with syntax highlighting feature. +say(){ + local String="$(_say.colorizeString "${1}" --gencolor)"; echo -e "${String}"; - return 0; } _status(){ case "${1}" in -error) - local Color="\033[1;31m"; - local StatusColor="${StatusRed}"; - ;; - -success) - local Color="\033[1;32m"; - local StatusColor="${StatusGreen}"; - ;; + local Color="\033[0;31m"; + local Header="ERR"; + ;; -warn) - local Color="\033[1;33m"; - local StatusColor="${StatusYelo}"; - ;; + local Color="\033[0;33m"; + local Header="WARN"; + ;; + -success) + local Color="\033[0;32m"; + local Header="INFO"; + ;; esac - local String="${2}"; - local xSTRIP=(${String}); - local String=''; - for Uri in "${xSTRIP[@]}" - do - if [[ "${Uri}" =~ ${Source} ]]; then - local String+="${Color}${Uri}${Clear} "; - else local String+="${Uri} "; - fi - done - echo -e "${StatusColor} INFO ${Clear} ➙ ${String}"; - return 0; + local String="$(_say.colorizeString "${2}" --color "${Color}")"; + echo -ne "[${Color} ${Header} ${Clear}]: "; + echo -e "${String}"; } -# say.error(str) -> str +# say.error(str) ~ str # Says error to terminal. say.error(){ _status -error "${@}"; } -# say.warn(str) -> str +# say.warn(str) ~ str # Says warning to terminal. say.warn(){ _status -warn "${@}"; } -# say.success(str) -> str +# say.success(str) ~ str # Says success to terminal. say.success(){ _status -success "${@}"; } -# say.checkStatus(status) -> str +# log(str) ~ str +# Say log to the user. +log(){ + case "${?}" in + 0) + _status -success "${@}"; + ;; + *) + _status -error "${@}"; + ;; + esac +} + +# say.checkStatus(status) ~ str # This prints success or failure according to exit code. # Args: # status (int) > takes exit code. say.checkStatus(){ if [[ "${1}" == 0 ]]; then - echo -e " ➙ ${StatusGreen} SUCCESS ${Clear}"; + echo -e " ~ [${COLOR_STRIP[1]} SUCCESS ${Clear}]"; else - echo -e " ➙ ${StatusRed} FAILED ${Clear}"; + echo -e " ~ [${COLOR_STRIP[0]} FAILED ${Clear}]"; exit 1; fi }