Skip to content

Commit

Permalink
♻️ using only printf in the core
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Apr 25, 2024
1 parent 1777e58 commit c5d7ec8
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 65 deletions.
4 changes: 0 additions & 4 deletions tests.d/1003-lib-string/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,15 @@ Exit code: `0`
2=2
→ string::indexOf 'hello' 'he'
yeah
0=0
→ string::indexOf 'hello' 'he' 10
nop
-1=-1
→ string::indexOf 'yesyes' 'ye' 1
3=3
→ string::indexOf 'yesyes' 'yes' 3
yeah
3=3
→ string::indexOf 'yesyes' 'yes' 5
Expand Down Expand Up @@ -159,7 +156,6 @@ llo=⌈llo⌉
=⌈⌉
→ string::extractBetween 'hello' 'h' 'a'
yeah
=⌈⌉
multilinetext="1 line one
Expand Down
2 changes: 1 addition & 1 deletion tests.d/1300-valet-cli/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Exit code: `0`

```log
INFO This command requires sudo privileges.
▶ called sudo echo alright
▶ called sudo printf %s alright
▶ called sudo whoami
```

Expand Down
2 changes: 1 addition & 1 deletion tests.d/1301-profiler/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ D I S timer delta source:line function
00 00 00 0.0XXX 0.0XXX self-mock.sh:154 selfMock2() → log::info 'More: arg2.'
00 00 00 0.0XXX 0.0XXX self-mock.sh:156 selfMock2() → aSubFunctionInselfMock2
00 00 00 0.0XXX 0.0XXX self-mock.sh:163 aSubFunctionInselfMock2() → log::debug 'This is a sub function.'
00 00 00 0.0XXX 0.0XXX self-mock.sh:158 selfMock2() → echo 'That'\''s it!'
00 00 00 0.0XXX 0.0XXX self-mock.sh:158 selfMock2() → printf '%s\n' 'That'\''s it!'
```

**Error** output:
Expand Down
2 changes: 1 addition & 1 deletion valet
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -Eeu -o pipefail

if [[ ${BASH_VERSINFO[0]:-0} -lt 5 ]]; then
echo "Bash 5 or higher is required to run valet."
printf '%s\n' "Bash 5 or higher is required to run valet."
exit 1
fi

Expand Down
8 changes: 3 additions & 5 deletions valet.d/commands.d/self-build-utils
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,11 @@ function writeCommandDefinitionVariablesToFile() {
local outputFile="${1}"
local content

if [[ ! -d "${outputFile%/*}" ]]; then
mkdir -p "${outputFile%/*}"
fi
io::createFilePathIfNeeded "${outputFile}"

{
# write the header
echo "#!/usr/bin/env bash
printf '%s\n' "#!/usr/bin/env bash
# author: github.com/jcaillon
# description: This script declares global variables used in valet to manage commands.
# shellcheck disable=SC2034
Expand Down Expand Up @@ -288,7 +286,7 @@ _CMD_INCLUDED=1
local line
local IFS=':'
while read -r -d $'\n' line; do
echo "${line#declare -? }"
printf '%s\n' "${line#declare -? }"
done <<< "${LAST_RETURNED_VALUE}"
} >"${outputFile}"
}
2 changes: 1 addition & 1 deletion valet.d/commands.d/self-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function bumpValetBuildVersion() {

string::bumpSemanticVersion "${currentVersion}" "patch" "false"

echo -n "${LAST_RETURNED_VALUE}" >"${versionFile}"
printf '%s' "${LAST_RETURNED_VALUE}" >"${versionFile}"

log::info "The valet build version has been bumped to ⌜${LAST_RETURNED_VALUE}⌝."
}
Expand Down
2 changes: 1 addition & 1 deletion valet.d/commands.d/self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,5 @@ VALET_CONFIG_BUMP_VERSION_ON_BUILD=\"\${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-${EX
"

echo "${valetConfigFileContent}" >"${valetConfigFile}"
printf '%s\n' "${valetConfigFileContent}" >"${valetConfigFile}"
}
6 changes: 3 additions & 3 deletions valet.d/commands.d/self-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

# if not executing in bash, we can stop here
if [[ -z "${BASH_VERSION:-}" ]]; then
echo "❌ This script must be run with bash." 1>&2
printf '%s\n' "❌ This script must be run with bash." 1>&2
exit 0
fi

Expand Down Expand Up @@ -193,8 +193,8 @@ function selfUpdate() {
${SUDO} mkdir -p "${binDirectory}" 1>/dev/null || core::fail "Could not create the bin directory ⌜${binDirectory}⌝."
log::info "Creating a shim ⌜${GLOBAL_VALET_HOME}/valet → ${valetBin}⌝."
{
echo "#!/usr/bin/env bash"
echo -n "'${GLOBAL_VALET_HOME}/valet' \"\$@\""
printf '%s\n' "#!/usr/bin/env bash"
printf '%s' "'${GLOBAL_VALET_HOME}/valet' \"\$@\""
} | ${SUDO} tee -a "${valetBin}" 1>/dev/null
${SUDO} chmod +x "${valetBin}"
fi
Expand Down
4 changes: 2 additions & 2 deletions valet.d/commands.d/self-mock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function selfMock1() {
log::success "This is a success message."
log::warning "This is a warning message."$'\n'"With a second line."
if log::isDebugEnabled; then
echo "The debug mode is activated!" 1>&2
printf '%s\n' "The debug mode is activated!" 1>&2
fi
;;
wait-indefinitely)
Expand Down Expand Up @@ -155,7 +155,7 @@ function selfMock2() {

aSubFunctionInselfMock2

echo "That's it!"
printf '%s\n' "That's it!"
}

function aSubFunctionInselfMock2() {
Expand Down
2 changes: 1 addition & 1 deletion valet.d/commands.d/self-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function createRelease() {

# bump the version
string::bumpSemanticVersion "${version}" "${bumpLevel:-minor}" && newVersion="${LAST_RETURNED_VALUE}"
if [[ "${dryRun:-}" != "true" ]]; then echo -n "${newVersion}" >"${GLOBAL_VALET_HOME}/valet.d/version"; fi
if [[ "${dryRun:-}" != "true" ]]; then printf '%s' "${newVersion}" >"${GLOBAL_VALET_HOME}/valet.d/version"; fi
log::info "The new version of valet is: ${newVersion}."

# commit the new version and push it
Expand Down
22 changes: 11 additions & 11 deletions valet.d/commands.d/self-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ description: |-
function selfSetup() {
log::info "Now setting up Valet."

echo "─────────────────────────────────────"
echo "${VALET_CONFIG_COLOR_INFO:-$'\e'"[0;36m"}This is a COLOR CHECK, this line should be COLORED (in cyan by default).${VALET_CONFIG_COLOR_DEFAULT:-$'\e'"[0m"}"
echo "${VALET_CONFIG_COLOR_SUCCESS:-$'\e'"[0;32m"}This is a COLOR CHECK, this line should be COLORED (in green by default).${VALET_CONFIG_COLOR_DEFAULT:-$'\e'"[0m"}"
echo "─────────────────────────────────────"
printf '%s\n' "─────────────────────────────────────"
printf '%s\n' "${VALET_CONFIG_COLOR_INFO:-$'\e'"[0;36m"}This is a COLOR CHECK, this line should be COLORED (in cyan by default).${VALET_CONFIG_COLOR_DEFAULT:-$'\e'"[0m"}"
printf '%s\n' "${VALET_CONFIG_COLOR_SUCCESS:-$'\e'"[0;32m"}This is a COLOR CHECK, this line should be COLORED (in green by default).${VALET_CONFIG_COLOR_DEFAULT:-$'\e'"[0m"}"
printf '%s\n' "─────────────────────────────────────"

if interactive::promptYesNo "Do you see the colors in the color check above the line?"; then
export VALET_CONFIG_ENABLE_COLORS=true
Expand All @@ -45,13 +45,13 @@ function selfSetup() {
log::createPrintFunction
eval "${GLOBAL_LOG_PRINT_FUNCTION}"

echo "─────────────────────────────────────"
echo "This is a nerd icon check, check out the next lines:"
echo "A cross within a square: ${VALET_CONFIG_ICON_ERROR:-$'\uf2d3'}"
echo "A warning sign: ${VALET_CONFIG_ICON_WARNING:-$'\uf071'}"
echo "A checked box: ${VALET_CONFIG_ICON_SUCCESS:-$'\uf14a'}"
echo "An information icon: ${VALET_CONFIG_ICON_INFO:-$'\uf05a'}"
echo "─────────────────────────────────────"
printf '%s\n' "─────────────────────────────────────"
printf '%s\n' "This is a nerd icon check, check out the next lines:"
printf '%s\n' "A cross within a square: ${VALET_CONFIG_ICON_ERROR:-$'\uf2d3'}"
printf '%s\n' "A warning sign: ${VALET_CONFIG_ICON_WARNING:-$'\uf071'}"
printf '%s\n' "A checked box: ${VALET_CONFIG_ICON_SUCCESS:-$'\uf14a'}"
printf '%s\n' "An information icon: ${VALET_CONFIG_ICON_INFO:-$'\uf05a'}"
printf '%s\n' "─────────────────────────────────────"

if ! interactive::promptYesNo "Do you correctly see the nerd icons in the icon check above the line?"; then
log::info "If you see the replacement character ? in my terminal, it means you don't have a nerd-font setup in your terminal."$'\n'"You can download any font here: https://www.nerdfonts.com/font-downloads and install it."$'\n'"After that, you need to setup your terminal to use this newly installed font."$'\n'"You can also choose to enable the icons in Valet if you plan to install a nerd font."
Expand Down
21 changes: 11 additions & 10 deletions valet.d/commands.d/self-test-utils
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function echoFileWithLineNumberSubstitution() {
line="${line/%:[[:digit:]][[:digit:]][[:digit:]]/:XXX}"
line="${line/%:[[:digit:]][[:digit:]][[:digit:]][[:digit:]]/:XXX}"
fi
echo "${line}"
printf '%s\n' "${line}"
done <"${file}"
}

Expand All @@ -119,7 +119,7 @@ function runCoreTests() {
fi

# we need to rebuild the commands for the core commands only
rebuildCommands ""
rebuildCommands "" ""

# we should always run the test suite from the valet home directory to have consistent paths
# in the report files
Expand All @@ -133,7 +133,7 @@ function runCoreTests() {
log::warning "The valet examples directory ⌜${GLOBAL_VALET_HOME}/examples.d⌝ does not exist, cannot run the tests on the core examples."
else
# we need to rebuild the commands for the examples only
rebuildCommands "${GLOBAL_VALET_HOME}/examples.d"
rebuildCommands "${GLOBAL_VALET_HOME}/examples.d" ""

log::info "Running all test suites in directory ⌜${GLOBAL_VALET_HOME}/examples.d⌝."
runTestSuites "${GLOBAL_VALET_HOME}/examples.d/showcase/tests.d"
Expand All @@ -150,7 +150,11 @@ function rebuildCommands() {
log::getLevel && originalLogLevel="${LAST_RETURNED_VALUE}"
log::setLevel warning true
core::sourceForFunction selfBuild
selfBuild --output "" --user-directory "${1:-}"
if (( $# == 1)); then
selfBuild --user-directory "${1:-}"
else
selfBuild --user-directory "${1:-}" --output "${2}"
fi
log::setLevel "${originalLogLevel}" true
}

Expand Down Expand Up @@ -288,11 +292,8 @@ function runTest() {

# run the test
pushd "${testDirectory}" >/dev/null
# shellcheck disable=SC1091
set +Eeu +o pipefail
# shellcheck disable=SC1090
source "${testScript}"
set -Eeu -o pipefail
popd >/dev/null

# remove the onExitTest function, we can exit safely now
Expand Down Expand Up @@ -332,7 +333,7 @@ function compareWithApproved() {
rm -f "${receivedFile}" 2>/dev/null || :
return 0
else
echo "${receivedFile} is different than ${approvedFile} (see above)" 1>&2
printf '%s\n' "${receivedFile} is different than ${approvedFile} (see above)" 1>&2
log::error "🧪 ${testName}: KO, differs from approved file (see difference above)." | tee >(cat >&2)
fi

Expand Down Expand Up @@ -476,7 +477,7 @@ function setSimplifiedImportantVariables() {
"
}

# This function is used to echo the content of a file with some substitutions.
# This function is used to print the content of a file with some substitutions.
# The substitutions are:
# - replace the GLOBAL_VALET_HOME with $GLOBAL_VALET_HOME
# - replace the current test directory with a dot
Expand Down Expand Up @@ -505,6 +506,6 @@ function echoFileSubstitutingPath() {
if [[ ${line} =~ "after "[0-9]{1,}s.$ ]]; then
line="${line/%after */after Xs.}"
fi
echo -n "${line}"
printf '%s' "${line}"
done <"${file}"
}
4 changes: 2 additions & 2 deletions valet.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function log::setLevel() {
# the current log level in the global variable LAST_RETURNED_VALUE.
#
# Usage:
# log::getLevel && echo "The log level is ⌜${LAST_RETURNED_VALUE}⌝."
# log::getLevel && printf '%s\n' "The log level is ⌜${LAST_RETURNED_VALUE}⌝."
function log::getLevel() {
LAST_RETURNED_VALUE="${GLOBAL_LOG_LEVEL:-info}"
}
Expand Down Expand Up @@ -371,7 +371,7 @@ function log::debug() {
# Returns 0 if debug mode is active, 1 otherwise.
#
# Usage:
# log::isDebugEnabled && echo "Debug mode is active."
# log::isDebugEnabled && printf '%s\n' "Debug mode is active."
function log::isDebugEnabled() {
if [[ ${LOG_LEVEL_INT:-1} -eq 0 ]]; then
return 0
Expand Down
10 changes: 5 additions & 5 deletions valet.d/lib-array
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -Eeu -o pipefail
# Usage:
# declare -g myArray=( "z" "a" "b" )
# array::sort myArray
# echo "${myArray[@]}"
# printf '%s\n' "${myArray[@]}"
function array::sort() {
local -n array=${1}
local -i i j
Expand Down Expand Up @@ -39,7 +39,7 @@ function array::sort() {
# Usage:
# declare -g myArray=( "a" "b" )
# array::appendIfNotPresent myArray "c"
# echo "${myArray[@]}"
# printf '%s\n' "${myArray[@]}"
function array::appendIfNotPresent() {
local -n array=${1}
local value="${2}"
Expand Down Expand Up @@ -70,7 +70,7 @@ function array::appendIfNotPresent() {
#
# Usage:
# declare -g myArray=( "a" "b" )
# array::isInArray myArray "b" && echo "b is in the array"
# array::isInArray myArray "b" && printf '%s\n' "b is in the array"
function array::isInArray() {
local -n array="${1}"
local value="${2}"
Expand Down Expand Up @@ -137,9 +137,9 @@ function array::makeArraysSameSize() {
# declare -g criteria1=( 3 2 2 )
# declare -g criteria2=( 1 3 2 )
# array::sortWithCriteria myArray criteria1 criteria2
# echo "${myArray[@]}"
# printf '%s\n' "${myArray[@]}"
# > c b a
# echo "${LAST_RETURNED_ARRAY[@]}"
# printf '%s\n' "${LAST_RETURNED_ARRAY[@]}"
# > 3 2 1
function array::sortWithCriteria() {
local -n array="${1}"
Expand Down
2 changes: 1 addition & 1 deletion valet.d/lib-interactive
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ interactive::displayDialogBox() {
# Usage:
# interactive::testKeys
function interactive::testKeys() {
echo "Press any key to see the character that bash sees (CTRL + C to interrupt).${AC__CURSOR_HIDE}"
printf '%s\n' "Press any key to see the character that bash sees (CTRL + C to interrupt).${AC__CURSOR_HIDE}"
while true; do
if interactive::waitForKey -t 1; then
printf "%sYou pressed: ⌜%q⌝%s" "${AC__ERASE_LINE}" "${LAST_KEY_PRESSED@E}" "${AC__CURSOR_MOVE__}1${__AC__COLUMN}"
Expand Down
8 changes: 4 additions & 4 deletions valet.d/lib-io
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function io::invoke() {
# Otherwise it hides both streams, effectively rendering the execution silent unless it fails.
#
# This is the equivalent of:
# myvar="$(echo "mystring" | mycommand)"
# myvar="$(printf '%s\n' "mystring" | mycommand)"
# In windows, this is tremendously faster that the example above.
# On linux, it is slighly faster (but it might be slower if you don't have SSD?).
# On linux, you can use a tmpfs directory for massive gains over subshells.
Expand Down Expand Up @@ -377,10 +377,10 @@ function io::sleep() {
io::createTempFile && IO_SLEEP_FIFO="${LAST_RETURNED_VALUE}"
rm "${IO_SLEEP_FIFO}"
mkfifo -m 700 "${IO_SLEEP_FIFO}"
exec 9<>"${IO_SLEEP_FIFO}"
exec 6<>"${IO_SLEEP_FIFO}"
rm "${IO_SLEEP_FIFO}"
fi
read -rt "${timeout}" -u 9
read -rt "${timeout}" -u 6
else
# when everything else failed...
sleep "${timeout}"
Expand All @@ -396,7 +396,7 @@ function io::sleep() {
# io::cat "myFile"
function io::cat() {
io::readFile "${1}"
echo "${LAST_RETURNED_VALUE}"
printf '%s\n' "${LAST_RETURNED_VALUE}"
}

# Read the content of the standard input.
Expand Down
4 changes: 1 addition & 3 deletions valet.d/lib-string
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set -Eeu -o pipefail
#
# Usage:
# string::cutField "field1 field2 field3" 1 " " && local field="${LAST_RETURNED_VALUE}"
# echo "${field}" # will output "field2"
# printf '%s' "${field}" # will output "field2"
#
# Notes:
# This is faster than:
Expand Down Expand Up @@ -217,7 +217,6 @@ function string::indexOf() {
# if the starting index is greater than the length of the string, we return -1
if [[ startingIndex -ge stringLength ]]; then
LAST_RETURNED_VALUE=-1
echo "nop"
return 0
fi

Expand All @@ -229,7 +228,6 @@ function string::indexOf() {

# if the string starts with the search, we have our index
if [[ ${string} == "${search}"* ]]; then
echo "yeah"
LAST_RETURNED_VALUE=${startingIndex}
return 0
fi
Expand Down
Loading

0 comments on commit c5d7ec8

Please sign in to comment.