Skip to content

Commit 3c034fe

Browse files
authored
Add task auto-completion with zsh to the Taskfile setup (#22)
* Add task autocompletion * Clean up awk regex * Allow : characters in task function names for autocompletion * Remove no longer needed : disclaimer in the readme * Keep the usage simple and plain * Fix settings window shrinking
1 parent f9d061c commit 3c034fe

File tree

5 files changed

+59
-21
lines changed

5 files changed

+59
-21
lines changed

README.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ Generate your own Taskfile at [taskfile.sh](https://taskfile.sh).
1616
- Easy to understand and maintain
1717
- Automatically generated list of available task
1818

19-
## How does it work?
19+
# How does it work?
2020

2121
Taskfiles are simple bash scripts, but an easy-to-read function format. There are some things that we need to explain
2222
for our Taskfile setup. It all starts with a `Taskfile`. Download your `Taskfile` from
2323
[taskfile.sh](https://taskfile.sh) and save it. Make sure the Taskfile is executable: `chmod +x ./Taskfile`. You can now
2424
run `./Taskfile` in your terminal.
2525

26-
### Tasks
26+
## Tasks
2727

2828
A task is defined by creating a function that starts with `task:`. This defines a task that can be triggered by running
2929
the `./Taskfile`. Right next to the task, you should add a task definition with two hashes. This will let the
@@ -40,7 +40,7 @@ function task:example { ## Show some example text
4040
In a task you can call other functions, and run all tooling you desire. Now running `./Taskfile example` will execute
4141
the new task.
4242

43-
### Sections
43+
## Sections
4444

4545
To group multiple tasks, sections can be created in your Taskfile. A section is created by creating a comment line with
4646
a double hashtag like so:
@@ -51,17 +51,43 @@ a double hashtag like so:
5151

5252
Lines with only a single `#` will not appear as section in `task:help` and can be seen as plain comments.
5353

54-
### Help command
54+
## Help command
5555

5656
Running `./Taskfile help`, the `task:help` function is triggered. This task will list all available sections and tasks
5757
using the double `##` comments you've learned about above. Now it's clear how you can run any other task!
5858

59-
## Credits
59+
# Auto-completion
60+
61+
Autocompletion works when you use `zsh` and `oh-my-zsh`. Create the following file in your oh-my-zsh directory
62+
`~/.oh-my-zsh/completions/_task.zsh`:
63+
64+
```shell
65+
#compdef task
66+
67+
_task() {
68+
local -a commands
69+
local tasks=$(task comp_targets)
70+
71+
while IFS= read -r line; do
72+
if [[ -n "$line" ]]; then
73+
commands+=("$line")
74+
fi
75+
done <<< "$tasks"
76+
77+
_describe -t commands 'task commands' commands
78+
}
79+
80+
_task "$@"
81+
```
82+
83+
Now after running `task shorthand`, your `task` commands will get autocompleted.
84+
85+
# Credits
6086

6187
This Taskfile setup is based on [Adrian Cooney's Taskfile](https://github.com/adriancooney/Taskfile) and is widely
6288
adopted by [Enrise](https://enrise.com) in our modified flavour.
6389

64-
## Contributors
90+
# Contributors
6591

6692
A big thanks to all the contributors of Taskfile!
6793

Taskfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function task:help { ## Show all available tasks
130130
{printf "\033[33m%-34s\033[0m %s\n", $1, $2}' $0 |\
131131
sed -E "s/[#]{2,}[ ]*/${RESET}/g" |\
132132
sed -E "s/function task:*/ /g"
133-
echo -e "\n${BLUE}Usage:${RESET} $0 ${YELLOW}<task>${RESET} <args>"
133+
echo -e "\n${BLUE}Usage:${RESET} ./Taskfile ${YELLOW}<task>${RESET} <args>"
134134
}
135135

136136
function task:shorthand { ## Create CLI shorthand task instead of ./Taskfile

bin/task

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@ RED=$(printf '\033[31m')
1010
RESET=$(printf '\033[0m')
1111

1212
while [ -d $CURRENT_DIR ] && [ $CURRENT_DIR != '/' ]; do
13-
if [ -f ./Taskfile ];
14-
then
15-
./Taskfile $@
16-
exit $?
17-
elif [ -f ./Makefile ]; then
18-
echo -e "${RED}Found a Makefile instead of a Taskfile...${RESET}\n"
19-
make $@
13+
if [[ -e "${CURRENT_DIR}/Taskfile" ]]; then
14+
TASKFILE="${CURRENT_DIR}/Taskfile"
15+
break
16+
fi
17+
18+
if [[ -e "${CURRENT_DIR}/Makefile" ]]; then
19+
echo -e "Found a ${RED}Makefile${RESET} instead of a Taskfile...\n"
20+
make --directory=$CURRENT_DIR $@
2021
exit $?
21-
else
22-
cd ../
23-
CURRENT_DIR=$(pwd)
2422
fi
23+
24+
CURRENT_DIR=$(dirname $CURRENT_DIR)
2525
done
2626

27-
echo -e "${RED}ERROR: ${RESET}./Taskfile not found in the current or parent directories."
28-
exit 1
27+
if [[ -z "$TASKFILE" ]]; then
28+
echo -e "${RED}ERROR: ${RESET}./Taskfile not found in the current or parent directories."
29+
exit 1
30+
fi
31+
32+
if [[ $1 == 'comp_targets' ]]; then
33+
awk 'BEGIN {FS = " { [#][#][ ]?"} /^function task:(.*)[#][#][ ]?/ {gsub(/:/, "\\:", $1); printf "%s:%s\n", $1, $2}' \
34+
"$TASKFILE" \
35+
| sed -E 's/[#]{2,}[ ]*//g' \
36+
| sed -E 's/function task\\://g'
37+
exit 1
38+
fi
39+
40+
$TASKFILE $@
41+
exit $?

src/components/Generator/GeneredTaskfile/taskfile-base.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function task:help { ## Show all available tasks
5858
{printf "\033[33m%-34s\033[0m %s\n", $1, $2}' $0 |\
5959
sed -E "s/[#]{2,}[ ]*/${RESET}/g" |\
6060
sed -E "s/function task:*/ /g"
61-
echo -e "\n${BLUE}Usage:${RESET} $0 ${YELLOW}<task>${RESET} <args>"
61+
echo -e "\n${BLUE}Usage:${RESET} ./Taskfile ${YELLOW}<task>${RESET} <args>"
6262
}
6363

6464
function task:shorthand { ## Create CLI shorthand task instead of ./Taskfile

src/components/Generator/generator.module.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
@include from(large) {
3232
width: 25rem;
3333
align-self: flex-start;
34-
max-height: none;
3534
}
3635
}
3736

0 commit comments

Comments
 (0)