Skip to content

Commit 3fec433

Browse files
authored
refactor(core): Large refactor of output functions, creating main() and separating text and ASCII (#19)
* flag(ASCII): Added flags -L/-T; "logo only" or "text only" output modes. * Fix formatting * Attempting to fix tests * Debugging Ubuntu tests * Fixing tests.yaml for new flag * Adding back a function that got deleted, fixed logo padding
1 parent 12b598f commit 3fec433

File tree

2 files changed

+104
-87
lines changed

2 files changed

+104
-87
lines changed

.github/tests/ubuntu.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ errorOut () {
1212
}
1313

1414

15-
if [[ $(bash fetch --config sample.config.conf -v -L .) ]]; then
15+
if [[ $(bash fetch --config sample.config.conf -v -C .) ]]; then
1616
successOut 'Fetched current output:'
17-
bash fetch --config sample.config.conf -v
17+
bash fetch --config sample.config.conf -C . -v
1818
else
1919
errorOut 'fetch failed output:'
20-
bash fetch --config sample.config.conf -v
20+
bash fetch --config sample.config.conf -C . -v
2121
exit 1
2222
fi
2323

24+
# shellcheck disable=SC1094
2425
source ./ascii/ubuntu.sh
2526

2627
n=0
2728
IFS=$'\n'
2829
while read -r line; do
2930
_output[${n}]="${line}"
3031
((n++))
31-
done <<< "$(bash fetch --config sample.config.conf -v -L .)"
32+
done <<< "$(bash fetch --config sample.config.conf -v -C .)"
3233

3334
if [[ ! ${_output[0]} =~ ^(.*)'Finding kernel...found as'(.*)'Linux '[[:digit:]]+'.'[[:digit:]]+'.'[[:digit:]]+'-'[[:digit:]]+'-azure' ]]; then
3435
errorOut "!! Failed on kernel."

fetch

Lines changed: 99 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,14 @@ detect_distro() {
431431
done
432432
if [ -n "${distrib_id}" ]; then
433433
if [ "${BASH_VERSINFO[0]}" -ge 4 ]; then
434-
distrib_id=$(for i in ${distrib_id}; do printf '%s' "${i^} "; done)
434+
distrib_id=$(for d in ${distrib_id}; do printf '%s' "${d^} "; done)
435435
my_distro=${distrib_id% }
436436
unset distrib_id
437437
else
438438
distrib_id=$(
439-
for i in ${distrib_id}; do
440-
FIRST_LETTER=$(printf '%s' "${i:0:1}" | tr "[:lower:]" "[:upper:]")
441-
printf '%s' "${FIRST_LETTER}${i:1} "
439+
for d in ${distrib_id}; do
440+
FIRST_LETTER=$(printf '%s' "${d:0:1}" | tr "[:lower:]" "[:upper:]")
441+
printf '%s' "${FIRST_LETTER}${d:1} "
442442
done
443443
)
444444
my_distro=${distrib_id% }
@@ -1395,120 +1395,145 @@ detect_memory() {
13951395

13961396
###############################
13971397
# functions: output
1398-
info() {
1399-
local _info="${1}"
1400-
local _info_disp="my_${_info}"
1401-
1402-
if [ -n "${!_info_disp}" ]; then
1403-
_info_subtitle="config_${_info}[subtitle]"
1398+
print_info() {
1399+
local _info="my_${1}"
1400+
local _infodisplay="my_${1}"
1401+
until [ -n "${!_info}" ]; do
1402+
((${#_display} < 1)) && break
1403+
_display=("${_display[@]:1}")
1404+
_info="my_${1}}"
1405+
done
1406+
1407+
if [ -n "${!_info}" ]; then
1408+
_info_subtitle="config_${1}[subtitle]"
14041409
if [ -n "${!_info_subtitle}" ]; then
14051410
# shellcheck disable=SC2154
1406-
printf '%b\n' "${!_info_subtitle}${config_text[info_separator]} ${!_info_disp}"
1411+
printf '%b\n' "${!_info_subtitle}${config_text[info_separator]} ${!_info}"
14071412
else
1408-
printf '%b\n' "${!_info_disp}"
1413+
printf '%b\n' "${!_info}"
14091414
fi
14101415
else
14111416
:
14121417
fi
1413-
}
1414-
1415-
format_ascii() {
1416-
local _logo="${1}"
14171418

1419+
_display=("${_display[@]:1}")
1420+
}
1421+
print_ascii() {
1422+
local logo="${1}"
1423+
# shellcheck disable=SC2154
1424+
gap=${config_ascii[gap]}
14181425
# Calculate: (max detected logo width - length of current line)
1419-
_tmp="${_logo//\$\{??\}/}"
1426+
_tmp="${logo//\$\{??\}/}"
14201427
_tmp=${_tmp//\\\\/\\}
14211428
_tmp=${_tmp/// }
1429+
# shellcheck disable=SC2154
14221430
if ((${#_tmp} < ascii_len)); then
14231431
logo_padding=$((ascii_len - ${#_tmp}))
14241432
else
14251433
logo_padding=0
14261434
fi
14271435

1436+
# Random coloring support
14281437
if [ "${config_ascii['colors']}" == "random" ]; then
14291438
declare _randc
14301439
local n=1
1431-
while [[ ${_logo} =~ \$\{[c][1-6]\} ]]; do
1440+
while [[ ${logo} =~ \$\{[c][1-6]\} ]]; do
14321441
_randc="$(getColor 'rand')"
1433-
_logo="${_logo//\$\{c${n}\}/${_randc}}"
1442+
logo="${logo//\$\{c${n}\}/${_randc}}"
14341443
((n++))
14351444
done
14361445
fi
14371446

14381447
# Expand color variables
1439-
_logo="${_logo//\$\{c1\}/$c1}"
1440-
_logo="${_logo//\$\{c2\}/$c2}"
1441-
_logo="${_logo//\$\{c3\}/$c3}"
1442-
_logo="${_logo//\$\{c4\}/$c4}"
1443-
_logo="${_logo//\$\{c5\}/$c5}"
1444-
_logo="${_logo//\$\{c6\}/$c6}"
1445-
1446-
((text_padding = logo_padding + gap))
1447-
printf "%b \e[%sC" "${_logo}" "${text_padding}"
1448-
}
1449-
1450-
print_ascii() {
1451-
# shellcheck disable=SC2154
1452-
while IFS=$'\n' read -r line; do
1453-
line=${line//\\\\/\\}
1454-
line=${line/// }
1455-
((++lines, ${#line} > ascii_len)) && ascii_len="${#line}"
1456-
done <<< "${asciiLogo//\$\{??\}/}"
1457-
1458-
n=0
1459-
# shellcheck disable=SC2154
1460-
read -r -a _display <<< "${config_global[info]}"
1461-
while IFS=$'\n' read -r line; do
1462-
# shellcheck disable=SC2154
1463-
gap=${config_ascii[gap]}
1464-
1465-
# call format_ascii to prepare for info display
1466-
line=$(format_ascii "${line}")
1448+
logo="${logo//\$\{c1\}/$c1}"
1449+
logo="${logo//\$\{c2\}/$c2}"
1450+
logo="${logo//\$\{c3\}/$c3}"
1451+
logo="${logo//\$\{c4\}/$c4}"
1452+
logo="${logo//\$\{c5\}/$c5}"
1453+
logo="${logo//\$\{c6\}/$c6}"
1454+
1455+
# Let's output!
1456+
if [ "${config_text[display]}" == "on" ]; then
1457+
# Format line with gap after ASCII for info display
1458+
((text_padding = logo_padding + gap))
1459+
logo="$(printf "%b \e[%sC" "${logo}" "${text_padding}")"
14671460

14681461
# Display ASCII art and detected info
14691462
# shellcheck disable=SC2154
1470-
if [ ${n} -lt "${startline}" ]; then
1471-
printf '%b\n' "${line}${reset}"
1472-
elif [ ${n} -ge "${startline}" ]; then
1463+
if [ "${i}" -lt "${startline}" ]; then
1464+
printf '%b\n' "${logo}${reset}"
1465+
elif [ "${i}" -ge "${startline}" ]; then
14731466
if ((${#_display} > 0)); then
1474-
_info_display="my_${_display[0]}"
1475-
until [ -n "${!_info_display}" ]; do
1476-
((${#_display} <= 0)) && break
1477-
_display=("${_display[@]:1}")
1478-
_info_display="my_${_display[0]}"
1479-
done
1480-
_info_display=$(info "${_display[0]}")
1481-
if [ -n "${_info_display}" ]; then
1482-
printf '%b\n' "${line}${reset}${_info_display}"
1483-
else
1484-
continue
1485-
fi
1467+
_infodisplay="$(print_info "${_display[0]}")"
1468+
printf '%b\n' "${logo}${reset}${_infodisplay}"
14861469
_display=("${_display[@]:1}")
14871470
else
1488-
printf '%b\n' "${line}${reset}"
1471+
printf '%b\n' "${logo}${reset}"
14891472
fi
14901473
fi
14911474

14921475
# Cleanup
1493-
((n++))
1476+
# shellcheck disable=SC2031
14941477
unset _tmp
1495-
unset _padding
1496-
done <<< "${asciiLogo}"
1478+
unset text_padding
1479+
else
1480+
printf '%b\n' "${logo}${reset}"
1481+
fi
14971482

14981483
# Cleanup
14991484
unset n
15001485
}
15011486

15021487
#############################
1503-
# functions: information
1488+
# function: Usage
15041489
usage() {
15051490
printf "Help!\n"
15061491
}
15071492

1493+
##############################
1494+
# function: Version
15081495
versioninfo() {
15091496
printf 'fetch %s\n' "${FETCH_VERSION}"
15101497
}
15111498

1499+
main() {
1500+
detect_kernel
1501+
detect_os
1502+
1503+
# filter {config_global[info]} into a new variable, minus kernel because
1504+
# that is already detected above. keep old variable intact for output purposes.
1505+
GLOBAL_INFO="${config_global[info]//kernel /}"
1506+
1507+
for g in ${GLOBAL_INFO}; do
1508+
eval "detect_${g}"
1509+
done
1510+
1511+
# shellcheck disable=SC2154
1512+
read -r -a _display <<< "${config_global[info]}"
1513+
1514+
if [ "${config_ascii[display]}" == "on" ]; then
1515+
i=0
1516+
# Get logo max width
1517+
# shellcheck disable=SC2154
1518+
while IFS=$'\n' read -r line; do
1519+
line=${line//\\\\/\\}
1520+
line=${line/// }
1521+
((++lines, ${#line} > ascii_len)) && ascii_len="${#line}"
1522+
done <<< "${asciiLogo//\$\{??\}/}"
1523+
# Loop over logo and output
1524+
# shellcheck disable=SC2154
1525+
while IFS=$'\n' read -r line; do
1526+
print_ascii "${line}"
1527+
((i++))
1528+
done <<< "${asciiLogo}"
1529+
unset i
1530+
else
1531+
while ((${#_display} > 0)); do
1532+
print_info "${_display[0]}"
1533+
done
1534+
fi
1535+
}
1536+
15121537
# Catch configuration flag
15131538
[[ $* != *--config* ]] && fetchConfig "${FETCH_DATA_USER_DIR}/${FETCH_CONFIG_FILENAME}"
15141539

@@ -1530,7 +1555,7 @@ case ${1} in
15301555
*) : ;;
15311556
esac
15321557

1533-
while getopts ":hvVNRD:A:L:" flags; do
1558+
while getopts ":hvVNRTLD:A:C:" flags; do
15341559
# shellcheck disable=SC2154
15351560
case ${flags} in
15361561
h)
@@ -1541,11 +1566,13 @@ while getopts ":hvVNRD:A:L:" flags; do
15411566
versioninfo
15421567
exit 0
15431568
;;
1544-
v) declare config_global[verbose]="on" ;;
1569+
v) config_global[verbose]="on" ;;
15451570
D) my_distro="${OPTARG}" ;;
15461571
A) ascii_distro="${OPTARG}" ;;
1547-
N) declare config_text[color]="off" ;;
1548-
L) FETCH_DATA_DIR="${OPTARG}" ;;
1572+
N) config_text[color]="off" ;;
1573+
C) FETCH_DATA_DIR="${OPTARG}" ;;
1574+
T) config_ascii[display]="off" ;;
1575+
L) config_text[display]="off" ;;
15491576
R) config_ascii['colors']="random" ;;
15501577
:)
15511578
errorOut "Error: You're missing an argument somewhere. Exiting."
@@ -1562,17 +1589,6 @@ while getopts ":hvVNRD:A:L:" flags; do
15621589
esac
15631590
done
15641591

1565-
detect_kernel
1566-
detect_os
1567-
1568-
# filter {config_global[info]} into a new variable, minus kernel because
1569-
# that is already detected above. keep old variable intact for output purposes.
1570-
GLOBAL_INFO="${config_global[info]//kernel /}"
1571-
1572-
for i in ${GLOBAL_INFO}; do
1573-
eval "detect_${i}"
1574-
done
1575-
1576-
print_ascii
1592+
main
15771593

15781594
((extglob_set)) && shopt -u extglob

0 commit comments

Comments
 (0)