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 1/3] 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 2/3] 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 3/3] 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 }