Skip to content

Commit 2556c4e

Browse files
committed
Bash functions: revamp moulti_process_lines.
1 parent 4143e67 commit 2556c4e

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

examples/moulti-functions.bash

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,42 @@ function moulti_type {
7474
MOULTI_NEW_STEP_PATTERN='^([-=#@]+)(\s*)(.+?)\2\1$'
7575

7676
function moulti_process_lines {
77-
# Optional user hooks:
78-
command -v moulti_make_step > /dev/null && custom_make_step=1
79-
command -v moulti_inspect_line > /dev/null && inspect_line=1
80-
# Main loop:
8177
local step_counter=0
78+
IFS=''
8279
while read -r line; do
8380
if [[ "${line}" =~ ${MOULTI_NEW_STEP_PATTERN} ]]; then
8481
((++step_counter))
8582
local prev_id="${step_id}"
8683
local next_id="$$_${step_counter}"
87-
if [ "${custom_make_step}" ]; then
88-
step_id=$(moulti_make_step "${prev_id}" "${next_id}" "${line}" "${BASH_REMATCH[@]}" < /dev/null)
89-
[ "${step_id}" ] || step_id="${prev_id}"
84+
step_id=$(moulti_make_step "${prev_id}" "${next_id}" "${line}" "${BASH_REMATCH[@]}" < /dev/null)
85+
if [ "${step_id}" ] && [ "${step_id}" != "${prev_id}" ]; then
86+
# Redirect stdout to `moulti pass our_new_step`
87+
exec > >(moulti pass "${step_id}")
9088
else
91-
step_id="${next_id}"
92-
moulti step add "${step_id}" --title="${line}" --bottom-text=' ' --scroll-on-activity=-1 < /dev/null
89+
step_id="${prev_id}"
9390
fi
94-
# Redirect stdout to `moulti pass our_new_step`
95-
exec > >(moulti pass "${step_id}")
96-
fi
97-
if [ "${inspect_line}" ]; then
98-
moulti_inspect_line "${line}" "${step_id}" < /dev/null
99-
else
100-
printf '%s\n' "${line}"
10191
fi
92+
moulti_inspect_line "${line}" "${step_id}" < /dev/null
10293
done
10394
}
95+
96+
# Arguments:
97+
# 1: previous step id
98+
# 2: next step id (suggestion)
99+
# 3: current line (complete, no CR/LF)
100+
# 4: matched substring
101+
# 5: capture #1
102+
# 6: capture #2, etc.
103+
# This function MUST output the id of the created step.
104+
function moulti_make_step {
105+
# Default implementation: use the complete line as title:
106+
moulti step add "$2" --title="$3" --bottom-text=' ' --scroll-on-activity=-1 && echo "$2"
107+
}
108+
109+
# Arguments:
110+
# 1: current line (complete, no CR/LF)
111+
# 2: current step id
112+
function moulti_inspect_line {
113+
# Default implementation: systematically output the line:
114+
printf '%s\n' "${line}"
115+
}

0 commit comments

Comments
 (0)