Skip to content

Commit

Permalink
✨ implementing more interactive functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Apr 13, 2024
1 parent 2a88760 commit 5c8b905
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 282 deletions.
21 changes: 18 additions & 3 deletions docs/working-on-bash-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ To write performance script, you should:
- Avoid [subshells](https://tldp.org/LDP/abs/html/subshells.html),
- avoid [pipes](https://tldp.org/LDP/abs/html/special-chars.html#PIPEREF) which also run as [child processes](https://tldp.org/LDP/abs/html/othertypesv.html#CHILDREF).
- If possible, prefer manipulating variables content instead of files content.
- Avoid here string `<<<`.
- Try to avoid here string `<<<` (AFAIK it uses a temporary file behind the scene).

> This improvements can lead to **HUGE** differences in run time. Especially in windows bash which is quite slow.
>
> The initial version of valet was taking around 5s to parse and execute a command, and it went down to a few hundred milliseconds after refactoring using the rules above.
The chapters below give you some tips for the most common
The chapters below give you some tips for the common problems that you can encounter:

### Call and get the output of a function

Expand Down Expand Up @@ -184,4 +184,19 @@ done

IFS can be set as a `local` variable, and it can be any character.

However, you will not go through lines that are empty. You will need to keep the `while read` if you need them.
However, you will not go through lines that are empty. You will need to keep the `while read` if you need them.

### Pass string to stdin of a program or function

Instead of a pipe:

```bash
echo y | doSomething
```

do:

```bash
echo y 1> myfile
doSomething <myfile
```
4 changes: 2 additions & 2 deletions tests.d/1002-lib-kurl/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,14 @@ stack:
├─ In function testKurl::toVar() $GLOBAL_VALET_HOME/tests.d/1002-lib-kurl/00.kurl.sh:73
├─ In function main() $GLOBAL_VALET_HOME/tests.d/1002-lib-kurl/00.kurl.sh:101
├─ In function source() $GLOBAL_VALET_HOME/tests.d/1002-lib-kurl/00.kurl.sh:131
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:451
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:443
├─ In function runTest() valet.d/commands.d/self-test-utils:241
├─ In function runTestSuites() valet.d/commands.d/self-test-utils:195
├─ In function runCoreTests() valet.d/commands.d/self-test-utils:107
├─ In function selfTest() valet.d/commands.d/self-test.sh:110
├─ In function main::runFunction() $GLOBAL_VALET_HOME/valet.d/main:585
├─ In function main::parseMainArguments() $GLOBAL_VALET_HOME/valet.d/main:537
└─ In function main() ./valet:99
└─ In function main() $GLOBAL_VALET_HOME/valet:99
```

### Testing kurl, debug mode, with content http code 400
Expand Down
4 changes: 2 additions & 2 deletions tests.d/1005-lib-io/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,14 @@ stack:
├─ In function testIo::invoke() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:61
├─ In function main() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:115
├─ In function source() $GLOBAL_VALET_HOME/tests.d/1005-lib-io/01.invoke.sh:120
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:451
├─ In function source() $GLOBAL_VALET_HOME/valet.d/core:443
├─ In function runTest() valet.d/commands.d/self-test-utils:241
├─ In function runTestSuites() valet.d/commands.d/self-test-utils:195
├─ In function runCoreTests() valet.d/commands.d/self-test-utils:107
├─ In function selfTest() valet.d/commands.d/self-test.sh:110
├─ In function main::runFunction() $GLOBAL_VALET_HOME/valet.d/main:585
├─ In function main::parseMainArguments() $GLOBAL_VALET_HOME/valet.d/main:537
└─ In function main() ./valet:99
└─ In function main() $GLOBAL_VALET_HOME/valet:99
```

### Testing io::invoke, output to var
Expand Down
8 changes: 5 additions & 3 deletions tests.d/1007-lib-interactive/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ source interactive

function testInteractive::promptYesNo() {
echo "echo y | interactive::promptYesNo 'Do you see this message?'"
echo y | interactive::promptYesNo 'Do you see this message?'
echo y 1>"${GLOBAL_TEMPORARY_WORK_FILE}"
interactive::promptYesNo 'Do you see this message?' <"${GLOBAL_TEMPORARY_WORK_FILE}"

endTest "test interactive::promptYesNo with yes" 0

echo
echo "echo n | interactive::promptYesNo 'Do you see this message?'"
echo n | interactive::promptYesNo 'Do you see this message?' || true
echo n 1>"${GLOBAL_TEMPORARY_WORK_FILE}"
interactive::promptYesNo 'Do you see this message?' <"${GLOBAL_TEMPORARY_WORK_FILE}" || true

endTest "Testing interactive::promptYesNo" 1
}

function testInteractive::askForConfirmation() {
echo "echo y | interactive::askForConfirmation 'Please press OK.'"
echo y | interactive::askForConfirmation 'Please press OK.'
interactive::askForConfirmation 'Please press OK.' <"${GLOBAL_TEMPORARY_WORK_FILE}"

endTest "test interactive::askForConfirmation with yes" 0
}
Expand Down
27 changes: 27 additions & 0 deletions tests.d/1007-lib-interactive/01.utilities.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

# shellcheck source=../../valet.d/lib-interactive
source interactive

function testInteractive::createSpace() {
echo "interactive::createSpace 5"
interactive::createSpace 5

endTest "Testing interactive::createSpace" 0
}

function testInteractive::getCursorPosition() {
echo "printf '\e[%sR' '123;456' | interactive::getCursorPosition"
printf '\e[%sR' '123;456' 1>"${GLOBAL_TEMPORARY_WORK_FILE}"
interactive::getCursorPosition < "${GLOBAL_TEMPORARY_WORK_FILE}"
echo "CURSOR_LINE: ${CURSOR_LINE}; CURSOR_COLUMN: ${CURSOR_COLUMN}"

endTest "Testing interactive::getCursorPosition" 0
}

function main() {
testInteractive::createSpace
testInteractive::getCursorPosition
}

main
36 changes: 33 additions & 3 deletions tests.d/1007-lib-interactive/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ echo y | interactive::promptYesNo 'Do you see this message?'
 ┌──┐
 │ Do you see this message? │
 └──┘\
[?25l (Y)ES   (N)O [?25h ┌──┐

[?25l (Y)ES  (N)O [?25h ┌──┐
 │ Yes. │
/└──┘
```
Expand All @@ -30,7 +31,8 @@ echo n | interactive::promptYesNo 'Do you see this message?'
 ┌──┐
 │ Do you see this message? │
 └──┘\
[?25l (Y)ES   (N)O [?25h ┌──┐

[?25l (Y)ES  (N)O [?25h ┌──┐
 │ No. │
/└──┘
```
Expand All @@ -46,8 +48,36 @@ echo y | interactive::askForConfirmation 'Please press OK.'
 ┌──┐
 │ Please press OK. │
 └──┘\
[?25l (O)K [?25h ┌──┐

[?25l (O)K [?25h ┌──┐
 │ Ok. │
/└──┘
```

## Test script 01.utilities

### Testing interactive::createSpace

Exit code: `0`

**Standard** output:

```plaintext
interactive::createSpace 5

```

### Testing interactive::getCursorPosition

Exit code: `0`

**Standard** output:

```plaintext
printf '\e[%sR' '123;456' | interactive::getCursorPosition
CURSOR_LINE: 123; CURSOR_COLUMN: 456
```

16 changes: 16 additions & 0 deletions tests.d/1100-self-config/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ VALET_CONFIG_COLOR_COMMAND="${VALET_CONFIG_COLOR_COMMAND:-}"
VALET_CONFIG_COLOR_ACTIVE_BUTTON="${VALET_CONFIG_COLOR_ACTIVE_BUTTON:-}"
VALET_CONFIG_COLOR_UNACTIVE_BUTTON="${VALET_CONFIG_COLOR_UNACTIVE_BUTTON:-}"
# -----------
# Other configs.
# -----------
# If true, will enable the automatic bump of the version of Valet on build.
# Intended for Valet developers only.
VALET_CONFIG_BUMP_VERSION_ON_BUILD="${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-}"
```
Expand Down Expand Up @@ -338,6 +346,14 @@ VALET_CONFIG_COLOR_COMMAND="${VALET_CONFIG_COLOR_COMMAND:-}"
VALET_CONFIG_COLOR_ACTIVE_BUTTON="${VALET_CONFIG_COLOR_ACTIVE_BUTTON:-}"
VALET_CONFIG_COLOR_UNACTIVE_BUTTON="${VALET_CONFIG_COLOR_UNACTIVE_BUTTON:-}"
# -----------
# Other configs.
# -----------
# If true, will enable the automatic bump of the version of Valet on build.
# Intended for Valet developers only.
VALET_CONFIG_BUMP_VERSION_ON_BUILD="${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-}"
```
Expand Down
10 changes: 6 additions & 4 deletions tests.d/1104-self-setup/00.self-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ function testselfSetup() {
function curl() { return 0; }
export -f awk diff curl

echo "→ echo nny | selfSetup"
echo nny | selfSetup && exitCode=0 || exitCode=$?
echo "→ echo nnn | selfSetup"
echo nnn 1>"${GLOBAL_TEMPORARY_WORK_FILE}"
selfSetup <"${GLOBAL_TEMPORARY_WORK_FILE}" && exitCode=0 || exitCode=$?
endTest "Testing selfSetup 1" ${exitCode}

rm -f "${configFile}"
unset -f awk diff curl
local originalPath="${PATH}"
export PATH=""

echo "→ echo nny | selfSetup"
echo yyo | selfSetup && exitCode=0 || exitCode=$?
echo "→ echo yyo | selfSetup"
echo yyo 1>"${GLOBAL_TEMPORARY_WORK_FILE}"
selfSetup <"${GLOBAL_TEMPORARY_WORK_FILE}" && exitCode=0 || exitCode=$?
endTest "Testing selfSetup 2" ${exitCode}

export PATH="${originalPath}"
Expand Down
36 changes: 21 additions & 15 deletions tests.d/1104-self-setup/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ Exit code: `0`
**Standard** output:

```plaintext
→ echo nny | selfSetup
→ echo nnn | selfSetup
─────────────────────────────────────
CINThis is a COLOR CHECK, this line should be COLORED (in cyan by default).CDE
CSUThis is a COLOR CHECK, this line should be COLORED (in green by default).CDE
─────────────────────────────────────
 ┌──┐
 │ Do you see the colors in the color check above the line? │
 └──┘\
[?25l (Y)ES   (N)O [?25h ┌──┐

[?25l (Y)ES  (N)O [?25h ┌──┐
 │ No. │
/└──┘
─────────────────────────────────────
Expand All @@ -30,15 +31,17 @@ An information icon: II
 ┌──┐
 │ Do you correctly see the nerd icons in the icon check above the line? │
 └──┘\
[?25l (Y)ES   (N)O [?25h ┌──┐

[?25l (Y)ES  (N)O [?25h ┌──┐
 │ No. │
/└──┘
 ┌──┐
 │ Do you want to enable the icons in Valet? │
 └──┘\
[?25l (Y)ES   (N)O [?25h ┌──┐
 │ Yes. │
/└──┘

[?25l (Y)ES  (N)O [?25h ┌──┐
 │ No. │
/└──┘
```

**Error** output:
Expand All @@ -49,13 +52,13 @@ INFO If you see the replacement character ? in my terminal, it means you don
You can download any font here: https://www.nerdfonts.com/font-downloads and install it.
After that, you need to setup your terminal to use this newly installed font.
You can also choose to enable the icons in Valet if you plan to install a nerd font.
INFO II Creating the valet config file ⌜/tmp/valet.d/f801-0⌝.
SUCCESS IS You are all set!
INFO II As a reminder, you can modify the configuration done during this set up by either:
INFO Creating the valet config file ⌜/tmp/valet.d/f801-0⌝.
SUCCESS You are all set!
INFO As a reminder, you can modify the configuration done during this set up by either:
- replaying the command ⌜valet self setup⌝,
- running the command ⌜valet self config⌝.
INFO II Run ⌜valet --help⌝ to get started.
INFO II You can create your own commands and have them available in valet, please check ⌜https://github.com/jcaillon/valet/blob/main/docs/create-new-command.md⌝ to do so.
INFO Run ⌜valet --help⌝ to get started.
INFO You can create your own commands and have them available in valet, please check ⌜https://github.com/jcaillon/valet/blob/main/docs/create-new-command.md⌝ to do so.
```

### Testing selfSetup 2
Expand All @@ -65,15 +68,16 @@ Exit code: `0`
**Standard** output:

```plaintext
→ echo nny | selfSetup
→ echo yyo | selfSetup
─────────────────────────────────────
CINThis is a COLOR CHECK, this line should be COLORED (in cyan by default).CDE
CSUThis is a COLOR CHECK, this line should be COLORED (in green by default).CDE
─────────────────────────────────────
 ┌──┐
 │ Do you see the colors in the color check above the line? │
 └──┘\
[?25l (Y)ES   (N)O [?25h ┌──┐

[?25l (Y)ES  (N)O [?25h ┌──┐
 │ Yes. │
/└──┘
─────────────────────────────────────
Expand All @@ -86,13 +90,15 @@ An information icon: II
 ┌──┐
 │ Do you correctly see the nerd icons in the icon check above the line? │
 └──┘\
[?25lCAB (Y)ES  CUB (N)O [?25h ┌──┐

[?25lCAB (Y)ES  CUB (N)O [?25h ┌──┐
 │ Yes. │
/└──┘
 ┌──┐
 │ Did you read the warnings above? │
 └──┘\
[?25lCAB (O)K [?25h ┌──┐

[?25lCAB (O)K [?25h ┌──┐
 │ Ok. │
/└──┘
```
Expand Down
7 changes: 6 additions & 1 deletion valet.d/commands.d/self-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ function selfBuild() {
writeCommandDefinitionVariablesToFile "${outputFile}"
log::info "The command definition variables have been written to ⌜${outputFile}⌝."
fi
bumpValetBuildVersion

if [[ ${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-false} == "true" ]]; then
bumpValetBuildVersion
fi

log::success "The valet user commands have been successfully built"
}
Expand Down Expand Up @@ -187,6 +190,8 @@ function bumpValetBuildVersion() {
string::bumpSemanticVersion "${currentVersion}" "patch" "false"

echo -n "${LAST_RETURNED_VALUE}" >"${versionFile}"

log::info "The valet build version has been bumped to ⌜${LAST_RETURNED_VALUE}⌝."
}

# This function extracts the command definitions from the files and stores them in
Expand Down
8 changes: 8 additions & 0 deletions valet.d/commands.d/self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ VALET_CONFIG_COLOR_COMMAND=\"\${VALET_CONFIG_COLOR_COMMAND:-${EXPORTED_VALET_CON
VALET_CONFIG_COLOR_ACTIVE_BUTTON=\"\${VALET_CONFIG_COLOR_ACTIVE_BUTTON:-${EXPORTED_VALET_CONFIG_COLOR_ACTIVE_BUTTON:-}}\"
VALET_CONFIG_COLOR_UNACTIVE_BUTTON=\"\${VALET_CONFIG_COLOR_UNACTIVE_BUTTON:-${EXPORTED_VALET_CONFIG_COLOR_UNACTIVE_BUTTON:-}}\"
# -----------
# Other configs.
# -----------
# If true, will enable the automatic bump of the version of Valet on build.
# Intended for Valet developers only.
VALET_CONFIG_BUMP_VERSION_ON_BUILD=\"\${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-${EXPORTED_VALET_CONFIG_BUMP_VERSION_ON_BUILD:-}}\"
"

echo "${valetConfigFileContent}" >"${VALET_CONFIG_FILE}"
Expand Down
Loading

0 comments on commit 5c8b905

Please sign in to comment.