Skip to content

Commit

Permalink
✨ crude but working fsfs!
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Apr 19, 2024
1 parent bf78dc7 commit a2ae4dc
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 49 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,12 @@ Please check the [CONTRIBUTING.md][contributing] documentation if you intend to
- head / tail from file.
- Allow to regroup single letter options (e.g. -fsSL).
- Add a command self package that build the user commands into a single script file.
- Add vscode extensions [recommendations](https://github.com/imfing/hextra-starter-template/blob/main/.vscode/extensions.json).
- fix running tests with verbose mode on.
- try to make interactive functions usable in colorless mode.
- for yes/no, add param to choose the default option.
- Replace awk with bash.
- Provide an alternative bash function if diff is not found.
- Allow to separate commands from options/arguments with `--`.
- Fsfs: Display the number of lines and the current first line for the right panel.
- Fsfs: improve drawing time; connect the left and right panel with a T.

[releases]: https://github.com/jcaillon/valet/releases
[latest-release]: https://github.com/jcaillon/valet/releases/latest
Expand Down
15 changes: 8 additions & 7 deletions docs/working-on-bash-scripts.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# How to work on bash script

> [!INFORMATION]
> Disclaimer: This page is just one opinion. This is not the best way to work on bash scripts, this is just an explanation on how I work.
> Disclaimer: This page is just one opinion. This is not the best way to work on bash scripts, this is just an explanation of how I work.
## IDE

I work mostly with VS code on windows + WSL.
I work with VS code on windows + WSL.

Install VS code from [here](https://code.visualstudio.com/download).

Expand All @@ -31,16 +31,17 @@ GitHub Copilot is of great help if you can have it.

## VS code settings

You can open your `~/.valet.d` directory as a workspace on vscode. I recommend to copy the `valet.d/core` in your workspace folder and add the following settings in your `~/.valet.d/.vscode/settings.json`:
You can open your `~/.valet.d` directory as a workspace on vscode. I recommend to copy (or link) the `valet.d` directory from the Valet installation to `~/.valet.d/.valet.libs` in your workspace folder and add the following settings in your `~/.valet.d/.vscode/settings.json`:

```json
{
"bashIde.globPattern": "**/*@(.sh|.inc|.bash|.command|core)",
"bashIde.includeAllWorkspaceSymbols": true
// https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
"bashIde.globPattern": "**/*@(.sh|.inc|.bash|.command|core|lib-*)",
"bashIde.includeAllWorkspaceSymbols": true
}
```

This allows you to have autocompletion of the core functions from your command scripts.
This allows you to have autocompletion of the core and libraries functions from your command scripts.

## Where to start your bash journey

Expand All @@ -63,7 +64,7 @@ To write performance script, you should:

> 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 initial version of valet was taking around 5s to parse and execute a command, and it went down to a under a hundred milliseconds after refactoring using the rules above.
The chapters below give you some tips for the common problems that you can encounter:

Expand Down
13 changes: 6 additions & 7 deletions tests.d/1006-lib-array/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function testArray::makeArraysSameSize {
}

function testArray::sortWithCriteria() {
declare -g myArray=(a b c d e f g)
declare -g myArray=(a b c d e f g)
declare -g criteria1=(3 2 2 1 1 4 0)
declare -g criteria2=(1 3 2 5 0 2 9)

Expand All @@ -109,8 +109,7 @@ function testArray::sortWithCriteria() {
echo
echo "→ array::sortWithCriteria myArray criteria1 criteria2"
array::sortWithCriteria myArray criteria1 criteria2

declare -p myArray
declare -p LAST_RETURNED_ARRAY_VALUE myArray
echo "expected: g e d c b a f"

declare -a ARRAY_MATCHES=([0]="one the" [1]="the breakdown" [2]="holding the baby" [3]="the d day")
Expand All @@ -123,7 +122,7 @@ function testArray::sortWithCriteria() {
echo "→ array::sortWithCriteria ARRAY_MATCHES ARRAY_INDEXES ARRAY_DISTANCES"
array::sortWithCriteria ARRAY_MATCHES ARRAY_INDEXES ARRAY_DISTANCES

declare -p ARRAY_MATCHES
declare -p LAST_RETURNED_ARRAY_VALUE ARRAY_MATCHES

unset ARRAY_MATCHES ARRAY_INDEXES ARRAY_DISTANCES

Expand Down Expand Up @@ -151,13 +150,13 @@ function testArray::fuzzyFilterSort() {
echo "→ array::fuzzyFilterSort the myArray"
array::fuzzyFilterSort the myArray

declare -p LAST_RETURNED_ARRAY_VALUE
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2

echo
echo "→ array::fuzzyFilterSort elv myArray ⌜ ⌝"
array::fuzzyFilterSort elv myArray ⌜ ⌝

declare -p LAST_RETURNED_ARRAY_VALUE
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2

echo
myArray=(
Expand All @@ -174,7 +173,7 @@ function testArray::fuzzyFilterSort() {
echo "→ array::fuzzyFilterSort the myArray ⌜ ⌝ 6"
array::fuzzyFilterSort the myArray ⌜ ⌝ 6

declare -p LAST_RETURNED_ARRAY_VALUE
declare -p LAST_RETURNED_ARRAY_VALUE LAST_RETURNED_ARRAY_VALUE2

unset myArray

Expand Down
5 changes: 5 additions & 0 deletions tests.d/1006-lib-array/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ declare -a criteria1=([0]="3" [1]="2" [2]="2" [3]="1" [4]="1" [5]="4" [6]="0")
declare -a criteria2=([0]="1" [1]="3" [2]="2" [3]="5" [4]="0" [5]="2" [6]="9")
→ array::sortWithCriteria myArray criteria1 criteria2
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="6" [1]="4" [2]="3" [3]="2" [4]="1" [5]="0" [6]="5")
declare -a myArray=([0]="g" [1]="e" [2]="d" [3]="c" [4]="b" [5]="a" [6]="f")
expected: g e d c b a f
declare -a ARRAY_MATCHES=([0]="one the" [1]="the breakdown" [2]="holding the baby" [3]="the d day")
declare -a ARRAY_INDEXES=([0]="4" [1]="0" [2]="8" [3]="0")
declare -a ARRAY_DISTANCES=([0]="0" [1]="0" [2]="0" [3]="0")
→ array::sortWithCriteria ARRAY_MATCHES ARRAY_INDEXES ARRAY_DISTANCES
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="1" [1]="3" [2]="0" [3]="2")
declare -a ARRAY_MATCHES=([0]="the breakdown" [1]="the d day" [2]="one the" [3]="holding the baby")
```

Expand All @@ -104,13 +106,16 @@ declare -a myArray=([0]="one the" [1]="the breakdown" [2]="constitutional" [3]="
→ array::fuzzyFilterSort the myArray
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="the breakdown" [1]="the d day" [2]="one the" [3]="hold the baby")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="1" [1]="9" [2]="0" [3]="4")
→ array::fuzzyFilterSort elv myArray ⌜ ⌝
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="⌜e⌝⌜l⌝e⌜v⌝ator" [1]="d⌜e⌝⌜l⌝i⌜v⌝er" [2]="mak⌜e⌝ a ⌜l⌝i⌜v⌝ing")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="10" [1]="6" [2]="8")
declare -a myArray=([0]="On the" [1]="One of the most beautiful" [2]="One of this happy end" [3]="thaerty" [4]="thazrerty")
→ array::fuzzyFilterSort the myArray ⌜ ⌝ 6
declare -a LAST_RETURNED_ARRAY_VALUE=([0]="⌜t⌝⌜h⌝a⌜e⌝r…" [1]="⌜t⌝⌜h⌝azr…" [2]="On ⌜t⌝⌜h⌝…" [3]="…⌜t⌝⌜h⌝⌜e⌝ …" [4]="…⌜t⌝⌜h⌝is…")
declare -a LAST_RETURNED_ARRAY_VALUE2=([0]="3" [1]="4" [2]="0" [3]="1" [4]="2")
```

2 changes: 1 addition & 1 deletion tests.d/1101-self-build-utils/00.self-build-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function testExtractCommandYamls() {
extractCommandYamls "resources/extract-command-yamls-test-file"

local content
for content in "${LAST_RETURNED_VALUE_ARRAY[@]}"; do
for content in "${LAST_RETURNED_ARRAY_VALUE[@]}"; do
echo "content:"$'\n'"${content}"
done

Expand Down
2 changes: 1 addition & 1 deletion tests.d/1102-self-build/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CMD_DESCRIPTION_selfSetup=$'The command run after the installation of Valet to s
CMD_DESCRIPTION_selfTest='Test your valet custom commands using approval tests approach.'
CMD_DESCRIPTION_selfUpdate=$'Update valet using the latest release on GitHub.\n'
CMD_DESCRIPTION_showCommandHelp=$'Show the help this program or of the help of a specific command.\n\nYou can show the help with or without colors and set the maximum columns for the help text.'
CMD_DESCRIPTION_this=$'Valet helps you browse, understand and execute your custom bash commands.\n\nOnline documentation is available at https://github.com/jcaillon/valet.\n\nYou can call valet without any commands to start an interactive session.\n\nExit codes:\n\n- 0: everything went well\n- 1+: an error occured\n\nCreate your own commands:\nYou 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 or the examples under examples.d to do so.\nValet looks for commands in the valet user directory, which default to ~/.valet.d and can be overwritten using an environment variable (see below).\nOnce you have created your new command script, run the valet self build command to update the valet menu.\n\nConfiguration through environment variables:\n\nIn addition to the environment variables defined for each options, you can define environment variables to configure valet.\n\nThese variables are conviently defined in the valet user config file, located by default at ~/.config/valet/config (the path to this file can be configured using the VALET_CONFIG_FILE environment variable).\n\nYou can run valet self config to open the configuration file with your default editor (the file will get created if it does not yet exist).\n\nDeveloper notes:\n\nYou can enable debug mode with profiling for valet by setting the environment variable VALET_CONFIG_STARTUP_PROFILING to true (it will output to ~/profile_valet.txt).'
CMD_DESCRIPTION_this=$'Valet helps you browse, understand and execute your custom bash commands.\n\nOnline documentation is available at https://github.com/jcaillon/valet.\n\nYou can call valet without any commands to start an interactive session.\n\nExit codes:\n\n- 0: everything went well\n- 1+: an error occured\n\nCreate your own commands:\n\nYou 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 or the examples under examples.d to do so.\nValet looks for commands in the valet user directory, which default to ~/.valet.d and can be overwritten using an environment variable (see below).\nOnce you have created your new command script, run the valet self build command to update the valet menu.\n\nConfiguration through environment variables:\n\nIn addition to the environment variables defined for each options, you can define environment variables to configure valet.\n\nThese variables are conviently defined in the valet user config file, located by default at ~/.config/valet/config (the path to this file can be configured using the VALET_CONFIG_FILE environment variable).\n\nYou can run valet self config to open the configuration file with your default editor (the file will get created if it does not yet exist).\n\nDeveloper notes:\n\nYou can enable debug mode with profiling for valet by setting the environment variable VALET_CONFIG_STARTUP_PROFILING to true (it will output to ~/profile_valet.txt).'
CMD_EXAMPLES_DESCRIPTION_selfMock2=$'Call command1 with option1, option2 and some arguments.\n'
CMD_EXAMPLES_DESCRIPTION_showCommandHelp='Shows the help for the command cmd'
CMD_EXAMPLES_DESCRIPTION_this='Displays this help text.'
Expand Down
1 change: 1 addition & 0 deletions valet
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ description: |-
- 1+: an error occured
⌜Create your own commands:⌝
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 or the examples under examples.d to do so.
Valet looks for commands in the valet user directory, which default to ~/.valet.d and can be overwritten using an environment variable (see below).
Once you have created your new command script, run the ⌜valet self build⌝ command to update the valet menu.
Expand Down
4 changes: 2 additions & 2 deletions valet.d/commands.d/self-build-utils
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -Eeu -o pipefail
# $1: The file to extract from.
#
# Returns:
# LAST_RETURNED_VALUE_ARRAY: Each extracted content in an array.
# LAST_RETURNED_ARRAY_VALUE: Each extracted content in an array.
#
# Usage:
# extractCommandYamls "myfile"
Expand Down Expand Up @@ -41,7 +41,7 @@ function extractCommandYamls() {
fi
done <"${file}"

LAST_RETURNED_VALUE_ARRAY=("${contentArray[@]}")
LAST_RETURNED_ARRAY_VALUE=("${contentArray[@]}")
}

# This function extracts properties from a command definition and stores them in
Expand Down
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 @@ -211,7 +211,7 @@ function extractCommandDefinitionsToVariables() {
log::info "Extracting commands from ⌜${file}⌝."
extractCommandYamls "${file}"
local content
for content in "${LAST_RETURNED_VALUE_ARRAY[@]}"; do
for content in "${LAST_RETURNED_ARRAY_VALUE[@]}"; do

if log::isDebugEnabled; then log::debug "Extracting command definition for: ⌜${content%%$'\n'*}...⌝."; fi

Expand Down
25 changes: 23 additions & 2 deletions valet.d/lib-array
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,19 @@ function array::makeArraysSameSize() {
# $1: the name of the array to sort
# $2+: the names of the arrays to use as criteria
#
# Returns:
# The original array is sorted in place.
# An array that contains the corresponding indexes of the sorted array in the original array in the global variable LAST_RETURNED_ARRAY_VALUE
#
# Usage:
# declare -g myArray=( "a" "b" "c" )
# declare -g criteria1=( 3 2 2 )
# declare -g criteria2=( 1 3 2 )
# array::sortWithCriteria myArray criteria1 criteria2
# echo "${LAST_RETURNED_VALUE_ARRAY[@]}"
# echo "${myArray[@]}"
# > c b a
# echo "${LAST_RETURNED_ARRAY_VALUE[@]}"
# > 3 2 1
function array::sortWithCriteria() {
local -n array="${1}"
shift
Expand Down Expand Up @@ -173,6 +179,8 @@ function array::sortWithCriteria() {
sortedArray+=("${array[i]}")
done

LAST_RETURNED_ARRAY_VALUE=("${indexes[@]}")

array=("${sortedArray[@]}")
}

Expand All @@ -191,6 +199,7 @@ function array::sortWithCriteria() {
#
# Returns:
# an the sorted and filtered in the global variable LAST_RETURNED_ARRAY_VALUE
# an array containing the indexes of the matched lines in the original array in the global variable LAST_RETURNED_ARRAY_VALUE2
#
# Usage:
# array::fuzzyFilterSort "pattern" "myarray" && local filteredArray="${LAST_RETURNED_ARRAY_VALUE}"
Expand All @@ -208,6 +217,7 @@ function array::fuzzyFilterSort() {
local afterChar="${4:-}"
local maxMatchedLineLength="${5:-9999999}"

local -a originalIndexes
ARRAY_MATCHES=()
ARRAY_INDEXES=()
ARRAY_DISTANCES=()
Expand All @@ -219,9 +229,11 @@ function array::fuzzyFilterSort() {
shopt -s nocasematch

local line patternChar lineChar matchedLine hasBeginEllipsis hasEndEllipsis
local -i lineCharIndex patternCharIndex lastLineCharIndex distance patternFirstCharIndex
local -i lineCharIndex patternCharIndex lastLineCharIndex distance patternFirstCharIndex itemIndex=-1

for line in "${array[@]}"; do
itemIndex+=1

lineLength="${#line}"

hasBeginEllipsis=false
Expand Down Expand Up @@ -317,15 +329,24 @@ function array::fuzzyFilterSort() {
ARRAY_MATCHES+=("${matchedLine}")
ARRAY_INDEXES+=("${patternFirstCharIndex}")
ARRAY_DISTANCES+=("${distance}")

originalIndexes+=("${itemIndex}")
fi

done


shopt -u nocasematch

# sort the results
array::sortWithCriteria ARRAY_MATCHES ARRAY_INDEXES ARRAY_DISTANCES

# create the final correspondance array
LAST_RETURNED_ARRAY_VALUE2=()
for i in "${LAST_RETURNED_ARRAY_VALUE[@]}"; do
LAST_RETURNED_ARRAY_VALUE2+=("${originalIndexes[i]}")
done

LAST_RETURNED_ARRAY_VALUE=("${ARRAY_MATCHES[@]}")

unset ARRAY_MATCHES ARRAY_INDEXES ARRAY_DISTANCES
Expand Down
Loading

0 comments on commit a2ae4dc

Please sign in to comment.